You are here

function amp_node_view in Accelerated Mobile Pages (AMP) 7

Implements hook_node_view().

File

./amp.module, line 626

Code

function amp_node_view($node, $view_mode, $langcode) {
  if ($node = menu_get_object()) {
    if (!in_array($node->type, amp_get_enabled_types()) || !amp_node_is_enabled($node->nid)) {
      return;
    }
  }

  // Show amphtml links on AMP-enabled nodes so search engines can find AMP.
  if ($view_mode == 'full' && node_is_page($node)) {
    $uri = entity_uri('node', $node);
    $uri['options']['query']['amp'] = NULL;
    $uri['options']['absolute'] = TRUE;
    drupal_add_html_head_link(array(
      'rel' => 'amphtml',
      'href' => url($uri['path'], $uri['options']),
    ), TRUE);
  }

  // Add AMP metadata.
  if ($view_mode == 'amp' && node_is_page($node)) {
    $metadata = variable_get('amp_metadata_options_' . $node->type);
    $amp_metadata_type_fields = _amp_get_metadata_type_fields();
    $metadata_json['@context'] = 'http://schema.org';
    $main_uri = entity_uri('node', $node);
    $main_uri['options']['absolute'] = TRUE;
    $metadata_json['mainEntityOfPage'] = url($main_uri['path'], $main_uri['options']);
    foreach ($amp_metadata_type_fields as $name => $values) {
      if ($values['type'] == 'schema') {
        $metadata_json['@type'] = $metadata['amp_metadata_config_' . $name];
      }
      elseif ($values['type'] == 'text') {
        $metadata_json[$name] = $metadata['amp_metadata_config_' . $name];
      }
      elseif ($values['type'] == 'date') {
        $metadata_json[$name] = $metadata['amp_metadata_config_' . $name];
      }
      elseif ($values['type'] == 'Person') {
        $metadata_json[$name] = array(
          '@type' => 'Person',
          'name' => $metadata['amp_metadata_config_' . $name],
        );
      }
      elseif ($values['type'] == 'ImageObject') {
        $image_url = token_replace($metadata['amp_metadata_config_' . $name], array(
          'node' => $node,
        ));

        // Provide backup parsing of image element if token does not output a URL.
        if (strip_tags($image_url) != $image_url) {

          // Force path to be absolute.
          if (strpos($image_url, 'img src="/') !== FALSE) {
            global $base_root;
            $image_url = str_replace('img src="/', 'img src="' . $base_root . '/', $image_url);
          }
          $matches = array();
          preg_match('/src="([^"]*)"/', $image_url, $matches);
          if (!empty($matches[1])) {
            $image_url = $matches[1];
          }
        }

        // Obtain URI of absolute URL.
        $uri = 'public://';
        $public_stream_base_url = file_create_url($uri);
        $image_uri = '';
        if (substr($image_url, 0, strlen($public_stream_base_url)) == $public_stream_base_url) {
          $image_uri = file_build_uri(substr($image_url, strlen($public_stream_base_url)));
        }
        if (!empty($image_url) && !empty($image_uri)) {
          if (!isset($metadata['amp_metadata_config_imageStyle']) || empty($image_style_id = $metadata['amp_metadata_config_imageStyle'])) {
            $image_style_id = '';
          }
          if (!empty($image_info = _amp_get_image_information($image_url, $image_uri, $image_style_id))) {
            $metadata_json[$name] = array(
              '@type' => 'ImageObject',
              'url' => $image_info['url'],
              'width' => $image_info['width'],
              'height' => $image_info['height'],
            );
          }
        }
      }
    }

    // Add global publisher info.
    $metadata_json['publisher'] = array(
      '@type' => 'Organization',
      'name' => variable_get('amp_metadata_organization_name', ''),
    );
    $logo_fid = variable_get('amp_metadata_organization_logo', NULL);
    if ($logo_fid) {
      $logo = file_load($logo_fid);
      $logo_uri = $logo->uri;
      $logo_url = file_create_url($logo_uri);
      if (!empty($logo_url) && !empty($logo_uri)) {
        $logo_style_id = variable_get('amp_metadata_organization_logo_image_style_id', '');
        if (!empty($logo_info = _amp_get_image_information($logo_url, $logo_uri, $logo_style_id))) {
          $metadata_json['publisher']['logo'] = array(
            '@type' => 'ImageObject',
            'url' => $logo_info['url'],
            'width' => $logo_info['width'],
            'height' => $logo_info['height'],
          );
        }
      }
    }
    drupal_alter('amp_metadata', $metadata_json, $node, $type);

    // Replace the tokens.
    array_walk_recursive($metadata_json, function (&$value) use ($node) {
      $value = strip_tags(token_replace($value, [
        'node' => $node,
      ]));
      $value = str_replace("\n", "", $value);
      $value = str_replace("\r", "", $value);
    });
    $element = array(
      '#tag' => 'script',
      '#type' => 'html_tag',
      '#attributes' => array(
        'type' => 'application/ld+json',
      ),
      '#value' => json_encode($metadata_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT),
      array(
        'node' => $node,
      ),
    );
    drupal_add_html_head($element, 'amp_metadata');
  }
}