There doesn't seem to be an answer for Drupal 8 yet. I have the metatag module installed, it's a great module, but doesn't seem to address views at all. This post for Drupal 7 says that it comes with a 'metatag views' module, but, this doesn't seem to be true for Drupal 8. I've also tried to install metatag quick, but, that fails to do anything at all. So I'm stuck. Is there a way to do this programmatically in D8? How can I get this done?
5 Answers
I actually believe my comment provides an answer to this question; it cannot be done yet...
But looking at the issue, it seems to be pretty active at the moment and it is planned for the first release candidate. There is a patch available and cgmonroe claimed it worked in his beta 11 version of the module.
So you can wait until RC1 is released, which will very likely contain the Views integration, or you can try to apply the patch.
Current version of Metatag module includes a submodule "Metatag: Views".
Enabling this, you get a new section in your views editor to set all metatags available (specific tags are available in other submodules in a very drupal way).
-
2
-
1
-
1
-
1It works for me in Drupal 8. After enabling
Metatag: Views
module (This module is come with metatag module) the above option is displaying in views. From that I set the title of the page.– user50292Jun 6, 2018 at 9:56
While we wait for the metatag module to catch up, I'm using something like this in my theme's .theme
file:
<?php
function THEMENAME_preprocess_html(&$variables) {
// Set up the tag structure that's always the same no matter what page you're on
$tag = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'description',
'content' => FALSE,
),
);
// Switch on the current page path. Apparently Drupal 8 paths have leading slashes now
switch(\Drupal::service('path.current')->getPath()) {
case '/your/view/path':
$tag['#attributes']['content'] = 'Your view’s description';
break;
case '/another/view/path':
$tag['#attributes']['content'] = 'Another view’s description';
break;
}
// Only add the tag if an option matched the current path
if($tag['#attributes']['content']) {
// 'THEMENAME_meta_description' is my best guess for a good identifier.
// Maybe there's something better?
$variables['page']['#attached']['html_head'][] = array($tag, 'THEMENAME_meta_description');
}
}
I ended up turning all my View pages into blocks and then adding the block to a basic page. From there I can edit and add meta tags at will. Not an ideal solution, but should get us by until the new module is released. Thanks @Neograph734 for shedding some light on the issue.
-
2
-
2@JoshCoast the Metatag Views module is now available as nessunluogo suggested Mar 20, 2018 at 21:03
-
3
Please do following steps:
1) Install metatag module
2) Enable it
3) Now enable "Metatag: Views" module which is the child module of Metatag
4) Now clear the cache
5) Now go to views page
6) You can see the "META TAGS" option below the "LANGUAGE"
7) Now either you left the "Using defaults" option or just click it and change the meta title of the page
8) This title will set for default language content
9) For other language content, you need to go to translate a page of same views
10) First, translate the views
11) Edit the translated view
12) Now search the "Page title" under "PAGE DISPLAY SETTINGS"
13) Change the title accordingly
14) Save it
15) See the meta title of other language page it will changed now
16) Please do cache clear if that it not reflect
-
1Thanks! When I posted this a year and a half ago, the module didn't have this feature. Looks like it does now. Thanks for the walkthrough. I bet this will help someone. Jun 24, 2018 at 21:03