You are here

public function ExtraFieldBlockPlugin::build in Entity Extra Field 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/ExtraFieldType/ExtraFieldBlockPlugin.php \Drupal\entity_extra_field\Plugin\ExtraFieldType\ExtraFieldBlockPlugin::build()

Build the render array of the extra field type contents.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity type the extra field is being attached too.

\Drupal\Core\Entity\Display\EntityDisplayInterface $display: The entity display the extra field is apart of.

Return value

array The extra field renderable array.

Throws

\Drupal\Component\Plugin\Exception\PluginException

Overrides ExtraFieldTypePluginInterface::build

File

src/Plugin/ExtraFieldType/ExtraFieldBlockPlugin.php, line 254

Class

ExtraFieldBlockPlugin
Define extra field block type.

Namespace

Drupal\entity_extra_field\Plugin\ExtraFieldType

Code

public function build(EntityInterface $entity, EntityDisplayInterface $display) : array {
  $block = $this
    ->getBlockTypeInstance();
  if (!$block instanceof BlockPluginInterface) {
    return [];
  }
  $element = [];
  if ($block instanceof ContextAwarePluginInterface) {
    try {
      $this
        ->applyPluginRuntimeContexts($block, [
        'display' => EntityContext::fromEntity($display),
        'view_mode' => new Context(ContextDefinition::create('string'), $display
          ->getMode()),
        'entity_extra_field.target_entity' => EntityContext::fromEntity($entity),
      ]);
    } catch (\Exception $exception) {
      watchdog_exception('entity_extra_field', $exception);
    }
  }
  if ($block
    ->access($this->currentUser) && ($build = $block
    ->build())) {
    $element = [
      '#theme' => 'block',
      '#attributes' => [],
      '#configuration' => $block
        ->getConfiguration(),
      '#plugin_id' => $block
        ->getPluginId(),
      '#base_plugin_id' => $block
        ->getBaseId(),
      '#derivative_plugin_id' => $block
        ->getDerivativeId(),
      '#id' => str_replace(':', '-', $block
        ->getPluginId()),
      'content' => $build,
    ];
    CacheableMetadata::createFromRenderArray($element)
      ->merge(CacheableMetadata::createFromRenderArray($element['content']))
      ->addCacheableDependency($block)
      ->applyTo($element);
  }
  return $element;
}