You are here

function metatag_page_build in Metatag 7

Implements hook_page_build().

File

./metatag.module, line 1945
Primary hook implementations for Metatag.

Code

function metatag_page_build(array &$page) {

  // By default do not add meta tags to admin pages. To enable meta tags on
  // admin pages set the 'metatag_tag_admin_pages' variable to TRUE.
  if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) {
    return;
  }

  // Special consideration for the Me module, which uses the "user/me" path and
  // will cause problems.
  if (arg(0) == 'user' && arg(1) == 'me' && function_exists('me_menu_alter')) {
    return;
  }

  // The page region can be changed.
  $region = variable_get('metatag_page_region', 'content');

  // Ensure these arrays exist, otherwise several use cases will fail.
  if (!isset($page[$region]) || !is_array($page[$region])) {
    $page[$region] = array();
  }
  if (!isset($page[$region]['metatags']) || !is_array($page[$region]['metatags'])) {
    $page[$region]['metatags'] = array();
  }

  // The front page has special consideration. Also, check if this is an error
  // (403/404) page, those also require separate handling.
  $instance = 'global:frontpage';
  if (drupal_is_front_page() && metatag_config_is_enabled($instance) || ($instance = metatag_is_error_page())) {

    // Generate the cache ID.
    $cid_parts = array(
      'instance' => $instance,
    );
    $cid = metatag_cache_default_cid_parts($cid_parts);
    if (variable_get('metatag_cache_output', FALSE)) {
      if ($cache = metatag_cache_get($cid)) {
        $metatags = $cache->data;
      }
      else {
        $metatags = metatag_metatags_view($instance, array());
        metatag_cache_set($cid, $metatags);
      }
    }
    else {
      $metatags = metatag_metatags_view($instance, array());
    }
    $page[$region]['metatags'][$instance] = $metatags;
  }
  else {
    $page[$region]['metatags'] += metatag_page_get_metatags();
  }

  // If no meta tags were loaded at least load the global defaults. This may be
  // disabled, see README.txt for details.
  if (empty($page[$region]['metatags']) && variable_get('metatag_load_all_pages', TRUE)) {
    $instance = 'global';

    // Generate the cache ID.
    $cid_parts = array(
      'instance' => $instance,
      'path' => request_path(),
    );
    $cid = metatag_cache_default_cid_parts($cid_parts);
    if ($cache = metatag_cache_get($cid)) {
      $metatags = $cache->data;
    }
    else {
      $metatags = metatag_metatags_view($instance, array());

      // If output caching is enabled, save this for later.
      if (variable_get('metatag_cache_output', FALSE)) {
        metatag_cache_set($cid, $metatags);
      }
    }
    $page[$region]['metatags'][$instance] = $metatags;
  }
}