You are here

public function MediaImageDecorator::build in Entity Embed 8

Builds the renderable array for this Entity Embed display plugin.

Return value

array A renderable array representing the content of the embedded entity.

Overrides EntityEmbedDisplayInterface::build

File

src/Plugin/entity_embed/EntityEmbedDisplay/MediaImageDecorator.php, line 212

Class

MediaImageDecorator
Decorator on all EntityEmbedDisplays that adds alt and title overriding.

Namespace

Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay

Code

public function build() {
  $build = $this->decorated
    ->build();

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $this->decorated
    ->getEntityFromContext();
  if ($image_field = $this
    ->getMediaImageSourceField($entity)) {
    $settings = $entity->{$image_field}
      ->getItemDefinition()
      ->getSettings();
    if (!empty($settings['alt_field']) && $this
      ->hasAttribute('alt')) {
      $entity->{$image_field}->alt = $this
        ->getAttributeValue('alt');
      $entity->thumbnail->alt = $this
        ->getAttributeValue('alt');
    }
    if (!empty($settings['title_field']) && $this
      ->hasAttribute('title')) {
      $entity->{$image_field}->title = $this
        ->getAttributeValue('title');
      $entity->thumbnail->title = $this
        ->getAttributeValue('title');
    }
  }
  return $build;
}