You are here

function metatag_panels_ctools_render_alter in Metatag 7

Implements hook_ctools_render_alter().

File

metatag_panels/metatag_panels.module, line 137
Main file for metatag_panels module.

Code

function metatag_panels_ctools_render_alter($info, $page, $context) {

  // 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;
  }
  $output =& drupal_static('metatag_panels');
  $handler = $context['handler'];
  if (empty($handler->conf['metatag_panels']) || !$handler->conf['metatag_panels']['enabled']) {
    return;
  }
  $metatags = $handler->conf['metatag_panels']['metatags'];
  if (!is_array($metatags) || empty($metatags)) {
    $metatags = array();
  }

  // If meta tags were found but they're not nested for the language, fix it.
  // This leaves some possibility for future versions to support translation.
  if (empty($metatags) || !isset($metatags[LANGUAGE_NONE])) {
    $metatags = array(
      LANGUAGE_NONE => $metatags,
    );
  }

  // Translate all of the meta tags using i18n.
  metatag_translate_metatags($metatags[LANGUAGE_NONE], 'metatag_panels:' . $handler->name, NULL, FALSE);

  // Append global defaults.
  $all_metatags = array();
  foreach ($metatags as $langcode => $values) {
    if (!empty($values)) {
      $all_metatags = $values + metatag_config_load_with_defaults('');
    }
  }
  $metatags = $all_metatags;
  $instance = 'panels:' . $handler->name;
  $options = array(
    'instance' => $instance,
  );

  // Trigger hook_metatag_metatags_alter(). Allow the raw meta tags to be
  // modified prior to rendering.
  drupal_alter('metatag_metatags', $metatags, $instance, $options);
  if (empty($metatags)) {
    return;
  }

  // Substitute Panels context variables.
  foreach ($metatags as $metatag => $data) {
    if (isset($data['value']) && is_string($data['value']) && strpos($data['value'], '%') !== FALSE) {
      $metatags[$metatag]['value'] = check_plain(urldecode(ctools_context_keyword_substitute($data['value'], array(), $context['handler']->conf['display']->context)));
    }
  }

  // Get the contexts that exist within this panel.
  ctools_include('context-task-handler');
  $task_object = ctools_context_handler_get_task_object($context['task'], $context['subtask'], $context['handler']);
  $task_contexts = ctools_context_load_contexts($task_object, TRUE, $context['contexts']);

  // Build the tokens out of CTools contexts.
  $tokens = array();
  foreach ($task_contexts as $task_context) {
    $tokens[$task_context->keyword] = $task_context->data;
  }

  // Because of page execution order, sometimes the page title does not get set
  // by Panels in time for metatags to use it, so we'll explicitly set it here
  // if we need to.
  if (!empty($info['title'])) {
    drupal_set_title($info['title'], PASS_THROUGH);
  }

  // Don't output meta tags that only contain the pager.
  $current_pager = metatag_get_current_pager();

  // Build the Metatag.
  $options['token data'] = $tokens;
  foreach ($metatags as $metatag => $data) {

    // Render CTools context substitution values prior to rendering the meta
    // tag.
    if (isset($data['value']) && is_string($data['value'])) {
      $data['value'] = check_plain(urldecode(ctools_context_keyword_substitute(trim($data['value']), array(), $task_contexts)));
    }
    $metatag_instance = metatag_get_instance($metatag, $data);
    if ($metatag_instance) {
      $tag_output = $metatag_instance
        ->getElement($options);

      // Don't output the pager if that's all there is.
      if ($tag_output != $current_pager) {
        $output[$metatag] = $tag_output;
      }
    }
  }

  // Give third-parties the opportunity to alter the metatag for a given
  // instance.
  drupal_alter('metatag_metatags_view', $output, $options['instance']);
}