public function PatternWrapperEntityReferenceFormatter::viewElements in UI Patterns Field Formatters 8
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides EntityReferenceEntityFormatter::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ PatternWrapperEntityReferenceFormatter.php, line 178
Class
- PatternWrapperEntityReferenceFormatter
- Plugin implementation of 'pattern_wrapper_entity_reference_formatter'.
Namespace
Drupal\ui_patterns_field_formatters\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$pattern = $this
->getSetting('pattern');
// Set pattern fields.
$fields = [];
$mapping = $this
->getSetting('pattern_mapping');
$mapping = $mapping[$pattern]['settings'];
foreach ($mapping as $source => $field) {
if ($field['destination'] === '_hidden') {
continue;
}
// Get rid of the source tag.
$source = explode(":", $source)[1];
if ($source === 'items') {
$fields[$field['destination']][] = $elements;
}
if ($source === 'label') {
$fields[$field['destination']][] = $items
->getFieldDefinition()
->getLabel();
}
}
// Set pattern render array.
$build = [
'#type' => 'pattern',
'#id' => $this
->getSetting('pattern'),
'#fields' => $fields,
'#multiple_sources' => TRUE,
];
// Set the variant.
$pattern_variant = $this
->getCurrentVariant($pattern);
if (isset($pattern_variant)) {
$build['#variant'] = $pattern_variant;
}
// Set the settings.
$settings = $this
->getSetting('pattern_settings');
$pattern_settings = !empty($settings) && isset($settings[$pattern]) ? $settings[$pattern] : NULL;
if (isset($pattern_settings)) {
$build['#settings'] = $pattern_settings;
}
// Set pattern context.
$entity = $items
->getEntity();
$build['#context'] = [
'type' => 'field_formatter',
'entity' => $entity,
];
return $build;
}