5 * Contains hook implementations for the metatag_views module.
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\views\ViewEntityInterface;
10 use Drupal\views\ViewExecutable;
11 use Drupal\views\Views;
14 * Gets the meta tags of a specific view, if set.
17 * The view id, view config entity or view executable.
18 * @param string $display_id
19 * The display id. If empty uses the preselected display if $view is a
20 * ViewExecutable, otherwise the default display.
23 * The meta tags if set, null otherwise.
25 function metatag_get_view_tags($view, $display_id = NULL) {
29 if ($view instanceof ViewEntityInterface) {
30 $view = $view->getExecutable();
32 elseif (is_string($view)) {
33 $view = Views::getView($view);
35 if (!$view instanceof ViewExecutable) {
38 $view->setDisplay($display_id);
40 // And get the list of extenders for this display.
41 $extenders = $view->getDisplay()->getExtenders();
42 if (!isset($extenders['metatag_display_extender'])) {
43 // If the id of the plugin is not in the list then something is wrong.
47 // Retrieve the metatag settings from the extender.
48 return $extenders['metatag_display_extender']->getMetatags();
52 * Implements hook_metatags_alter().
54 function metatag_views_metatags_alter(&$metatags, $context) {
55 if (!$context['entity'] instanceof ViewEntityInterface) {
59 $view = $context['entity']->getExecutable();
60 // If display_id is not available, will default to Master display.
61 $display_id = \Drupal::routeMatch()->getParameter('display_id');
62 if ($tags = metatag_get_view_tags($view, $display_id)) {
63 // Apply view overrides.
64 $metatags = array_merge($metatags, $tags);
69 * Implements hook_metatag_route_entity().
71 function metatag_views_metatag_route_entity(RouteMatchInterface $route_match) {
72 if ($view_id = $route_match->getParameter('view_id')) {
73 $entity = \Drupal::entityTypeManager()->getStorage('view')->load($view_id);