function metatag_views_page_alter in Metatag 7
Implements hook_page_alter().
File
- metatag_views/
metatag_views.module, line 76 - Provides native meta tag integration with Views.
Code
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);
}
}
}