Problem/Motivation

Metatags uses inheritance to, for example, set global defaults > node defaults > node type values.
This is very flexible, helpful and also allows overriding inherited values with other strings or tokens.

But one thing you currently can't do (or it's not well documented in the UI) is to override an inherited value with NULL. This is for example a problem if "canonical" is set to [current-page:url:absolute] which means also on NOT FOUND (403) pages the canonical is being output, which is incorrect (at least SEO crawlers complain).
This concrete issue was also pointed out here: #3247275: Remove canonical for 404 and 403 pages

But also in other cases, you may want to REMOVE / UNSET an inherited value.

Steps to reproduce

See above.

Proposed resolution

Define a fixed (or configurable with default) value which is a placeholder for UNSET.

My suggestion would be to use <none> as Drupal uses this in other places. If it's important for anyone that the string "" can be a metatag value (and it's a rare case), we could make that value also editable. But I think it can be a constant.
Another alternative would be to use a checkbox, but that would bloat configuration and would also be less intuitive.

If a metatag has a <none> value, unset it when handling the metatag and do not output the metatag.

Document the behavior in the general description or metatag field description.

A further combined improvement would be to show the inherited value in the inherited metatags, see #2936826: Inherit parent metatags as placeholder in override forms. And / or only show the <none> documentation below in these cases.
Of course should always be allowed, even if there is no inherited value and in such case also behave like the field was left empty.

Remaining tasks

  1. Discuss
  2. Create MR with implementation & documentation
  3. Review & Test
  4. Release

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#9 metatag-n3272202-9.patch1.08 KBdamienmckenna

Issue fork metatag-3272202

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Anybody created an issue. See original summary.

anybody’s picture

I'd be interested, what @DamienMcKenna thinks about this. Hopefully I didn't oversee an already existing issue or functionality for this. That would already have been helpful in Drupal 7 though :)

anybody’s picture

Indeed, I found some related or even duplicate issues, which manifests the requirement for such a solution.

damienmckenna’s picture

Status: Active » Closed (duplicate)

From a usability perspective I'd prefer something in the UI, e.g. a checkbox that lets you control whether the value is overridden. Let's pick this up in #1798984.

anybody’s picture

anybody’s picture

Thank you super-fast Damien!

anybody’s picture

@damienmckenna just came back here for the same needs. The question is, do you think it will be realistic to implement the full featured override solution within the next decade? :D (Not asking you do do so or something like that, this is really meant to read with a smile).

My thought is the following:
I think needing to clear a predefined default in an override is clearly an edge-case. Still it's an edge-case that can happen!

It already works for any kind of string, it's just not possible to unset a value at higher level, because clearing the value would bring back the defaults. And entering a space would (hopefully) just set a space for the metatag value.

So if we'd allow a special string like

  • <unset>
  • <empty>
  • <none>

for technical users, we could quite simply extend the overriding to this remaining case.
And we could still convert that into checkboxes one day, if anyone solves it.

If you should change your opinion on this, our team would be happy to implement that, as we're running into this again and again.

Implementing the full-featured checkbox solution is great, but a big bunch of work and risky to never getting done.
This might be just "good enough" and fit things like Drupals magic "code> placehoder.

damienmckenna’s picture

Version: 8.x-1.x-dev » 2.1.x-dev
Status: Closed (duplicate) » Active

Reopening this as a simpler solution that won't involve greatly expanding the DOM structure and JS logic on edit forms.

damienmckenna’s picture

Status: Active » Needs review
StatusFileSize
new1.08 KB

So something like this might work, maybe?

anybody’s picture

Hi Damien, thanks, I'll take a look asap, bookmarked this! Also asked @Grevil to take a look!

grevil made their first commit to this issue’s fork.

grevil’s picture

Assigned: Unassigned » grevil
Status: Needs review » Needs work
Issue tags: +Needs tests

Works great! Thanks for the fix @damien!

I basically copied the example by @anybody, and it works like a charm! The canonical url will get removed on 403 pages if we declare it as .

I'll add a test for this.

grevil’s picture

Assigned: grevil » Unassigned
Issue tags: -Needs tests

To be honest, I don't quite get this request and how the inheritance works for metatags?

Based on the request, it sounds like, if we would set the metatag value of a child metatag to an empty string (e.g. setting the 403 canonical_url to ""), the metatag would use the parents value (global's canonical url). But this isn't the case.

Setting an empty string instead of already does the exact same thing. This is also represented in code and the currently failing tests.

    // If there is no value, just return either an empty array or empty string.
    if (is_null($this->value) || $this->value == '') {
      return [];
    }

    // Support the "<none>" option to hide all output for this meta tag.
    if (!is_array($this->value) && $this->value == '<none>') {
      return [];
    }

And how does the whole inheritance work then?

403

Inherits meta tags from: Global

I don't think it does.

grevil’s picture

If we allowed to set an empty string, inherit the value from the parent and delete the metatag, @thomas.frobieter, and I suggest having the following changes:

  • Add the ability to provide <none> as value, to kill the metatag entirely.
  • Add the ability to provide <empty> as value, to let the metatag have an empty string.
  • Add the ability to provide to leave the input empty to use the parent value (inherited).

Note, that the inherit / empty string functionality could also be implemented via checkbox.

anybody’s picture

Thanks, I'll check that!

anybody’s picture

@Grevil: Like just tested together I can not confirm this. Having an empty value leads to inheritance (as expected)!

grevil’s picture

Status: Needs work » Needs review

All done, please review.

Yea, this whole endeavour was caused, because programmatically setting a metatag value to an empty string will kill the metatag output entirely, resulting in the same behaviour as providing <none> (which is the root cause of my confusion).

Only if we set an empty string via UI, the submit call will silently override our empty string with its parent value. That is also, why the tests failed originally. This is REALLY confusing! ESPECIALLY if there is also an empy / isNull check on the ouput handler and no way to tell, that $this->value will silently get overriden on submit...

anybody’s picture

Status: Needs review » Reviewed & tested by the community

Super nice @grevil thank you! Works fine and as expected now. Please open a follow-up to reduce the confusion you described.

Next step (UX-wise) should be #2936826: Inherit parent metatags as placeholder in override forms

grevil’s picture

damienmckenna’s picture

tostinni’s picture

What about values coming from <select> ?
We have some in the Twitter card, but mostly in Schema.org Metatag module that use a lot of them.
Is there a way to automatically add <none> to the <select> or this is due to this module ?

damienmckenna’s picture

Version: 2.1.x-dev » 2.2.x-dev
damienmckenna’s picture

tostinni: That's a great point - please add a separate issue to extend tags to provide the "none" option. Thank you.

damienmckenna changed the visibility of the branch 3272202-add-none-placeholder to hidden.

  • damienmckenna committed aa44d4ba on 2.2.x
    Issue #3272202 by anybody, damienmckenna, grevil: Add <none> placeholder...
damienmckenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks everyone, and Anybody ;-)

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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

anybody’s picture

WHAO thank you so much @damienmckenna! This is great and super helpful! :)