Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement