You are here

class metatag_context_reaction in Metatag 7

Context reaction for Metatag.

Hierarchy

Expanded class hierarchy of metatag_context_reaction

3 string references to 'metatag_context_reaction'
metatag_context_context_page_reaction in metatag_context/metatag_context.module
Implements hook_context_page_reaction().
metatag_context_context_plugins in metatag_context/metatag_context.module
Implements hook_context_plugins().
metatag_context_context_registry in metatag_context/metatag_context.module
Implements hook_context_registry().

File

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

View source
class metatag_context_reaction extends context_reaction {

  /**
   * {@inheritdoc}
   */
  public function options_form($context) {
    $form = array();

    // Don't care about the instance name, the data is being managed by
    // Context and not Metatag.
    $instance = "";

    // Load the previously saved settings.
    $data = $this
      ->fetch_from_context($context);
    if (!isset($data['metatags'])) {
      $data['metatags'] = array();
    }

    // Support the pre-v1.0 data structures that were not nested with the
    // language code.
    if (!isset($data['metatags'][LANGUAGE_NONE])) {
      $data['metatags'] = array(
        LANGUAGE_NONE => $data['metatags'],
      );
    }

    // Provide token types.
    $options = array(
      'token types' => array(
        'node',
        'term',
        'user',
      ),
    );
    $form['help'] = array(
      '#prefix' => '<p><em>',
      '#markup' => t('Values assigned here inherit from the <a href="@url" title="Edit the global default meta tags.">global defaults</a> and will override any other meta tags assigned elsewhere.', array(
        '@url' => url('admin/config/search/metatags/config/global'),
      )),
      '#suffix' => '</em></p>',
    );
    $form['basic_header'] = array(
      '#prefix' => '<hr /><h3>',
      '#markup' => t('Basic tags'),
      '#suffix' => '</h3>',
    );

    // Load the basic Metatag form.
    metatag_metatags_form($form, $instance, $data['metatags'][LANGUAGE_NONE], $options);

    // Stop the meta tag fields appearing within a fieldset.
    $form['metatags']['#type'] = 'container';
    unset($form['metatags']['#collapsed']);
    unset($form['metatags']['#collapsible']);
    unset($form['#submit']);

    // Flatten the fieldsets because otherwise the Context module will not save
    // them properly.
    foreach (element_children($form['metatags'][LANGUAGE_NONE]) as $fieldset) {
      $child = $form['metatags'][LANGUAGE_NONE][$fieldset];
      if (isset($child['#type']) && $child['#type'] == 'fieldset') {
        $form['metatags'][LANGUAGE_NONE][$fieldset . '_heading'] = array(
          '#prefix' => '<hr /><h3>',
          '#markup' => $form['metatags'][LANGUAGE_NONE][$fieldset]['#title'],
          '#suffix' => '</h3>',
        );
        if (isset($form['metatags'][LANGUAGE_NONE][$fieldset]['#description'])) {
          $form['metatags'][LANGUAGE_NONE][$fieldset . '_description'] = array(
            '#prefix' => '<p>',
            '#markup' => $form['metatags'][LANGUAGE_NONE][$fieldset]['#description'],
            '#suffix' => '</p>',
          );
        }
        foreach ($form['metatags'][LANGUAGE_NONE][$fieldset] as $key => $value) {
          if (substr($key, 0, 1) == '#') {
            unset($form['metatags'][LANGUAGE_NONE][$fieldset][$key]);
            continue;
          }
          $form['metatags'][LANGUAGE_NONE][$key] = $value;
          unset($form['metatags'][LANGUAGE_NONE][$key]['#parents']);
          unset($form['metatags'][LANGUAGE_NONE][$fieldset][$key]);
        }
        unset($form['metatags'][LANGUAGE_NONE][$fieldset]);
      }
    }

    // Show all takens.
    $form['metatags']['tokens']['#token_types'] = 'all';
    $form['metatag_admin'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show on metatag admin page.'),
      '#weight' => -98,
      '#default_value' => isset($data['metatag_admin']) ? $data['metatag_admin'] : '',
    );

    // Add weight for current metatag_context.
    $default_weight = 0;
    if (!empty($context->reactions['metatag_context_reaction']['weight'])) {
      $default_weight = (int) $context->reactions['metatag_context_reaction']['weight'];
    }
    $form['weight'] = array(
      '#type' => 'textfield',
      '#title' => t('Weight'),
      '#size' => 2,
      '#default_value' => $default_weight,
      '#description' => t('A higher weight will make this context be executed later, overriding other context meta tags.'),
      '#weight' => -99,
    );
    return $form;
  }

  /**
   * Output a list of active contexts.
   */
  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);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
context_reaction::$description property
context_reaction::$plugin property
context_reaction::$title property
context_reaction::fetch_from_context function Retrieve options from the context provided. 1
context_reaction::get_contexts function Retrieve active contexts that have values for this reaction.
context_reaction::options_form_submit function Options form submit handler. 3
context_reaction::settings_form function Settings form. Provide variable settings for your reaction. 2
context_reaction::__clone function Clone our references when we're being cloned.
context_reaction::__construct function Constructor. Do not override.
metatag_context_reaction::execute public function Output a list of active contexts.
metatag_context_reaction::options_form public function Overrides context_reaction::options_form