9

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?

1
  • 1
    The Views integration is covered by this issue. It is not finished yet, but you might attempt to apply the patch and see if it works. Commented Dec 30, 2016 at 17:51

5 Answers 5

3

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.

15

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). metatag: views

3
  • 2
    Should be the accepted answer now. Commented Mar 16, 2018 at 16:22
  • 1
    Does it apply in drupal 8? Commented May 17, 2018 at 16:24
  • 1
    Yes, of course. There's the question's tag too. Commented May 24, 2018 at 17:19
3

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');
    }
}
2

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.

3
  • 2
    That is a creative workaround. Nice find! Commented Jan 13, 2017 at 19:47
  • 2
    @JoshCoast the Metatag Views module is now available as nessunluogo suggested Commented Mar 20, 2018 at 21:03
  • 3
    Yeah Drupal is more flexible than a condom. :-D Commented May 17, 2018 at 16:25
2
  1. Install the Metatag module
  2. Enable the display extender view from /admin/structure/views/settings/advanced
  3. Install the Metatag: Views module
  4. Clear the cache and visit the views page
  5. You will see the Meta tags options after the Language ones screenshot
  6. Either you leave the Using defaults option selected or unselect it and change the page meta title
  7. The title you enter will be used for the default language content
  8. For other languages, you need to translate a page of same view
  • Translate the view
  • Edit the translated view
  • Search Page title under Page display settings enter image description here
  • Change the title
  • Save the view

In case you do not see any change, clear the cache.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.