You are here

function entity_extra_field_display in Entity Extra Field 8

Same name and namespace in other branches
  1. 2.0.x entity_extra_field.module \entity_extra_field_display()

Entity extra field display.

Parameters

$type: The display type, (e.g. view, form).

$build: An array of elements to attach the extra field.

\Drupal\Core\Entity\EntityInterface $entity: An entity instance.

\Drupal\Core\Entity\Display\EntityDisplayInterface $display: An entity display instance.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Component\Plugin\Exception\PluginException

2 calls to entity_extra_field_display()
entity_extra_field_entity_view in ./entity_extra_field.module
Implements hook_entity_view().
entity_extra_field_form_alter in ./entity_extra_field.module
Implements hook_form_alter().

File

./entity_extra_field.module, line 105
The hook implementation for the entity extra field module.

Code

function entity_extra_field_display($type, &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityDisplayInterface $display) {
  $contexts = [];
  $storage = entity_extra_field_storage();
  $extra_field_ids = $storage
    ->getQuery()
    ->condition('display.type', $type)
    ->condition('base_bundle_type_id', $entity
    ->bundle())
    ->condition('base_entity_type_id', $entity
    ->getEntityTypeId())
    ->execute();
  if (!empty($extra_field_ids)) {
    $contexts[$entity
      ->getEntityTypeId()] = $entity;
    $extra_fields = $storage
      ->loadMultiple(array_values($extra_field_ids));

    /** @var \Drupal\entity_extra_field\Entity\EntityExtraField $extra_field */
    foreach ($extra_fields as $extra_field) {
      $contexts[$extra_field
        ->getEntityTypeId()] = $extra_field;
      $all_conditions_pass = $extra_field
        ->getFieldTypeConditionsAllPass();
      if (!$extra_field instanceof EntityExtraFieldInterface || !$extra_field
        ->hasDisplayComponent($display) || !$extra_field
        ->hasConditionsBeenMet($contexts, $all_conditions_pass)) {
        continue;
      }
      $build[$extra_field
        ->name()] = $extra_field
        ->build($entity, $display);
      if ($attachments = $extra_field
        ->getBuildAttachments()) {
        $build[$extra_field
          ->name()]['#attached'] = $attachments;
      }
      $build[$extra_field
        ->name()]['#cache']['tags'] = [
        'entity_extra_field',
        "entity_extra_field:{$type}.{$entity->getEntityTypeId()}.{$entity->bundle()}",
      ];
    }
  }
}