Problem/Motivation

To mitigate browser caching of a Views page, I attempted to add recommended cache control headers:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">

The problem is that the Expires input doesn't accept the number zero. After saving the View, the input reverts to empty

Steps to reproduce

Enable the Metatag Views module
Add the above values to the Advanced Metatags list on a Views Page.
Save the View.
The Expires input will have reverted to empty, and no "expires" metatag will be present in the resultant page source

Proposed resolution

Presumably, this is because zero isn't matching the requirements for an otherwise compliant date string.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#10 3471340 after.png488.53 KBcsakiistvan
#10 3471340 before.png572.48 KBcsakiistvan

Issue fork metatag-3471340

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

msypes created an issue. See original summary.

damienmckenna’s picture

Version: 8.x-1.26 » 2.0.x-dev

Thank you for the bug report.

Have you tested to see if the problem happens with other metatag forms?

msypes’s picture

No, I did not. I was looking to solve a particular caching issue and tripped over this. I hadn't previously used the module.

kasey_mk’s picture

Can confirm that the plain value '0' isn't stored on the other metatag forms either (e.g., /admin/config/search/metatag/node). Not only is the metatag <meta http-equiv="Expires" content="0"> not created on the page output, the value isn't stored in the form itself; re-opening the form reveals an empty field.

damienmckenna’s picture

This would happen because some of the logic uses empty() to check if the value is set, rather than checking to see if it's blank. So, if you look around for uses of empty() you'll probably find the source of the problem.

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

sriram_s’s picture

Status: Active » Needs review

Ran into this trying to set an Expires of 0 on a view to stop it being cached, and the value just vanishes on save. submitOptionsForm() in the display extender filters submitted tags with !empty($tag->value()), and !empty("0") is false, so a valid 0 gets dropped before it's ever stored. damienmckenna already spotted the empty() angle above.

Changed it to a strict !== '' check so 0 survives and genuinely empty tags still drop (kasey_mk's #4 has the same "empty field on reopen" repro). MR !256 has the one-line fix plus a functional test that saves 0 on the Archive view's Expires field and reopens to confirm it sticks; it fails on current code and passes with the fix. Kept it scoped to the Views integration, though the same empty() pattern turns up in a couple of other metatag forms if a follow-up would help.

The pipeline shows a batch of unrelated failures, but that's the fork runner (GitLab flags it right on the MR) not this change. Locally the metatag_views suite plus a sample of the base Kernel/Functional tests all pass with the patch applied.

csakiistvan’s picture

Assigned: Unassigned » csakiistvan
csakiistvan’s picture

Assigned: csakiistvan » Unassigned
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new572.48 KB
new488.53 KB

Environment

  • Drupal: 11.4.4
  • PHP: 8.5.5
  • Database: MariaDB 10.11.16
  • DDEV: v1.25.2
  • Metatag: 2.2.x-dev
  • Browser: Chrome

Prerequisites

  • Enable the Views UI and Metatag: Views modules:
    ddev drush en views_ui metatag_views -y
  • Enable the "Archive" view via the Views admin list at /admin/structure/views.

Steps

  1. Apply the fix from MR !256: in MetatagDisplayExtender.php, replace the !empty($tag-&gt;value()) check with a strict $tag-&gt;value() !== '' check.
  2. Rebuild caches: ddev drush cr
  3. Edit the "Archive" view and open the "Page" display's meta tags settings dialog.
  4. Set the "Expires" field to 0, click Apply, then save the view.
  5. Reopen the "Page" display's meta tags settings dialog.

Expected results

  • The Expires field still shows the value 0 after reopening the dialog.
  • The resulting page includes &lt;meta http-equiv="Expires" content="0"&gt;.

Actual results

Before the fix, entering 0 in the Expires field and saving the view caused the value to be silently dropped: reopening the meta tags settings showed an empty field, and no Expires meta tag was rendered on the page. This is because the code used empty() to filter submitted values, and empty("0") evaluates to true in PHP. After applying the fix, the value 0 is correctly preserved across saves and re-opens of the settings dialog.


Testing produced with the assistance of an LLM.

damienmckenna’s picture

Let's get this into the next release.

It also relates to #3378434: Empty values not removed from multi-value meta tags, so I'll need to review them together.

damienmckenna’s picture

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

Status: Reviewed & tested by the community » Needs work

The MR needs updates / fixes.

  • damienmckenna committed 76918c3e on 2.2.x
    Issue #3471340 by msypes, damienmckenna, kasey_mk, sriram_s, sriram_s:...
damienmckenna’s picture

Status: Needs work » Fixed

Committed. Thank you.

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

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

Maintainers, credit people who helped resolve this issue.