Skip to content

Commit

Permalink
Merge pull request #65 from hosef/issue-1-port-metatag-views
Browse files Browse the repository at this point in the history
Issue #1: Initial port of the Metatag Views submodule.
  • Loading branch information
jenlampton authored Dec 29, 2020
2 parents 9d2d19e + fad1b45 commit bbf7023
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion metatag_views/metatag_views.i18n.inc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function metatag_views_i18n_list_displays() {
if (!empty($display->display_options['metatags'][LANGUAGE_NONE])) {
// Need to rig i18n_string's ability to find the record.
$display->view_id = $view_id;
$displays[$display->view_id . METATAG_VIEWS_CONTEXT_SEPARATOR . $display->id] = $display;
$displays[$display->view_id . '__' . $display->id] = $display;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion metatag_views/metatag_views.inc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if (class_exists('i18n_string_object_wrapper')) {
// This will result in a context like
// 'metatag_views:view_name__page:description' for the 'description' tag
// on the 'page' display of the 'view_name' view.
return array('metatag_views', $this->object->view_id . METATAG_VIEWS_CONTEXT_SEPARATOR . $this->object->id);
return array('metatag_views', $this->object->view_id . '__' . $this->object->id);
}

}
Expand Down
5 changes: 1 addition & 4 deletions metatag_views/metatag_views.info
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
;marking as hidden until ported.
hidden = true

type = module
name = Metatag: Views
description = Provides Metatag integration within the Views interface.
package = SEO
core = 7.x
backdrop = 1.x

dependencies[] = metatag
dependencies[] = views
Expand Down
40 changes: 29 additions & 11 deletions metatag_views/metatag_views.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* 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', '__');
function metatag_views_autoload_info() {
return array(
'metatag_views_plugin_display_extender_metatags' => 'metatag_views_plugin_display_extender_metatags.inc',
);
}

/**
* Implements hook_views_api().
Expand All @@ -15,6 +17,19 @@ function metatag_views_views_api() {
return array('api' => 3.0);
}

function metatag_views_views_plugins() {
$plugins = array();
$plugins['display_extender']['metatags'] = array(
'title' => t('Meta tags'),
'help' => t('Provides meta tags for views.'),
'handler' => 'metatag_views_plugin_display_extender_metatags',
'enabled' => TRUE,
'path' => backdrop_get_path('module', 'metatag_views'),
);

return $plugins;
}

/**
* Implements hook_view_preview_info_alter().
*/
Expand All @@ -32,9 +47,9 @@ function metatag_views_views_preview_info_alter(&$rows, $view) {

// Set the page title to be the previewed views title before fetching meta
// tag values.
$title = drupal_set_title();
$title = backdrop_set_title();
if ($view_title = $view->get_title()) {
drupal_set_title($view_title);
backdrop_set_title($view_title);
}

$instance = 'view:' . $view->name;
Expand All @@ -52,13 +67,13 @@ function metatag_views_views_preview_info_alter(&$rows, $view) {
}

// Restore the page title.
drupal_set_title($title);
backdrop_set_title($title);
}

/**
* Implements hook_page_alter().
* Implements hook_preprocess_HOOK().
*/
function metatag_views_page_alter(&$page) {
function metatag_views_preprocess_page(&$variables) {
// 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)) {
Expand All @@ -78,7 +93,7 @@ function metatag_views_page_alter(&$page) {
}

// Prevent Views settings from overwriting global:frontpage.
if (drupal_is_front_page() && metatag_config_is_enabled('global:frontpage')) {
if (backdrop_is_front_page() && metatag_config_is_enabled('global:frontpage')) {
return;
}

Expand All @@ -104,15 +119,18 @@ function metatag_views_page_alter(&$page) {

// 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);
metatag_translate_metatags($metatags[LANGUAGE_NONE], 'metatag_views:' . $view->name . '__' . $view->current_display, NULL, FALSE);

// Build options for meta tag rendering.
$options = array();
$options['token data']['view'] = $view;
$options['language'] = $language->language;

// Add the metatags.
// @todo
$processed_metatags = metatag_metatags_view($instance, $metatags, $options);
foreach ($processed_metatags as $name => $data) {
backdrop_add_html_head($data, $name);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ class metatag_views_plugin_display_extender_metatags extends views_plugin_displa

// Leave some possibility for future versions to support translation.
foreach ($metatags as $langcode => $values) {
if (!empty($form['metatags'][$langcode]['#metatag_defaults'])) {
metatag_filter_values_from_defaults($form_state['values']['metatags'][$langcode], $form['metatags'][$langcode]['#metatag_defaults']);
$defaults['metatag'] = $form['options']['metatags'][$langcode]['#metatag_defaults']['config'];

if (!empty($defaults)) {
metatag_filter_values_from_defaults($metatags[$langcode], $defaults);
}
}

Expand Down
2 changes: 1 addition & 1 deletion metatag_views/tests/metatag_views.i18n.test
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MetatagViewsI18nTest extends MetatagTestHelper {
// Plan out the different translation string tests.
$string_en = 'Testing Metatag:Views.';
$string_fr = 'French page description.';
$config_name = 'metatag_views:metatag_views_test' . METATAG_VIEWS_CONTEXT_SEPARATOR . 'page:description';
$config_name = 'metatag_views:metatag_views_test__page:description';
$path = 'metatag-views-test';

// Confirm the string is present as it has been grabbed by the string-
Expand Down

0 comments on commit bbf7023

Please sign in to comment.