Commit 74039e82 authored by acbramley's avatar acbramley

Issue #2991860: Provide block label field formatter

parent 781d5a16
<?php
namespace Drupal\block_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
/**
* Plugin implementation of the 'block_field_label' formatter.
*
* @FieldFormatter(
* id = "block_field_label",
* label = @Translation("Block field label"),
* field_types = {
* "block_field"
* }
* )
*/
class BlockFieldLabelFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
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 || !$block_instance->access(\Drupal::currentUser())) {
continue;
}
$elements[$delta] = [
'#markup' => $block_instance->label(),
];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$renderer->addCacheableDependency($elements[$delta], $block_instance);
}
return $elements;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment