Skip to content
Snippets Groups Projects
Commit a84f252c authored by Dave Reid's avatar Dave Reid
Browse files

Changed hook_metatag_info() to allow listing of both tags and tag groups. This...

Changed hook_metatag_info() to allow listing of both tags and tag groups. This allows groups to specify their own label, description, and weights.
parent 56161b47
No related branches found
Tags 8.x-1.12
No related merge requests found
......@@ -94,7 +94,14 @@ function metatag_metatag_config_instance_info() {
* Implements hook_metatag_info().
*/
function metatag_metatag_info() {
$info['description'] = array(
$info['groups']['advanced'] = array(
'label' => t('Advanced'),
'form' => array(
'#weight' => 90,
),
);
$info['tags']['description'] = array(
'label' => t('Description'),
'description' => t("A brief and concise summary of the page's content, preferrably 150 characters or less. The description meta tag may be used by search engines to display a snippet about the page in search results."),
'class' => 'DrupalTextMetaTag',
......@@ -104,19 +111,19 @@ function metatag_metatag_info() {
'#wysiwyg' => FALSE,
),
);
$info['keywords'] = array(
$info['tags']['keywords'] = array(
'label' => t('Keywords'),
'description' => t("A comma-separated list of keywords about the page. This meta tag is <em>not</em> supported by most search engines."),
'class' => 'DrupalTextMetaTag',
);
$info['title'] = array(
$info['tags']['title'] = array(
'label' => t('Title'),
'description' => t("The text to display in the title bar of a visitor's web browser when they view this page. This meta tag may also be used as the title of the page when a visitor bookmarks or favorites this page."),
'class' => 'DrupalTitleMetaTag',
);
// More advanced meta tags.
$info['robots'] = array(
$info['tags']['robots'] = array(
'label' => t('Robots'),
'description' => t("Provides search engines with specific directions for what to do when this page is indexed."),
'class' => 'DrupalListMetaTag',
......@@ -129,9 +136,9 @@ function metatag_metatag_info() {
'noodp' => t('Blocks the <a href="@odp-url">Open Directory Project</a> description of the page from being used in the description that appears below the page in the search results.', array('@odp-url' => 'http://www.dmoz.org/')),
),
),
'group' => t('Advanced'),
'group' => 'advanced',
);
$info['generator'] = array(
$info['tags']['generator'] = array(
'label' => t('Generator'),
'description' => t("Describes the name and version number of the software or publishing tool used to create the page."),
'class' => 'DrupalTextMetaTag',
......@@ -140,27 +147,27 @@ function metatag_metatag_info() {
),
'header' => 'X-Generator',
'context' => array('global'),
'group' => t('Advanced'),
'group' => 'advanced',
);
$info['copyright'] = array(
$info['tags']['copyright'] = array(
'label' => t('Copyright'),
'description' => t("Details a copyright, trademark, patent, or other information that pertains to intellectual property about this page. Note that this will not automatically protect your site's content or your intellectual property."),
'class' => 'DrupalTextMetaTag',
'group' => t('Advanced'),
'group' => 'advanced',
);
// Link tags.
$info['canonical'] = array(
$info['tags']['canonical'] = array(
'label' => t('Canonical URL'),
'description' => t("Tells search engines where the preferred location or URL is for this page to help eliminate self-created duplicate content for search engines."),
'class' => 'DrupalLinkMetaTag',
'group' => t('Advanced'),
'group' => 'advanced',
);
$info['shortlink'] = array(
$info['tags']['shortlink'] = array(
'label' => t('Shortlink URL'),
'description' => '',
'class' => 'DrupalLinkMetaTag',
'group' => t('Advanced'),
'group' => 'advanced',
);
return $info;
......
......@@ -424,8 +424,8 @@ function metatag_metatags_values($instance, array $metatags = array(), array $op
* - token types: An array of token types to be passed to theme_token_tree().
*/
function metatag_metatags_form(array &$form, $instance, array $metatags = array(), array $options = array()) {
$metatag_info = metatag_get_info();
if (empty($metatag_info)) {
$info = metatag_get_info();
if (empty($info['tags'])) {
return;
}
......@@ -466,7 +466,7 @@ function metatag_metatags_form(array &$form, $instance, array $metatags = array(
$metatags += $options['defaults'];
// Build the form for each metatag.
foreach ($metatag_info as $metatag => $metatag_info) {
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'])) {
......@@ -491,16 +491,18 @@ function metatag_metatags_form(array &$form, $instance, array $metatags = array(
}
if (!empty($metatag_info['group'])) {
$group = drupal_clean_css_identifier(drupal_strtolower($metatag_info['group']));
if (!isset($form['metatags'][$group])) {
$form['metatags'][$group] = array(
$group_key = $metatag_info['group'];
if (isset($info['groups'][$group_key]['label']) && !isset($form['metatags'][$group_key])) {
$group = $info['groups'][$group_key] + array('form' => array(), 'description' => NULL);
$form['metatags'][$group_key] = $group['form'] + array(
'#type' => 'fieldset',
'#title' => check_plain($metatag_info['group']),
'#title' => check_plain($group['label']),
'#description' => filter_xss($group['description']),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
}
$form['metatags'][$group][$metatag] = $metatag_form + array('#parents' => array('metatags', $metatag));
$form['metatags'][$group_key][$metatag] = $metatag_form + array('#parents' => array('metatags', $metatag));
}
else {
$form['metatags'][$metatag] = $metatag_form;
......@@ -511,6 +513,7 @@ function metatag_metatags_form(array &$form, $instance, array $metatags = array(
$form['metatags']['tokens'] = array(
'#theme' => 'token_tree',
'#token_types' => $options['token types'],
'#weight' => 100,
);
// Add a submit handler to compare the submitted values against the deafult
......@@ -728,56 +731,60 @@ function metatag_field_attach_form($entity_type, $entity, &$form, &$form_state,
* The meta tag name, e.g. description, for which the info shall be returned,
* or NULL to return an array with info about all meta tags.
*/
function metatag_get_info($metatag = NULL) {
global $language;
function metatag_get_info($type = NULL, $name = NULL) {
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['metatag_info'] = &drupal_static(__FUNCTION__);
}
$metatag_info = &$drupal_static_fast['metatag_info'];
$info = &$drupal_static_fast['metatag_info'];
// hook_metatag_info() includes translated strings, so each language is cached
// separately.
$cid = 'metatag:info:metatags:' . $language->language;
if (!isset($info)) {
// hook_metatag_info() includes translated strings, so each language is cached
// separately.
$cid = 'info:' . $GLOBALS['language']->language;
if (empty($metatag_info)) {
if ($cache = cache_get($cid)) {
$metatag_info = $cache->data;
if ($cache = cache_get($cid, 'cache_metatag')) {
$info = $cache->data;
}
else {
$metatag_info = module_invoke_all('metatag_info');
$info = module_invoke_all('metatag_info');
$info += array('tags' => array(), 'groups' => array());
// Merge in default values.
foreach ($metatag_info as $name => $data) {
$metatag_info[$name] += array(
foreach ($info['tags'] as $key => $data) {
$info['tags'][$key] += array(
// Merge in default values.
'name' => $name,
'name' => $key,
'class' => 'DrupalTextMetaTag',
);
}
// Let other modules alter the entity info.
drupal_alter('metatag_info', $metatag_info);
cache_set($cid, $metatag_info);
// Let other modules alter the entity info and then cache it.
drupal_alter('metatag_info', $info);
cache_set($cid, $info, 'cache_metatag');
}
}
if (empty($metatag)) {
return $metatag_info;
if (isset($type) && isset($name)) {
return isset($info[$type][$name]) ? $info[$type][$name] : FALSE;
}
elseif (isset($type)) {
return isset($info[$type]) ? $info[$type] : array();
}
elseif (isset($metatag_info[$metatag])) {
return $metatag_info[$metatag];
else {
return $info;
}
}
function metatag_get_instance($metatag, array $data = array()) {
$info = metatag_get_info($metatag);
$class = $info['class'];
if (class_exists($class)) {
$info = metatag_get_info('tags', $metatag);
if (!empty($info['class']) && class_exists($info['class'])) {
$class = $info['class'];
return new $class($info, $data);
}
else {
trigger_error("FAIL TO LOAD CLASS $class for metatag $metatag.");
trigger_error("Failed to load class $class for metatag $metatag.", E_USER_ERROR);
}
}
......@@ -828,7 +835,7 @@ function metatag_preprocess_maintenance_page(&$variables) {
*/
function metatag_html_head_alter(&$elements) {
// Remove duplicate link tags if found.
$metatags = metatag_get_info();
$metatags = metatag_get_info('tags');
foreach (array_keys($metatags) as $name) {
if (!isset($elements['metatag_' . $name]) || $elements['metatag_' . $name]['#tag'] != 'link') {
// Only check for link tags added by the metatags module.
......@@ -858,13 +865,13 @@ function metatag_config_instance_info($instance = NULL) {
$cid = 'metatag:config:instance:info:' . $language->language;
if (!isset($info)) {
if ($cache = cache_get($cid)) {
if ($cache = cache_get($cid, 'cache_metatag')) {
$info = $cache->data;
}
else {
$info = module_invoke_all('metatag_config_instance_info');
drupal_alter('metatag_config_instance_info', $info);
cache_set($cid, $info);
cache_set($cid, $info, 'cache_metatag');
}
}
......
......@@ -53,20 +53,24 @@ function metatag_opengraph_metatag_config_default_alter(array &$configs) {
* Implements hook_metatag_info().
*/
function metatag_opengraph_metatag_info() {
$info['og:title'] = array(
$info['groups']['open-graph'] = array(
'label' => t('Open Graph'),
);
$info['tags']['og:title'] = array(
'label' => t('Open Graph title'),
'description' => t('The title of your object as it should appear within the graph, e.g., <em>The Rock</em>.'),
'class' => 'DrupalTextMetaTag',
'group' => t('Open Graph'),
'group' => 'open-graph',
'element' => array(
'#theme' => 'metatag_opengraph',
),
);
$info['og:type'] = array(
$info['tags']['og:type'] = array(
'label' => t('Open Graph type'),
'description' => t('The type of your object, e.g., <em>movie</em>.'),
'class' => 'DrupalTextMetaTag',
'group' => t('Open Graph'),
'group' => 'open-graph',
'form' => array(
'#type' => 'select',
'#options' => _metatag_opengraph_type_options(),
......@@ -77,40 +81,40 @@ function metatag_opengraph_metatag_info() {
),
);
//if (module_exists('select_or_other')) {
// $info['og:type']['form']['#type'] = 'select_or_other';
// $info['tags']['og:type']['form']['#type'] = 'select_or_other';
//}
$info['og:image'] = array(
$info['tags']['og:image'] = array(
'label' => t('Open Graph image'),
'description' => t('An image URL which should represent your object within the graph. The image must be at least 50px by 50px and have a maximum aspect ratio of 3:1. We support PNG, JPEG and GIF formats.'),
'class' => 'DrupalTextMetaTag',
'group' => t('Open Graph'),
'group' => 'open-graph',
'element' => array(
'#theme' => 'metatag_opengraph',
),
);
$info['og:url'] = array(
$info['tags']['og:url'] = array(
'label' => t('Open Graph URL'),
'description' => t('The canonical URL of your object that will be used as its permanent ID in the graph, e.g., <em>http://www.imdb.com/title/tt0117500/</em>.'),
'class' => 'DrupalTextMetaTag',
'group' => t('Open Graph'),
'group' => 'open-graph',
'element' => array(
'#theme' => 'metatag_opengraph',
),
);
$info['og:site_name'] = array(
$info['tags']['og:site_name'] = array(
'label' => t('Open Graph site name'),
'description' => t('A human-readable name for your site, e.g., <em>IMDb</em>.'),
'class' => 'DrupalTextMetaTag',
'group' => t('Open Graph'),
'group' => 'open-graph',
'context' => array('global'),
'element' => array(
'#theme' => 'metatag_opengraph',
),
);
$info['og:description'] = array(
$info['tags']['og:description'] = array(
'label' => t('Open Graph description'),
'description' => t('A one to two sentence description of your page.'),
'group' => t('Open Graph'),
'group' => 'open-graph',
'class' => 'DrupalTextMetaTag',
'element' => array(
'#theme' => 'metatag_opengraph',
......
......@@ -45,7 +45,7 @@ function _metatag_ui_config_overview_indent($text, $instance) {
function metatag_ui_config_overview() {
ctools_include('export');
$metatags = metatag_get_info();
$metatags = metatag_get_info('tags');
$configs = ctools_export_crud_load_all('metatag_config');
ksort($configs);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment