Follow-up to #1868460: adding rel=prev / rel=next

In above mentioned issue the support for rel=prev and rel=next was added. I wrote a custom pager, which is somehow similar to books template_preprocess_book_navigation() pager. The pager adds rel prev and next but they won't appear in the output.

Finally I found that they where removed by metatag_html_head_alter(). Metatag should not remove them, only replace if metatags prev/next values aren't empty.

Comments

osopolar’s picture

Sorry, I am not familiar with metatag api to fix this in an proper way. Before the links are removed in metatag_html_head_alter() they may be added to the global metatag default values (if they are empty), so they can be overridden later.

To get prev and next to work now I added a workaround to my custom module to rename the elements, before metatag will remove them:


/**
 * Implements hook_module_implements_alter().
 */
function custom_module_implements_alter(&$implementations, $hook) {
  if ($hook != 'html_head_alter') {
    return;
  }
  $module = 'custom';
  $group = array($module => $implementations[$module]);
  unset($implementations[$module]);
  $implementations = $group + $implementations;
}

/**
 * Implements hook_html_head_alter().
 */
function custom_html_head_alter(&$elements) {
  // Find matching tags added via drupal_add_html_head_link().
  foreach (array_keys($elements) as $key) {
    list($prefix, $name) = explode(':', $key, 3) + array('', '');
    if ($prefix === 'drupal_add_html_head_link' && in_array($name, array('prev', 'next'))) {
      // metatag_html_head_alter() will remove prev and next links.
      // Workaround to preserve them is to rename their key, so they won't be
      // removed.
      $elements['custom_' . $key] = $elements[$key];
      unset($elements[$key]);
    }
  }
}

th3m0d’s picture

Disregard this comment. Wrong page by accident. Sorry.

damienmckenna’s picture

Title: Metatags removes rel=prev/next links » Meta tags from other modules removed even if no values provided by Metatag
Issue tags: -Novice

This isn't strictly a Novice issue..

The problem is that Metatag only checks if there could be a value, not whether there actually is a value.

damienmckenna’s picture

Dieter Geiling’s picture

Is there any progress with this issue?

nikolay shapovalov’s picture

I found https://www.drupal.org/node/2415983.
We got this in comments.

* Removes tags added by core that are now handled by metatag.

So i think metatag should take care only about core tags, and the user shoud handle other tags from modules and themes by himself.

Or maybe we can add ['#access'] to render array instead unset element, and then implement theme_html_tag() to handle new key.

nikolay shapovalov’s picture

Status: Active » Needs review
StatusFileSize
new1.12 KB

Disable access to tags instead of unset it.

Typo in this patch

+  $elements['system_meta_generator']['#access'] = false;;

use #10

hver’s picture

I have the same problem, my own "prev" and "next" tags are deleted by Metatag module.
To solve this, I use drupal_add_html_head() instead of drupal_add_html_head_link()

 drupal_add_html_head([
          '#tag'        => 'link',
          '#attributes' => [
            'href' => $prev_link,
            'rel'  => 'prev',
          ],
        ], 'pager_prev_link');
      }
sebumd’s picture

+1 to hver's solution above. My metatags are being deleted by the Metatag module.

I used the following module on github out of the box: https://github.com/nlisgo/nextprev_links but changed the drupal_add_html_head_link to the following drupal_add_html_head so that metatag doesn't intercept the metatags.

drupal_add_html_head(array(
'#tag' => 'link',
'#attributes' => array(
'href' => $head_link['href'],
'rel' => 'next',
),
), 'pager_next_link');

nikolay shapovalov’s picture

StatusFileSize
new1.12 KB

With current patch metatag module will not delete any tags anymore.
It only disable access to it.

You can now chose tags to render

/**
 * Implements hook_html_head_alter().
 */
function MYMODULE_html_head_alter(&$els) {
  $els['metatag_prev']['#access'] = false;
  $els['MYTAG_prev']['#access'] = true;
}

/**
 * Implements hook_module_implements_alter().
 *
 * Run MYMODULE_html_head_alter() after all other modules.
 */
function link_rel_module_implements_alter(&$implementations, $hook) {
  $module_name = 'MYMODULE';
  if ($hook == 'html_head_alter') {
    $group = $implementations [$module_name];
    unset($implementations [$module_name]);
    $implementations [$module_name] = $group;
  }
}
damienmckenna’s picture

StatusFileSize
new2.05 KB

Does this work? I've rewritten it to loop over the meta tags actually being output on the page, and only remove meta tags that specifically match against tags it recognizes.

Status: Needs review » Needs work

The last submitted patch, 11: metatag-n2509246-11.patch, failed testing.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new2.24 KB

Ah, should probably verify that the variables are strings before running them through strtolower() X-)

damienmckenna’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This needs tests to confirm the types of meta tags that are removed.

giupenni’s picture

Not Work, canonical and shortlink tags are not removed if value is empty

damienmckenna’s picture

@giupenni: That might be because Drupal core adds those tags too?

giupenni’s picture

Yes, I think this is possible.
How might to solve it?

damienmckenna’s picture

I think we need a checkbox to control whether those core meta tags are removed anyway.

giupenni’s picture

I agree

siramsay’s picture

I just tested this as metatag module was removing rel prev/next added by flippy and it works nicely.

However now I get generator tag that was before removed by metatag module by not having a value, checkbox control for core meta tags will fix this so sounds like a good solution.

siramsay’s picture

I added back - // Remove the default generator meta tag.
- unset($elements['system_meta_generator']);

this works for my problem.

I was thinking that maybe core meta tags could be removed by metatag module as was the case before this patch and maybe the patch should just be aimed at not removing meta tags added by other contributed modules.

If this was documented then seem like leaving the field blank in metatag module is a good solution.
Unsure which would be easier since I have only looked at the code around the lines added and removed , so just a suggestion.

damienmckenna’s picture

StatusFileSize
new2.24 KB

Just rerolling the patch, I haven't added tests or the new option yet.

damienmckenna’s picture

Lets finish this off for the next release.

damienmckenna’s picture

StatusFileSize
new9.21 KB

Please give this a test - it now removes core's meta tags by default, and adds a new option allowing them to be left in place. And it adds a test to confirm the functionality works as intended.

damienmckenna’s picture

Issue tags: -Needs tests

@size and @giupenni: Please give that patch a try, let me know if it solves the problem for you.

giupenni’s picture

Great job DamienMcKenna. Seems to work like a charm!

  • DamienMcKenna committed 6f68fdf on 7.x-1.x
    Issue #2509246 by DamienMcKenna: Remove meta tags added by core, with...
damienmckenna’s picture

Status: Needs review » Fixed

@giupenni: Thanks for the review.

Committed.

  • DamienMcKenna committed 25f5190 on 7.x-1.x
    Revert "Issue #2509246 by DamienMcKenna: Remove meta tags added by core...
siramsay’s picture

just installed 7.x-1.11+1-dev (2016-Jan-27) and works as advertised, thanks Damien

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.