function metatag_metatags_form in Metatag 7
Build a FAPI array for editing meta tags.
Parameters
array $form: The current FAPI array.
string $instance: The configuration instance key of the metatags to use, e.g. "node:article".
array $metatags: An array of metatag data.
array $options: (optional) An array of options including the following keys and values:
- token types: An array of token types to be passed to theme_token_tree().
7 calls to metatag_metatags_form()
- metatag_config_edit_form in ./
metatag.admin.inc - Form API callback for editing a default configuration.
- metatag_context_config_edit_form in metatag_context/
metatag_context.admin.inc - FormAPI callback to build the edit form.
- metatag_context_reaction::options_form in metatag_context/
metatag_context.context.inc - metatag_form_alter in ./
metatag.module - Implements hook_form_alter().
- metatag_modify_metatags_action_form in ./
metatag.module - The Views bulk operation configuration form for modifying metatags.
File
- ./
metatag.module, line 1491 - Primary hook implementations for Metatag.
Code
function metatag_metatags_form(array &$form, $instance, array $metatags = array(), array $options = array()) {
$info = metatag_get_info();
if (empty($info['tags'])) {
return;
}
// Work out the language code to use, default to NONE.
$langcode = LANGUAGE_NONE;
if (!empty($form['#entity_type'])) {
if (!empty($form['#entity'])) {
$langcode = metatag_entity_get_language($form['#entity_type'], $form['#entity']);
}
else {
$entity_info = entity_get_info($form['#entity_type']);
if (!empty($entity_info['token type'])) {
$entity_key = '#' . $entity_info['token type'];
if (!empty($form[$entity_key])) {
$langcode = metatag_entity_get_language($form['#entity_type'], $form[$entity_key]);
}
}
}
}
// Merge in the default options.
$options += array(
'token types' => array(),
'defaults' => metatag_config_load_with_defaults($instance),
'instance' => $instance,
);
// Trigger hook_metatag_token_types_alter().
// Allow the defined tokens to be modified.
drupal_alter('metatag_token_types', $options);
$form['metatags'] = array(
'#type' => 'fieldset',
'#title' => t('Meta tags'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#multilingual' => TRUE,
'#tree' => TRUE,
'#access' => user_access('edit meta tags') || user_access('administer meta tags'),
'#weight' => 40,
'#language' => $langcode,
'#attributes' => array(
'class' => array(
'metatags-form',
),
),
);
$form['metatags'][$langcode] = array(
'#metatag_defaults' => $options['defaults'],
'#type' => 'container',
'#multilingual' => TRUE,
'#tree' => TRUE,
);
// Show a different intro message for entity pages vs config pages.
if (isset($form['#entity'])) {
$form['metatags']['intro_text'] = array(
'#markup' => '<p>' . t('Configure the meta tags below. Tokens, e.g. "[node:summary]", automatically insert the corresponding information from that field or value, which helps to avoid redundant meta data and possible search engine penalization; see the "Browse available tokens" popup for more details.') . '</p>',
'#weight' => -10,
);
}
else {
$form['metatags']['intro_text'] = array(
'#markup' => '<p>' . t('Configure the meta tags below. Use tokens (see the "Browse available tokens" popup) to avoid redundant meta data and search engine penalization. For example, a \'keyword\' value of "example" will be shown on all content using this configuration, whereas using the [node:field_keywords] automatically inserts the "keywords" values from the current entity (node, term, etc).') . '</p>',
'#weight' => -10,
);
}
// Only support vertical tabs if there is a vertical tab element.
foreach (element_children($form) as $key) {
if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'vertical_tabs') {
$form['metatags']['#group'] = $key;
$form['metatags']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'metatag') . '/metatag.vertical-tabs.js';
break;
}
}
// Merge in the default meta tag configurations.
$metatags += $options['defaults'];
// This will be used later.
$group_metatag_access = array();
// Build the form for each metatag.
foreach ($info['tags'] as $metatag => $metatag_info) {
// @todo Replace context matching with hook_metatag_access().
if (isset($options['context']) && isset($metatag_info['context'])) {
if (!in_array($options['context'], $metatag_info['context'])) {
continue;
}
}
$metatag_instance = metatag_get_instance($metatag, isset($metatags[$metatag]) ? $metatags[$metatag] : array());
if (empty($metatag_instance)) {
continue;
}
// Get the form element from the meta tag class.
$metatag_form = $metatag_instance
->getForm($options);
if (isset($metatag_form['value']['#default_value']) && is_array($metatag_form['value']['#default_value'])) {
$metatag_form['value']['#default_value'] = array_filter($metatag_form['value']['#default_value']);
}
// Add a default value form element.
if (isset($options['defaults'][$metatag]['value'])) {
if (is_array($options['defaults'][$metatag]['value'])) {
$options['defaults'][$metatag]['value'] = array_filter($options['defaults'][$metatag]['value']);
}
$metatag_form['default'] = array(
'#type' => 'hidden',
'#value' => $options['defaults'][$metatag]['value'],
);
}
// Optional extended edit permissions.
if (variable_get('metatag_extended_permissions', FALSE)) {
$metatag_form['#access'] = user_access('edit meta tag: ' . $metatag) || user_access('administer meta tags');
}
else {
$metatag_form['#access'] = $form['metatags']['#access'];
}
if (!empty($metatag_info['group'])) {
$group_key = $metatag_info['group'];
if (isset($info['groups'][$group_key]['label']) && !isset($form['metatags'][$langcode][$group_key])) {
$group = $info['groups'][$group_key] + array(
'form' => array(),
'description' => NULL,
);
$form['metatags'][$langcode][$group_key] = $group['form'] + array(
'#type' => 'fieldset',
'#title' => $group['label'],
'#description' => !empty($group['description']) ? $group['description'] : '',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
}
$form['metatags'][$langcode][$group_key][$metatag] = $metatag_form + array(
'#parents' => array(
'metatags',
$langcode,
$metatag,
),
);
// Hide the fieldset itself if there is not at least one of the meta tag
// fields visible.
if (variable_get('metatag_extended_permissions', FALSE)) {
$form['metatags'][$langcode][$group_key]['#access'] = count(element_get_visible_children($form['metatags'][$langcode][$group_key])) > 0;
}
else {
$form['metatags'][$langcode][$group_key]['#access'] = $form['metatags']['#access'];
}
// Structure the access parameter into this array, and make use of it
// later when we move on. Besides, this foreach is getting heavy.
$group_metatag_access[$group_key] = $form['metatags'][$langcode][$group_key]['#access'];
}
else {
$form['metatags'][$langcode][$metatag] = $metatag_form;
}
}
// Hide the fieldset itself if there is not at least one of the meta tag
// fields visible; only bother checking this if the user had edit access in
// the first place.
if ($form['metatags']['#access'] && variable_get('metatag_extended_permissions', FALSE)) {
$form['metatags']['#access'] = count(element_get_visible_children($form['metatags'][$langcode])) > 0;
}
// Check the #access of each group. If it passed, we display options for
// tokens. By this we update the #description of each group.
if ($form['metatags']['#access']) {
// Check if each meta tag group is being displayed.
if (!empty($group_metatag_access)) {
// Built the token browser link. For value "all" theme_token_tree()
// compares with string, not array.
if (in_array('all', $options['token types'])) {
$options['token types'] = 'all';
}
$token_listing_link = theme('token_tree', array(
'token_types' => $options['token types'],
'dialog' => TRUE,
));
foreach ($group_metatag_access as $group_key => $token_access) {
if ($token_access) {
// Update the description.
if (isset($form['metatags'][$langcode][$group_key]['#description'])) {
$form['metatags'][$langcode][$group_key]['#description'] .= '<br />';
}
else {
$form['metatags'][$langcode][$group_key]['#description'] = '';
}
// Add the token browser popup link.
$form['metatags'][$langcode][$group_key]['#description'] .= $token_listing_link;
}
}
}
}
// Add a submit handler to compare the submitted values against the default
// values.
$form += array(
'#submit' => array(),
);
if (module_exists('commerce') && isset($form['#entity_type']) && $form['#entity_type'] == 'commerce_product') {
$form['actions']['submit']['#submit'][] = 'metatag_commerce_product_form_submit';
}
else {
array_unshift($form['#submit'], 'metatag_metatags_form_submit');
}
}