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
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | 3471340 after.png | 488.53 KB | csakiistvan |
| #10 | 3471340 before.png | 572.48 KB | csakiistvan |
Issue fork metatag-3471340
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
Comment #2
damienmckennaThank you for the bug report.
Have you tested to see if the problem happens with other metatag forms?
Comment #3
msypes commentedNo, I did not. I was looking to solve a particular caching issue and tripped over this. I hadn't previously used the module.
Comment #4
kasey_mk commentedCan 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.Comment #5
damienmckennaThis 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.
Comment #8
sriram_s commentedRan 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.
Comment #9
csakiistvanComment #10
csakiistvanEnvironment
Prerequisites
/admin/structure/views.Steps
MetatagDisplayExtender.php, replace the!empty($tag->value())check with a strict$tag->value() !== ''check.ddev drush cr0, click Apply, then save the view.Expected results
0after reopening the dialog.<meta http-equiv="Expires" content="0">.Actual results
Before the fix, entering
0in 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 usedempty()to filter submitted values, andempty("0")evaluates to true in PHP. After applying the fix, the value0is correctly preserved across saves and re-opens of the settings dialog.Testing produced with the assistance of an LLM.
Comment #11
damienmckennaLet'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.
Comment #12
damienmckennaComment #13
damienmckennaThe MR needs updates / fixes.
Comment #16
damienmckennaCommitted. Thank you.