Unpacking Flexible Content Fields

When building flexible content row specs, it is often cleanest to keep data unpacking out of templates. To accomodate this, rowspecs have a basic unpacking framework.

Unpack

The unpack method is provided to be included in a blade template to got the row's underlying data. It will return an object with properties for each piece of row data. In your template, you would call

@php
    $row_data = App\Crud\Rows\RowClass::unpack($row);
@endphp

Common Content

The getCommonContent provides basic content that all flexible content rows should have. At present, this is the row_id, row_label from the rows settings and row_type as the type of the row.

Row Content

To parse field content into usable data, override and populate the getRowContent function. This should return an array of parsed data, and will be merged with getCommonContent to provide a row's full data. There are also some helpers provided to simplify getting to row content from the main $row argument.

Typically, you will want to use $row->blockContent('field_name') or $row->settingContent('setting_field_name') to get the actual value entered into fields.

Generally, it is better to provide every key (with falsey or null values when they are otherwise not present) than to filter this list or hide properties with no value. Developers using this data in the blade template can then be sure of which properties will and won't exist for any given row.

Simple Fields (Text)

protected static function getRowContent(FlexibleRow $row): array
{
    return [
        'text' => $row->blockContent('text'),
    ];
}

Array Fields (Select)

The actual value returned by blockContent for select fields is an array describing the option that was selected, not the value of that option.

protected static function getRowContent(FlexibleRow $row): array
{
    return [
        'layout' => Arr::get($row->blockContent('layout'), 'id', 'default_value'),
    ];
}

Note that if you have changed the tracking config property in enso.settings.select_track_by, id will not be the correct array key.

File Fields

The blockContent of Fields which provide file upload/selection store just the ids, but usually return a Collection of the files from the database. Be aware that this is true even if your upload field limits the number of files to 1, so if you expect to only ever have one image, you may wish to then select the first() child of the collection.

protected static function getRowContent(FlexibleRow $row): array
{
    return [
        'image' => $row->blockContent('image')->first(),
        'images' => $row->blockContent('images'),
    ];
}

Granular access

If blockContent and settingContent can't provide what you need, you can get the blocks directly, and manipulate those instead with getBlock() and getBlocks('field_name')

Helper Methods

There is additional extra functionality for more complex scenarios:

Wysiwyg content

Due to the way that both the html and the json deltas for wysiwyg fields, a helper has been provided to cleanly get this.

static::getWysiwygHtml($row->getBlocks(), 'my_wysiwyg');

Row Specs that contain FlexibleContentFields

Sometimes you will need to add rowspecs within rowspecs. To retrieve this content this, we have provided a flexibleFieldContent method. It will get the FlexibleContentField to iterate over it's content and call the respective unpack funcations on those rows, returning the resulting data objects as a collection.

protected static function getRowContent(FlexibleRow $row): array
{
    return [
        'content' => static::flexibleFieldContent('content', $row),
    ];
}