metatag_panels.module in Metatag 7
Main file for metatag_panels module.
File
metatag_panels/metatag_panels.moduleView source
<?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']['metatags']) ? 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;
$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']);
}
/**
* 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;
}
}
Functions
Name | Description |
---|---|
metatag_panels_ctools_render_alter | Implements hook_ctools_render_alter(). |
metatag_panels_form | Metatag panels configuration form. |
metatag_panels_form_submit | Submission handler for Metatag panels configuration form. |
metatag_panels_page_build | Implements hook_page_build(). |
metatag_panels_page_manager_variant_operations_alter | Implements hook_page_manager_variant_operations_alter(). |