Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. <?php
  2. /**
  3. * @file
  4. * Contains views_head_metadata.module.
  5. */
  6.  
  7. /**
  8. * Implements hook_views_pre_render().
  9. */
  10. function views_head_metadata_views_pre_render(\Drupal\views\ViewExecutable $view) {
  11. // Get the current display.
  12. $display = $view->getDisplay();
  13.  
  14. // And get the list of extenders for this display.
  15. $extenders = $display->getExtenders();
  16. if (!isset($extenders['head_metadata'])) {
  17. // If the id of our plugin is not in the list something is wrong.
  18. return;
  19. }
  20.  
  21. // Retrieve the settings of our plugins using our custom plugin method.
  22. $metadata_values = $extenders['head_metadata']->getMetadataValues();
  23.  
  24. // Add the metadata tag for the title value.
  25. if (!empty($metadata_values['title'])) {
  26. // Generate a tag name unique for this display.
  27. $tag_name = 'views:' . $view->id() . ':' . $view->current_display . ':title';
  28. $content = $metadata_values['title'];
  29. $tag = [
  30. '#tag' => 'meta',
  31. '#attributes' => [
  32. 'name' => $tag_name,
  33. 'content' => $content
  34. ],
  35. ];
  36. $view->element['#attached']['html_head'][] = [$tag, $tag_name];
  37. }
  38.  
  39. // Add the metadata tag for the description value.
  40. if (!empty($metadata_values['description'])) {
  41. // Generate a tag name unique for this display.
  42. $tag_name = 'views:' . $view->id() . ':' . $view->current_display . ':description';
  43. $content = $metadata_values['description'];
  44. $tag = [
  45. '#tag' => 'meta',
  46. '#attributes' => [
  47. 'name' => $tag_name,
  48. 'content' => $content
  49. ],
  50. ];
  51. $view->element['#attached']['html_head'][] = [$tag, $tag_name];
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement