123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- /**
- * @file
- * Main file for metatag_panels module.
- */
- /**
- * Implements hook_page_manager_variant_operations_alter().
- */
- function metatag_panels_page_manager_variant_operations_alter(&$operations, $handler) {
- // Use this obnoxious construct to safely insert our item.
- reset($operations['children']);
- $children_operations = array();
- foreach ($operations['children'] as $key => $value) {
- $children_operations[$key] = $value;
- if ($key == 'context') {
- $children_operations['meta'] = array(
- 'title' => t('Meta tags'),
- 'description' => t('Edit variant level meta tags.'),
- 'form' => 'metatag_panels_form',
- );
- }
- }
- $operations['children'] = $children_operations;
- }
- /**
- * Metatag panels configuration form.
- */
- function metatag_panels_form($form, $form_state) {
- $handler = $form_state['handler'];
- // Load available contexts.
- ctools_include('context-task-handler');
- $contexts = ctools_context_handler_get_all_contexts($form_state['task'], $form_state['subtask'], $handler);
- // Convert contexts into keywords readable by the token engine.
- $token_types = array();
- foreach ($contexts as $context) {
- if ($context->keyword == 'taxonomy_term') {
- $token_types[] = 'term';
- }
- else {
- $token_types[] = $context->keyword;
- }
- }
- // Allow the user to enable/disable meta tags for this panel.
- $form['settings']['metatags_enabled'] = array(
- '#type' => 'checkbox',
- '#title' => t('Enable Metatag configuration.'),
- '#default_value' => isset($handler->conf['metatag_panels']['enabled']) ? $handler->conf['metatag_panels']['enabled'] : FALSE,
- );
- // Don't set any metatag instance name as the configuration data is managed
- // locally within panels.
- $instance = '';
- $options = array('token types' => $token_types);
- $metatags = empty($handler->conf['metatag_panels']) ? array() : $handler->conf['metatag_panels']['metatags'];
- // This leaves some possibility for future versions to support translation.
- if (!isset($metatags[LANGUAGE_NONE])) {
- $metatags = array(LANGUAGE_NONE => $metatags);
- }
- // Load the metatag form (passed by reference).
- metatag_metatags_form($form, $instance, $metatags[LANGUAGE_NONE], $options);
- // Add CTools substitutions list to the form.
- $rows = array();
- foreach ($contexts as $context) {
- foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
- $rows[] = array(
- check_plain($keyword),
- t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
- );
- }
- }
- if (!empty($rows)) {
- $form['contexts'] = array(
- '#title' => t('Substitutions'),
- '#type' => 'fieldset',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- );
- $header = array(t('Keyword'), t('Value'));
- $form['contexts']['context'] = array(
- '#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
- );
- $form['contexts']['#states'] = array(
- 'visible' => array(
- ':input[name="metatags_enabled"]' => array('checked' => TRUE),
- ),
- );
- }
- // Modify metatag form defaults.
- $form['metatags']['#collapsible'] = FALSE;
- $form['metatags']['#collapsed'] = FALSE;
- // Don't show the Metatag options until it's enabled.
- $form['metatags']['#states'] = array(
- 'visible' => array(
- ':input[name="metatags_enabled"]' => array('checked' => TRUE),
- ),
- );
- return $form;
- }
- /**
- * Submission handler for Metatag panels configuration form.
- */
- function metatag_panels_form_submit($form, $form_state) {
- $conf = array(
- 'enabled' => $form_state['values']['metatags_enabled'],
- 'metatags' => array(),
- );
- // Only bother saving the meta tags if they were enabled.
- if ($conf['enabled']) {
- $conf['metatags'] = $form_state['values']['metatags'][LANGUAGE_NONE];
- // Translate the meta tags.
- metatag_translations_update($conf['metatags'], 'metatag_panels:' . $form_state['handler']->name);
- }
- // Save the values for later.
- $form_state['handler']->conf['metatag_panels'] = $conf;
- }
- /**
- * Implements hook_ctools_render_alter().
- */
- 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;
- if (empty($metatags)) {
- return;
- }
- // Substitute Panels context variables.
- foreach ($metatags as $metatag => $data) {
- if (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 = array(
- 'instance' => 'panels:' . $handler->name,
- 'token data' => $tokens,
- );
- foreach ($metatags as $metatag => $data) {
- // Render CTools context substitution values prior to rendering the meta
- // tag.
- if (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']);
- }
- /**
- * Implements hook_page_build().
- *
- * @see metatag_panels_ctools_render_alter()
- */
- function metatag_panels_page_build(&$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;
- }
- $metatags = drupal_static('metatag_panels');
- if (!empty($metatags)) {
- // The page region can be changed.
- $region = variable_get('metatag_page_region', 'content');
- $page[$region]['metatags']['global'] = $metatags;
- }
- }
|