Problem/Motivation

Using metatag 8.x-1.10 we've got some custom code that does some trickery to change the page title. But it appears that #3099168: Remove "title" meta tag, leave the regular title tag broke the assumptions that the custom code was making.

The patch attached is a failing test on 8.x-1.x but will pass on 8.x-1.10.

Proposed resolution

The custom code should either convert the hook_page_attachments_alter() implementation into a hook_metatags_attachments_alter() OR adjust the contents of drupal_static('metatag_attachments') and not try to change $attachments.

This issue now:

  • tidies up the comments added by #3099168: Remove "title" meta tag, leave the regular title tag to be a bit clearer as they seemed (to me) to imply metatag_preprocess_html() comes before metatag_page_attachments() which is not the case
  • Improves the logic in metatag_page_attachments() to do less iterating.
  • Adds an integration test for modules implementing hook_metatags_attachments_alter()

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new3.58 KB

Here's a patch which shows the problem.

alexpott’s picture

I can't run a test with DrupalCI against 8.x-1.10 but here's the output:

./vendor/bin/phpunit -v modules/metatag/tests/src/Functional/MetatagIntegrationTest.php                                   6.7s  Mon 22 Jun 17:00:32 2020
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.3.18
Configuration: /Users/alex/dev/sites/drupal8alt.dev/phpunit.xml

Testing Drupal\Tests\metatag\Functional\MetatagIntegrationTest
.                                                                   1 / 1 (100%)

Time: 5.29 seconds, Memory: 6.00MB

OK (1 test, 3 assertions)

HTML output was generated
http://drupal8alt.test/sites/simpletest/browser_output/Drupal_Tests_metatag_Functional_MetatagIntegrationTest-3-94690396.html

Remaining deprecation notices (2)

  1x: drupal_installation_attempted() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Installer\InstallerKernel::installationAttempted() instead. See https://www.drupal.org/node/3035275
    1x in MetatagIntegrationTest::testPageTitle from Drupal\Tests\metatag\Functional

  1x: drupal_http_header_attributes() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes() instead. See https://www.drupal.org/node/3000051
    1x in MetatagIntegrationTest::testPageTitle from Drupal\Tests\metatag\Functional

Status: Needs review » Needs work

The last submitted patch, 2: 3153911-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new1 KB
new3.5 KB

After spending sometime in the debugger I can see where the implementation of metatag_test_integration_page_attachments_alter() is incorrect. It's expecting $attachments['#attached']['html_head'] to still have a title attribute. Since #3099168: Remove "title" meta tag, leave the regular title tag it no longer does. So the code doesn't work as expected. If we change it as per the patch attached here - everything seems to work.

Perhaps this is better implemented as hook_metatags_attachments_alter() in the first place.

Status: Needs review » Needs work

The last submitted patch, 5: 3153911-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mrinalini9’s picture

Status: Needs work » Needs review
StatusFileSize
new4 KB
new397 bytes

Fixing test case failure issue in #5, please review.

alexpott’s picture

@mrinalini9 that fix looks important for the metatag module. This issue was created to explore modules that implement hook_page_attachments_alter() and change metatag related stuff. You could check the metatag issue queue and if there is not an issue already you could file an issue to correct #7.

alexpott’s picture

Title: Setting the title in hook_page_attachments_alter() used to work in 8.x-1.10 but now does not » Improve logic in metatag_page_attachments() and make code comments more explicit
Issue summary: View changes
StatusFileSize
new3.66 KB
new5.25 KB

This will fail tests because HEAD metatag is failing tests due to #7. But that should be fixed in another issue.

alexpott’s picture

StatusFileSize
new1.24 KB
new5.98 KB

Reading metatag_preprocess_html() again I think we need to be clearer about whose attachments we are reading from. In $variables there's $variables['#attached'] and $variables['html_attributes'] and $attributes looks like the same variable as it is in metatag_page_attachments().

I think we should copy the approach from metatag_page_attachments() and call them $metatag_attachments

The last submitted patch, 9: 3153911-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 10: 3153911-10.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

alexpott’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new5.91 KB
new1011 bytes

Changing the hook implementation to the correct hook_metatags_attachments_alter() so now the module has test coverage of this hook.

alexpott’s picture

StatusFileSize
new1.32 KB
new6.06 KB

And finally lets tidy up the test.

The last submitted patch, 13: 3153911-11.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 14: 3153911-14.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

damienmckenna’s picture

The tests have been fixed in the 8.x-1.x branch, back to fixing this.

alexpott’s picture

Status: Needs work » Needs review

This should pass now...

damienmckenna’s picture

Title: Improve logic in metatag_page_attachments() and make code comments more explicit » Improve logic in metatag_page_attachments() and make code comments more clear
Status: Needs review » Fixed
Parent issue: » #3129597: Plan for Metatag 8.x-1.14

Nice work, thank you! Committed.

Status: Fixed » Closed (fixed)

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