public function BlockFieldLabelFormatter::viewElements in Block field 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 FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ BlockFieldLabelFormatter.php, line 58
Class
- BlockFieldLabelFormatter
- Plugin implementation of the 'block_field_label' formatter.
Namespace
Drupal\block_field\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
/** @var \Drupal\block_field\BlockFieldItemInterface $item */
$block_instance = $item
->getBlock();
// Make sure the block exists and is accessible.
if (!$block_instance) {
continue;
}
$access = $block_instance
->access($this->currentUser
->getAccount(), TRUE);
CacheableMetadata::createFromRenderArray($elements)
->addCacheableDependency($access)
->applyTo($elements);
if (!$access
->isAllowed()) {
continue;
}
$elements[$delta] = [
'#markup' => $block_instance
->label(),
];
CacheableMetadata::createFromRenderArray($elements[$delta])
->addCacheableDependency($block_instance)
->applyTo($elements[$delta]);
}
return $elements;
}