123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?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();
- while (list($key, $value) = each($operations['children'])) {
- $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);
- $conf = empty($handler->conf['metatag_panels']) ? array() : $handler->conf['metatag_panels']['metatags'];
- // Load the metatag form (passed by reference).
- metatag_metatags_form($form, $instance, $conf, $options);
- // 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' => $form_state['values']['metatags'],
- );
- $form_state['handler']->conf['metatag_panels'] = $conf;
- }
- /**
- * Implements hook_ctools_render_alter().
- */
- function metatag_panels_ctools_render_alter($info, $page, $context) {
- $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'];
- $metatags += metatag_config_load_with_defaults('');
- if (empty($metatags)) {
- return;
- }
- // 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;
- }
- // Build the Metatag.
- $options = array(
- 'instance' => 'panels:' . $handler->name,
- 'token data' => $tokens,
- );
- foreach ($metatags as $metatag => $data) {
- $metatag_instance = metatag_get_instance($metatag, $data);
- if ($metatag_instance) {
- $output[$metatag] = $metatag_instance->getElement($options);
- }
- }
- // 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) {
- $metatags = drupal_static('metatag_panels');
- if (!empty($metatags)) {
- $page['content']['metatags']['global'] = $metatags;
- }
- }
|