'); $suffix = array_fill(0, count($parents), ''); $text = implode('', $prefix) . $text . implode('', $suffix); } return $text; } function metatag_config_overview() { ctools_include('export'); $metatags = metatag_get_info('tags'); $configs = ctools_export_crud_load_all('metatag_config'); uksort($configs, '_metatag_config_instance_sort'); $rows = array(); foreach ($configs as $config) { $row = array(); // Style disabled configurations differently. if (!empty($config->disabled)) { $row['class'][] = 'disabled'; } $details = '
'; $details .= ' '; // Add indentation to the leading cell based on how many parents the config has. $details = _metatag_config_overview_indent($details, $config->instance); $row['data']['details'] = $details; $operations = array(); if (metatag_config_access('disable', $config)) { $operations['edit'] = array( 'title' => ($config->export_type & EXPORT_IN_DATABASE) ? t('Edit') : t('Override'), 'href' => 'admin/config/search/metatags/config/' . $config->instance, ); } if (metatag_config_access('enable', $config)) { $operations['enable'] = array( 'title' => t('Enable'), 'href' => 'admin/config/search/metatags/config/' . $config->instance . '/enable', 'query' => array( 'token' => drupal_get_token('enable-' . $config->instance), ) + drupal_get_destination(), ); } if (metatag_config_access('disable', $config)) { $operations['disable'] = array( 'title' => t('Disable'), 'href' => 'admin/config/search/metatags/config/' . $config->instance . '/disable', 'query' => array( 'token' => drupal_get_token('disable-' . $config->instance), ) + drupal_get_destination(), ); } if (metatag_config_access('revert', $config)) { $operations['revert'] = array( 'title' => t('Revert'), 'href' => 'admin/config/search/metatags/config/' . $config->instance . '/revert', ); } if (metatag_config_access('delete', $config)) { $operations['delete'] = array( 'title' => t('Delete'), 'href' => 'admin/config/search/metatags/config/' . $config->instance . '/delete', ); } $operations['export'] = array( 'title' => t('Export'), 'href' => 'admin/config/search/metatags/config/' . $config->instance . '/export', ); if (module_exists('i18n_string') && !variable_get('metatag_i18n_disabled', FALSE)) { $operations['translate'] = array( 'title' => t('Translate'), 'href' => 'admin/config/search/metatags/config/' . $config->instance . '/translate', ); } $row['data']['operations'] = array( 'data' => array( '#theme' => 'links', '#links' => $operations, '#attributes' => array('class' => array('links', 'inline')), ), ); $rows[$config->instance] = $row; } $build['config_table'] = array( '#theme' => 'table', '#header' => array( 'type' => t('Type'), 'operations' => t('Operations'), ), '#rows' => $rows, '#empty' => t('No meta tag defaults available yet.'), '#attributes' => array( 'class' => array('metatag-config-overview'), ), '#attached' => array( 'js' => array( drupal_get_path('module', 'metatag') . '/metatag.admin.js', ), 'css' => array( drupal_get_path('module', 'metatag') . '/metatag.admin.css', ), ), '#suffix' => '' . t('Any items marked "Unknown" are configurations in the system for entity types or bundles which have been disabled via the API or the Settings page; they will not be used.', array('@url' => url('admin/config/search/metatags/settings'))) . '
', ); return $build; } /** * Build an FAPI #options array for the instance select field. */ function _metatag_config_instance_get_available_options() { $options = array(); $instances = metatag_config_instance_info(); foreach ($instances as $instance => $instance_info) { if (metatag_config_load($instance)) { continue; } $parents = metatag_config_get_parent_instances($instance, FALSE); array_shift($parents); if (!empty($parents)) { $parent = reset($parents); $parent_label = isset($instances[$parent]['label']) ? $instances[$parent]['label'] : t('Unknown'); if (!isset($options[$parent_label])) { $options[$parent_label] = array(); if (!metatag_config_load($parent)) { $options[$parent_label][$parent] = t('All'); } } $options[$parent_label][$instance] = $instance_info['label']; unset($options[$parent]); } else { $options[$instance] = $instance_info['label']; } } return $options; } function metatag_config_add_form($form, &$form_state) { $form['instance'] = array( '#type' => 'select', '#title' => t('Type'), '#description' => t('Select the type of default meta tags you would like to add.'), '#options' => _metatag_config_instance_get_available_options(), '#required' => TRUE, ); $form['config'] = array( '#type' => 'value', '#value' => array(), ); $form['actions']['#type'] = 'actions'; $form['actions']['save'] = array( '#type' => 'submit', '#value' => t('Add and configure'), ); $form['actions']['cancel'] = array( '#type' => 'link', '#title' => t('Cancel'), '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/metatags', ); return $form; } function metatag_config_add_form_submit($form, &$form_state) { form_state_values_clean($form_state); $config = (object) $form_state['values']; metatag_config_save($config); $form_state['redirect'] = 'admin/config/search/metatags/config/' . $config->instance; } function metatag_config_edit_form($form, &$form_state, $config) { $form['cid'] = array( '#type' => 'value', '#value' => !empty($config->cid) ? $config->cid : NULL, ); $form['instance'] = array( '#type' => 'value', '#value' => $config->instance, ); $contexts = explode(':', $config->instance); $options['context'] = $contexts[0]; if ($contexts[0] != 'global') { // The context part of the instance may not map to an entity type, so allow // the token_get_entity_mapping() function to fallback to the provided type. if ($token_type = token_get_entity_mapping('entity', $contexts[0], TRUE)) { $options['token types'] = array($token_type); } else { $options['token types'] = array($contexts[0]); } // Trigger hook_metatag_token_types_alter(). // Allow the defined tokens to be modified. drupal_alter('metatag_token_types', $options); } // Ensure that this configuration is properly compared to its parent 'default' // configuration values. if (count($contexts) > 1) { // If the config is something like 'node:article' or 'taxonomy_term:tags' // then the parent default config is 'node' or 'taxonomy_term'. $default_instance = $contexts; array_pop($default_instance); $default_instance = implode(':', $default_instance); $options['defaults'] = metatag_config_load_with_defaults($default_instance); } elseif ($contexts[0] != 'global') { // If the config is something like 'node' or 'taxonomy_term' then the // parent default config is 'global'. $options['defaults'] = metatag_config_load_with_defaults('global'); } else { // If the config is 'global' than there are no parent defaults. $options['defaults'] = array(); } metatag_metatags_form($form, $config->instance, $config->config, $options); $form['metatags']['#type'] = 'container'; $form['actions']['#type'] = 'actions'; $form['actions']['save'] = array( '#type' => 'submit', '#value' => t('Save'), ); $form['actions']['cancel'] = array( '#type' => 'link', '#title' => t('Cancel'), '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/metatags', ); $form['#submit'][] = 'metatag_config_edit_form_submit'; return $form; } function metatag_config_edit_form_submit($form, &$form_state) { // Build the configuration object and save it. form_state_values_clean($form_state); $config = (object) $form_state['values']; // @todo Consider renaming the config field from 'config' to 'metatags' $config->config = $config->metatags[LANGUAGE_NONE]; unset($config->metatags); metatag_config_save($config); $label = metatag_config_instance_label($config->instance); drupal_set_message(t('The meta tag defaults for @label have been saved.', array('@label' => $label))); $form_state['redirect'] = 'admin/config/search/metatags'; } function metatag_config_enable($config) { if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], 'enable-' . $config->instance)) { return MENU_ACCESS_DENIED; } ctools_export_crud_enable('metatag_config', $config); $label = metatag_config_instance_label($config->instance); drupal_set_message(t('The meta tag defaults for @label have been enabled.', array('@label' => $label))); drupal_goto(); } function metatag_config_disable($config) { if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], 'disable-' . $config->instance)) { return MENU_ACCESS_DENIED; } ctools_export_crud_disable('metatag_config', $config); $label = metatag_config_instance_label($config->instance); drupal_set_message(t('The meta tag defaults for @label have been disabled.', array('@label' => $label))); drupal_goto(); } function metatag_config_delete_form($form, &$form_state, $config) { $form['cid'] = array('#type' => 'value', '#value' => $config->cid); $form['instance'] = array('#type' => 'value', '#value' => $config->instance); $label = metatag_config_instance_label($config->instance); $delete = metatag_config_access('delete', $config); $title = $delete ? t('Are you sure you want to delete the meta tag defaults for @label?', array('@label' => $label)) : t('Are you sure you want to revert the meta tag defaults for @label?', array('@label' => $label)); return confirm_form( $form, $title, 'admin/config/search/metatags', t('This action cannot be undone.') ); } function metatag_config_delete_form_submit($form, &$form_state) { $config = metatag_config_load($form_state['values']['instance']); metatag_config_delete($config); $label = metatag_config_instance_label($config->instance); $delete = metatag_config_access('delete', $config); $title = $delete ? t('The meta tag defaults for @label have been deleted.', array('@label' => $label)) : t('The meta tag defaults for @label have been reverted.', array('@label' => $label)); drupal_set_message($title); $form_state['redirect'] = 'admin/config/search/metatags'; } function metatag_config_export_form($config) { ctools_include('export'); return drupal_get_form('ctools_export_form', ctools_export_crud_export('metatag_config', $config), t('Export')); } /** * Form constructor to revert nodes to their default metatags. * * @see metatag_bulk_revert_form_submit() * @ingroup forms */ function metatag_bulk_revert_form() { // Get the list of entity:bundle options $options = array(); foreach (entity_get_info() as $entity_type => $entity_info) { foreach (array_keys($entity_info['bundles']) as $bundle) { if (metatag_entity_supports_metatags($entity_type, $bundle)) { $options[$entity_type . ':' . $bundle] = $entity_info['label'] . ': ' . $entity_info['bundles'][$bundle]['label']; } } } $form['update'] = array( '#type' => 'checkboxes', '#required' => TRUE, '#title' => t('Select the entities to revert'), '#options' => $options, '#default_value' => array(), '#description' => t('All meta tags will be removed for all content of the selected entities.'), ); $metatags = metatag_get_info(); $options = array(); foreach ($metatags['tags'] as $tag_name => $tag) { $options[$tag_name] = t('@group_label: @tag_label', array( '@group_label' => $metatags['groups'][$tag['group']]['label'], '@tag_label' => $tag['label'], )); } if (count($options) > 0) { $form['tags'] = array( '#type' => 'checkboxes', '#required' => FALSE, '#title' => t('Select the tags to revert'), '#description' => t('If you select any tags, only those tags will be reverted.'), '#options' => $options, ); } $languages = language_list(); $options = array( LANGUAGE_NONE => t('Language neutral'), ); foreach ($languages as $language) { $options[$language->language] = $language->name; } $form['languages'] = array( '#type' => 'checkboxes', '#required' => FALSE, '#title' => t('Select the languages to revert'), '#description' => t('If you select any languages, only tags for those languages will be reverted.'), '#options' => $options, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Revert'), ); return $form; } /** * Form submit handler for metatag reset bulk revert form. * * @see metatag_batch_revert_form() * @see metatag_bulk_revert_batch_finished() */ function metatag_bulk_revert_form_submit($form, &$form_state) { $batch = array( 'title' => t('Bulk updating metatags'), 'operations' => array(), 'finished' => 'metatag_bulk_revert_batch_finished', 'file' => drupal_get_path('module', 'metatag') . '/metatag.admin.inc', ); $tags = isset($form_state['values']['tags']) ? array_filter($form_state['values']['tags']) : array(); $languages = isset($form_state['values']['languages']) ? array_filter($form_state['values']['languages']) : array(); // Set a batch operation per entity:bundle. foreach (array_filter($form_state['values']['update']) as $option) { list($entity_type, $bundle) = explode(':', $option); $batch['operations'][] = array('metatag_bulk_revert_batch_operation', array($entity_type, $bundle, $tags, $languages)); } batch_set($batch); } /** * Batch callback: delete custom metatags for selected bundles. */ function metatag_bulk_revert_batch_operation($entity_type, $bundle, $tags, $languages, &$context) { if (!isset($context['sandbox']['current'])) { $context['sandbox']['count'] = 0; $context['sandbox']['current'] = 0; } // Query the selected entity table. $entity_info = entity_get_info($entity_type); $query = new EntityFieldQuery(); $query->entityCondition('entity_type', $entity_type) ->propertyCondition($entity_info['entity keys']['id'], $context['sandbox']['current'], '>') ->propertyOrderBy($entity_info['entity keys']['id']); if ($entity_type != 'user') { /** * Entities which do not define a bundle such as User fail returning no results. * @see https://www.drupal.org/node/1054168#comment-5266208 */ $query->entityCondition('bundle', $bundle); } // Get the total amount of entities to process. if (!isset($context['sandbox']['total'])) { $context['sandbox']['total'] = $query->count()->execute(); $query->count = FALSE; // If there are no bundles to revert, stop immediately. if (!$context['sandbox']['total']) { $context['finished'] = 1; return; } } // Process 25 entities per iteration. $query->range(0, 25); $result = $query->execute(); $entity_ids = !empty($result[$entity_type]) ? array_keys($result[$entity_type]) : array(); foreach ($entity_ids as $entity_id) { $metatags = metatag_metatags_load($entity_type, $entity_id); if (!empty($metatags)) { $reset = FALSE; if (empty($tags)) { // All tags should be reset, so we just delete any records from the db. $query = db_delete('metatag') ->condition('entity_type', $entity_type) ->condition('entity_id', $entity_id); if (!empty($languages)) { $query->condition('language', $languages, 'IN'); } $query->execute(); metatag_metatags_cache_clear($entity_type, $entity_id); $reset = TRUE; } else { // Iterate over tags and unset those, that we want to reset. $needs_reset = FALSE; foreach ($metatags as $metatags_language => $metatags_tags) { if (empty($languages) || in_array($metatags_language, $languages)) { foreach ($metatags_tags as $metatags_tag => $metatags_value) { if (in_array($metatags_tag, $tags)) { unset($metatags[$metatags_language][$metatags_tag]); $needs_reset = TRUE; } } } } // Save modified metatags. if ($needs_reset) { // We don't have a revision id, so we'll get the active one. // Unfortunately, the only way of getting the active revision ID is to // first load the entity, and then extract the ID. This is a bit // performance intensive, but it seems to be the only way of doing it. $entities = entity_load($entity_type, array($entity_id)); if (!empty($entities[$entity_id])) { // We only care about the revision_id. list(, $revision_id, ) = entity_extract_ids($entity_type, $entities[$entity_id]); } metatag_metatags_save($entity_type, $entity_id, $revision_id, $metatags, $bundle); $reset = TRUE; } } if ($reset) { $context['results'][] = t('Reverted metatags for @bundle with id @id.', array( '@bundle' => $entity_type . ': ' . $bundle, '@id' => $entity_id, )); } } } $context['sandbox']['count'] += count($entity_ids); $context['sandbox']['current'] = max($entity_ids); if ($context['sandbox']['count'] != $context['sandbox']['total']) { $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total']; } } /** * Batch finished callback. */ function metatag_bulk_revert_batch_finished($success, $results, $operations) { if ($success) { if (!count($results)) { drupal_set_message(t('No metatags were reverted.')); } else { $message = theme('item_list', array('items' => $results)); drupal_set_message($message); } } else { $error_operation = reset($operations); drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE)))); } } /** * Misc settings page. */ function metatag_admin_settings_form() { $form = array(); $form['#attached'] = array( 'js' => array( drupal_get_path('module', 'metatag') . '/metatag.admin.js', ), 'css' => array( drupal_get_path('module', 'metatag') . '/metatag.admin.css', ), ); $form['entities'] = array( '#type' => 'fieldset', '#title' => t('Master controls for all entities'), '#description' => t('By enabling and disabling items here, it is possible to control which entities (e.g. nodes, taxonomy terms) and bundles (e.g. content types, vocabularies) will have the meta tag form available on their respective edit pages. If an entity type is disabled it also disables it for all of that entity type\'s bundles.