Last active
February 11, 2016 21:35
-
-
Save delta-9/4340b39fdc47665024e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Contains views_head_metadata.module. | |
*/ | |
/** | |
* Implements hook_views_pre_render(). | |
*/ | |
function views_head_metadata_views_pre_render(\Drupal\views\ViewExecutable $view) { | |
// Get the current display. | |
$display = $view->getDisplay(); | |
// And get the list of extenders for this display. | |
$extenders = $display->getExtenders(); | |
if (!isset($extenders['head_metadata'])) { | |
// If the id of our plugin is not in the list something is wrong. | |
return; | |
} | |
// Retrieve the settings of our plugins using our custom plugin method. | |
$metadata_values = $extenders['head_metadata']->getMetadataValues(); | |
// Add the metadata tag for the title value. | |
if (!empty($metadata_values['title'])) { | |
// Generate a tag name unique for this display. | |
$tag_name = 'views:' . $view->id() . ':' . $view->current_display . ':title'; | |
$content = $metadata_values['title']; | |
$tag = [ | |
'#tag' => 'meta', | |
'#attributes' => [ | |
'name' => $tag_name, | |
'content' => $content | |
], | |
]; | |
$view->element['#attached']['html_head'][] = [$tag, $tag_name]; | |
} | |
// Add the metadata tag for the description value. | |
if (!empty($metadata_values['description'])) { | |
// Generate a tag name unique for this display. | |
$tag_name = 'views:' . $view->id() . ':' . $view->current_display . ':description'; | |
$content = $metadata_values['description']; | |
$tag = [ | |
'#tag' => 'meta', | |
'#attributes' => [ | |
'name' => $tag_name, | |
'content' => $content | |
], | |
]; | |
$view->element['#attached']['html_head'][] = [$tag, $tag_name]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment