|
| 1 | +import Tabs from '@theme/Tabs'; |
| 2 | +import TabItem from '@theme/TabItem'; |
| 3 | + |
| 4 | + |
| 5 | +# Fluid |
| 6 | + |
| 7 | +A Fluid field is a collection of fields. A Fluid field can contain any native fieldtype except another Fluid field. The fields assigned to the Fluid field can then be used multiple times in the same entry when creating/editing the entry. The author also has control over the order of the fields. |
| 8 | + |
| 9 | +## Template Usage |
| 10 | + |
| 11 | +When iterating over the rows of a Fluid field in a template you will have access to the value but also extra data which can be used to control the display of these fields and field groups. |
| 12 | + |
| 13 | +<Tabs> |
| 14 | +<TabItem value="twig" label="Twig"> |
| 15 | + |
| 16 | +```twig |
| 17 | +{% for row in entry.fluid %} |
| 18 | + Value: {{ row }} |
| 19 | +
|
| 20 | + Field Type: {{ row._field_type }} or {{ row.field.field_type }} |
| 21 | + Field Name: {{ row._field_name }} or {{ row.field.field_name }} |
| 22 | + Group Name: {{ row._field_name }} or {{ row.group.short_name }} |
| 23 | +
|
| 24 | + Field (when row is a field): {{ row.field }} |
| 25 | + Group (when row is a field group): {{ row.group }} |
| 26 | +
|
| 27 | + Condition for fields - {% if row.field %} ... {% endif %} |
| 28 | + Condition for field groups - {% if row.group %} ... {% endif %} |
| 29 | +
|
| 30 | +{% endfor %} |
| 31 | +``` |
| 32 | + |
| 33 | +</TabItem> |
| 34 | +<TabItem value="blade" label="Blade"> |
| 35 | + |
| 36 | +```php |
| 37 | +@foreach($entry->fluid as $row) |
| 38 | + Value: {{ $row }} |
| 39 | + |
| 40 | + Field Type: {{ $row->_field_type }} or {{ $row->field.field_type }} |
| 41 | + Field Name: {{ $row->_field_name }} or {{ $row->field.field_name }} |
| 42 | + Group Name: {{ $row->_field_name }} or {{ $row->group.short_name }} |
| 43 | + |
| 44 | + Field (when row is a field): {{ $row->field }} |
| 45 | + Group (when row is a field group): {{ $row->group }} |
| 46 | + |
| 47 | + Condition for fields - @if($row->field) ... @endif |
| 48 | + Condition for field groups - @if($row->group) ... @endif |
| 49 | + |
| 50 | +@endforeach |
| 51 | +``` |
| 52 | + |
| 53 | +</TabItem> |
| 54 | +</Tabs> |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +## Field Groups |
| 59 | + |
| 60 | +ExpressionEngine 7.3 introduced the ability to use field groups within a Fluid field to further improve the content authoring experience. Coilpack makes the Field Group's `short_name` available as the `_field_name` attribute and sets the `_field_type` attribute to a value of `field_group`. We recommend using these attributes to conditionally handle the display of your fields and field groups. |
| 61 | + |
| 62 | +<Tabs> |
| 63 | +<TabItem value="twig" label="Twig"> |
| 64 | + |
| 65 | +```twig |
| 66 | +{% for row in entry.fluid %} |
| 67 | + {% if row._field_name == 'my_group' %} |
| 68 | + {% for group_row in row.fields %} |
| 69 | + {% if group_row._field_name == 'my_text' %} |
| 70 | + <div class="my-text">{{ group_row | raw }}</div> |
| 71 | + {% elseif group_row._field_name == 'my_image' %} |
| 72 | + <div class="my-image"><img src="{{ group_row }}"/></div> |
| 73 | + {% elseif group_row._field_name == 'my_grid' %} |
| 74 | + <table> |
| 75 | + <tr> |
| 76 | + <th>Column 1</th> |
| 77 | + <th>Column 2</th> |
| 78 | + </tr> |
| 79 | + {% for grid_row in group_row %} |
| 80 | + <tr> |
| 81 | + <td>{{ grid_row.column_1 }}</td> |
| 82 | + <td>{{ grid_row.column_2 }}</td> |
| 83 | + </tr> |
| 84 | + {% endfor %} |
| 85 | + </table> |
| 86 | + {% endif %} |
| 87 | + {% endfor %} |
| 88 | + {% endif %} |
| 89 | +{% endfor %} |
| 90 | +``` |
| 91 | + |
| 92 | +</TabItem> |
| 93 | +<TabItem value="blade" label="Blade"> |
| 94 | + |
| 95 | +```php |
| 96 | +@foreach($entry->fluid as $row) |
| 97 | + @if($row->_field_name == 'my_group') |
| 98 | + @foreach($row->fields as $group_row) |
| 99 | + @if($group_row->_field_name == 'my_text') |
| 100 | + <div class="my-text">{!! $group_row !!}</div> |
| 101 | + @elseif($group_row->_field_name == 'my_image') |
| 102 | + <div class="my-image"><img src="{{ $group_row }}"/></div> |
| 103 | + @elseif($group_row->_field_name == 'my_grid') |
| 104 | + <table> |
| 105 | + <tr> |
| 106 | + <th>Column 1</th> |
| 107 | + <th>Column 2</th> |
| 108 | + </tr> |
| 109 | + @foreach($group_row as $grid_row) |
| 110 | + <tr> |
| 111 | + <td>{{ $grid_row->column_1 }}</td> |
| 112 | + <td>{{ $grid_row->column_2 }}</td> |
| 113 | + </tr> |
| 114 | + @endforeach |
| 115 | + </table> |
| 116 | + @endif |
| 117 | + @endforeach |
| 118 | + @endif |
| 119 | +@endfor |
| 120 | +``` |
| 121 | + |
| 122 | +</TabItem> |
| 123 | +</Tabs> |
| 124 | + |
| 125 | + |
| 126 | +## GraphQL |
| 127 | + |
| 128 | +When using GraphQL you can slightly alter the way you interact with a Fluid field by changing the value of the `graphql.prefer_union_types` in `config/coilpack.php`. There are pros and cons for each approach so this is purely a matter of developer preference. |
| 129 | + |
| 130 | +The default behavior does not utilize Union Types. This lends itself to a simpler request structure but will have extra response data with null values. |
| 131 | + |
| 132 | +```graphql |
| 133 | +exp_channel_entries { |
| 134 | + data { |
| 135 | + title |
| 136 | + fluid_field { |
| 137 | + _field_name |
| 138 | + _field_type |
| 139 | + my_group { |
| 140 | + _field_name |
| 141 | + _field_type |
| 142 | + my_text |
| 143 | + my_image |
| 144 | + my_grid { |
| 145 | + column_1 |
| 146 | + column_2 |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +Whereas using [Union Types](https://graphql.org/learn/schema/#union-types) can make the request a little more complex but will have a simpler and more succinct response. |
| 155 | + |
| 156 | +```graphql |
| 157 | +exp_channel_entries { |
| 158 | + data { |
| 159 | + title |
| 160 | + fluid_field { |
| 161 | + ... on fluid__my_group { |
| 162 | + name |
| 163 | + fields { |
| 164 | + __typename |
| 165 | + ... on fluid__my_text { |
| 166 | + mytext:content |
| 167 | + } |
| 168 | + ... on fluid__my_image { |
| 169 | + myimage:content { |
| 170 | + url |
| 171 | + filename |
| 172 | + width |
| 173 | + file_name |
| 174 | + } |
| 175 | + } |
| 176 | + ... on fluid__my_grid { |
| 177 | + mygrid:content { |
| 178 | + column_1 |
| 179 | + column_2 |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +:::info |
| 190 | +See the ExpressionEngine Documentation for more information on the [Fluid fieldtype](https://docs.expressionengine.com/latest/fieldtypes/fluid.html) |
| 191 | +::: |
0 commit comments