metatag_views.module in Metatag 7
Same filename and directory in other branches
Provides native meta tag integration with Views.
File
metatag_views/metatag_views.moduleView source
<?php
/**
* @file
* Provides native meta tag integration with Views.
*/
// This is used to join the view name and the display name when building the
// context string.
define('METATAG_VIEWS_CONTEXT_SEPARATOR', '__');
/**
* Implements hook_views_api().
*/
function metatag_views_views_api() {
return array(
'api' => 3.0,
);
}
/**
* Implements hook_ctools_plugin_api().
*/
function metatag_views_ctools_plugin_api($owner, $api) {
if ($owner == 'metatag' && $api == 'metatag') {
return array(
'version' => 1,
);
}
}
/**
* Implements hook_view_preview_info_alter().
*/
function metatag_views_views_preview_info_alter(&$rows, $view) {
$metatags = $view->display_handler
->get_option('metatags');
if (!is_array($metatags) || empty($metatags)) {
return;
}
// 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,
);
}
// Set the page title to be the previewed views title before fetching meta
// tag values.
$title = drupal_set_title();
if ($view_title = $view
->get_title()) {
drupal_set_title($view_title);
}
$instance = 'view:' . $view->name;
$options['token data']['view'] = $view;
$values = metatag_metatags_values($instance, $metatags, $options);
$use_views_tokens = $view->display_handler
->get_option('metatags_tokenize');
foreach ($values as $metatag => $value) {
$metatag_info = metatag_get_info('tags', $metatag);
// If enabled, use replacement tokens from the first Views result row.
if ($value && $use_views_tokens) {
$value = $view->style_plugin
->tokenize_value($value, 0);
}
$values[$metatag] = check_plain($metatag_info['label']) . ': ' . check_plain($value);
}
if (!empty($values)) {
$rows['query'][] = array(
'<strong>' . t('Meta tags') . '</strong>',
implode('<br />', $values),
);
}
// Restore the page title.
drupal_set_title($title);
}
/**
* Implements hook_page_alter().
*/
function metatag_views_page_alter(&$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;
}
$view = views_get_page_view();
// Check if Views metatags are enabled.
if (!empty($view) && metatag_config_is_enabled('view')) {
global $language;
// The following is taken from views_get_page_view().
// If a module is still putting in the display like we used to, catch that.
if (is_subclass_of($view, 'views_plugin_display')) {
$view = $view->view;
}
// Prevent Views settings from overwriting global:frontpage.
if (drupal_is_front_page() && metatag_config_is_enabled('global:frontpage')) {
return;
}
// Include only view name by default.
$instance = 'view:' . $view->name;
// Include display name if option is overridden.
if (!$view->display_handler
->is_defaulted('metatags')) {
$instance = 'view:' . $view->name . ':' . $view->current_display;
}
// Load the meta tags for this view.
$metatags = $view->display_handler
->get_option('metatags');
// Only proceed if there's something to work with.
if (!empty($metatags) && is_array($metatags)) {
// 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 (!isset($metatags[LANGUAGE_NONE])) {
$metatags = array(
LANGUAGE_NONE => $metatags,
);
}
// Translate all of the meta tags using i18n, but don't update the
// strings.
metatag_translate_metatags($metatags[LANGUAGE_NONE], 'metatag_views:' . $view->name . METATAG_VIEWS_CONTEXT_SEPARATOR . $view->current_display, NULL, FALSE);
// If enabled, use replacement tokens from the first Views result row.
if ($view->display_handler
->get_option('metatags_tokenize')) {
// If the Views output was loaded from the Views output cache, the
// fields won't have been rendered and thus the first row tokens would
// be empty. Therefore, in this case, we manually trigger rendering of
// the first row.
_metatag_views_render_first_row($view);
foreach ($metatags as $langcode => $values) {
foreach ($values as $metatag => $config) {
if (!empty($config['value']) && is_scalar($config['value'])) {
$metatags[$langcode][$metatag]['value'] = $view->style_plugin
->tokenize_value($config['value'], 0);
}
}
}
}
// Build options for meta tag rendering.
$options = array();
$options['token data']['view'] = $view;
$options['language'] = $language->language;
// The page region can be changed.
$region = variable_get('metatag_page_region', 'content');
// Add the metatags.
$page[$region]['metatags'][$instance] = metatag_metatags_view($instance, $metatags, $options);
}
}
}
/**
* Triggers rendering of the first row of the Views result set.
*
* This is used as a helper function in metatag_views_page_alter() to have the
* necessary field tokens available.
*
* @param view $view
* The view for which result fields should be rendered.
*/
function _metatag_views_render_first_row(view $view) {
$result = $view->result;
if (!$result) {
return;
}
// We only need the first row of the results.
$result = array_intersect_key($result, array(
0 => TRUE,
));
$view->style_plugin
->render_fields($result);
}
Functions
Name | Description |
---|---|
metatag_views_ctools_plugin_api | Implements hook_ctools_plugin_api(). |
metatag_views_page_alter | Implements hook_page_alter(). |
metatag_views_views_api | Implements hook_views_api(). |
metatag_views_views_preview_info_alter | Implements hook_view_preview_info_alter(). |
_metatag_views_render_first_row | Triggers rendering of the first row of the Views result set. |
Constants
Name | Description |
---|---|
METATAG_VIEWS_CONTEXT_SEPARATOR |