You are here

public function metatag_context_reaction::execute in Metatag 7

Output a list of active contexts.

File

metatag_context/metatag_context.context.inc, line 123
Context reaction for Metatag.

Class

metatag_context_reaction
Context reaction for Metatag.

Code

public function execute() {
  $output =& drupal_static('metatag_context');
  if (!isset($output)) {
    $metatags = array();
    $output = array();
    $contexts = context_active_contexts();
    $options = array();
    $instance_names = array();
    foreach ($contexts as $context) {
      if (!empty($context->reactions['metatag_context_reaction']['metatags'])) {
        $metadata_array = $context->reactions['metatag_context_reaction']['metatags'];
        if (isset($metadata_array[LANGUAGE_NONE])) {
          $metadata_array = $metadata_array[LANGUAGE_NONE];
        }

        // Translate all of the meta tags using i18n, but don't update the
        // strings.
        metatag_translate_metatags($metadata_array, 'metatag_context:' . $context->name, NULL, FALSE);

        // Add the meta tags to the output.
        foreach ($metadata_array as $langcode => $tags) {
          foreach ($tags as $name => $value) {
            $metatags[$langcode][$name] = $value;
          }
        }

        // Add this context to the list of instances.
        $weight = isset($context->reactions['metatag_context_reaction']['weight']) ? $context->reactions['metatag_context_reaction']['weight'] : 0;
        $instance_names[] = array(
          'name' => $context->name,
          'weight' => $weight,
        );
      }
    }

    // Only proceed if metatags were assigned.
    if (!empty($metatags)) {

      // Load the global defaults.
      $metatags += metatag_config_load_with_defaults('');

      // Sort by weight.
      uasort($instance_names, 'drupal_sort_weight');

      // Keep names only.
      $instance_names = array_map('current', $instance_names);

      // Compile the identifier for this combination based on the context
      // names.
      $instance = 'context:' . implode(',', $instance_names);
      $options['instance'] = $instance;

      // If an entity & entity type were saved elsewhere, grab them for later.
      // @see hook_entity_prepare_view().
      $entities = drupal_static('metatag_context_entities');
      if (!empty($entities) && count($entities[1]) == 1) {
        $data = array_values($entities[1]);
        $options['entity'] = $data[0];
        $options['entity_type'] = $entities[0];
        $options['token data'] = array(
          $entities[0] => $data[0],
        );
      }

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

      // Don't output meta tags that only contain the pager.
      $current_pager = metatag_get_current_pager();
      foreach ($metatags as $metatag => $data) {
        if ($metatag_instance = metatag_get_instance($metatag, $data)) {
          $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;
          }
        }
      }

      // Allow the output meta tags to be modified using
      // hook_metatag_metatags_view_alter().
      drupal_alter('metatag_metatags_view', $output, $instance);
    }
  }
}