You are here

function metatag_entity_view_alter in Metatag 8

Implements hook_entity_view_alter().

File

./metatag.module, line 214
Contains metatag.module.

Code

function metatag_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {

  // Don't proceed any further if the entity being viewed isn't the route
  // entity.
  if (!_metatag_is_entity_route_entity($entity)) {
    return;
  }
  if (!$entity
    ->getEntityType()
    ->hasLinkTemplate('canonical')) {
    return;
  }

  // If this is a 403 or 404 page then don't output these meta tags.
  // @todo Make the default meta tags load properly so this is unnecessary.
  if ($display
    ->getOriginalId() == 'node.403.default' || $display
    ->getOriginalId() == 'node.404.default') {
    $build['#attached']['html_head_link'] = [];
    return;
  }

  // Panelized entities still use the original entity's controller, but with
  // custom built entities. In those cases hook_entity_view_alter might be
  // called too early, where meta links are not yet set.
  // @see \Drupal\node\Controller\NodeController::view
  if ($display
    ->getThirdPartySetting('panelizer', 'enable', FALSE)) {
    $build['#pre_render'][] = function (array $element) {
      return _metatag_panelizer_pre_render($element);
    };
    return;
  }
  _metatag_remove_duplicate_entity_tags($build);
}