You are here

function amp_entity_view_alter in Accelerated Mobile Pages (AMP) 8

Same name and namespace in other branches
  1. 8.3 amp.module \amp_entity_view_alter()
  2. 8.2 amp.module \amp_entity_view_alter()

Implements hook_entity_view_alter().

File

./amp.module, line 44

Code

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

  // Check if entity is a non-new node in either full or AMP view mode.

  /** @var NodeInterface $entity */
  if ($entity instanceof NodeInterface && !$entity
    ->isNew() && in_array($build['#view_mode'], [
    'full',
    'amp',
  ])) {

    // Get a list of available view modes for the current entity.
    $view_modes = \Drupal::service('entity_display.repository')
      ->getViewModeOptionsByBundle('node', $entity
      ->bundle());

    // Double-check that the AMP view mode is enabled for this node type.
    if (isset($view_modes['amp'])) {
      $build['#cache']['contexts'][] = 'url.query_args:amp';
      $absolute_canonical = $entity
        ->toUrl('canonical', [
        'absolute' => TRUE,
      ])
        ->toString();
      if (!empty($build['#attached']['html_head_link'])) {
        $canonical_href = '';
        foreach ($build['#attached']['html_head_link'] as $key => $config) {
          if ($build['#view_mode'] === 'amp') {
            if ($config[0]['rel'] != 'canonical' && $config[0]['rel'] != 'shortlink') {
              unset($build['#attached']['html_head_link'][$key]);
            }
            elseif ($config[0]['rel'] == 'canonical') {

              // Replace the canonical link with an absolute version, this is
              // required for AMP pages and recommended for others.
              // @todo Remove this when https://www.drupal.org/node/2738373 is
              //   fixed.
              $build['#attached']['html_head_link'][$key][0]['href'] = $absolute_canonical;
              $build['#cache']['contexts'][] = 'url.site';
            }
          }
          elseif ($build['#view_mode'] === 'full') {
            if ($config[0]['rel'] == 'canonical' && !empty($config[0]['href'])) {

              // Check if the canonical link is absolute and external. Do not
              // expose AMP for those.
              $current_canonical = $config[0]['href'];
              if (UrlHelper::isExternal($current_canonical) && !UrlHelper::externalIsLocal($current_canonical, \Drupal::service('router.request_context')
                ->getCompleteBaseUrl())) {
                continue;
              }
              $amp_href = \Drupal::service('amp.query_parameters')
                ->add($absolute_canonical);
              $build['#attached']['html_head_link'][] = [
                [
                  'rel' => 'amphtml',
                  'href' => $amp_href,
                ],
                TRUE,
              ];
              $build['#cache']['contexts'][] = 'url.site';
            }
          }
        }
        if ($build['#view_mode'] === 'amp' && !empty($absolute_canonical)) {
          if (!empty($amp_merged_metadata = \Drupal::service('amp.merge_metadata')
            ->getMergedMetadata($entity
            ->getType()))) {
            if (!empty($amp_json_metadata = \Drupal::service('amp.prepare_metadata_json')
              ->getJson($amp_merged_metadata, $absolute_canonical, $entity))) {
              $build['#attached']['html_head']['amp_metadata_json'] = [
                // The metadata script element.
                [
                  '#type' => 'html_tag',
                  '#tag' => 'script',
                  '#attributes' => [
                    'type' => 'application/ld+json',
                  ],
                  '#value' => $amp_json_metadata,
                ],
                // The render array key.
                'amp_metadata',
              ];
              $build['#cache']['tags'][] = 'amp_metadata';
              $build['#cache']['tags'][] = 'amp_available_metadata';
            }
          }
        }
      }
    }
  }
}