Problem/Motivation
The minimum version of Drupal core supported by the Sitemap module at time-of-writing is 10.2.0.
Beginning in Drupal 10.2.0, the Drupal community is moving towards using PHP Attributes instead of Docblock Annotations for plugins, and deprecating plugins using annotations and plugin types not supporting attributes. We should follow suit
Using PHP Attributes would allow Sitemap contributors to take advantage of...
- IDE autocompletion
- Refactoring safety
- Compile-time class existence checks
... among other things. While some (paid) IDEs support Docblock Annotations in similar ways, the support for them is not as robust.
Proposed resolution
Use attributes instead of annotations for Sitemap plugin.
Remaining tasks
Write a patchReview and feedbackRTBC and feedbackCommitRelease- released in version 8.x-2.1
User interface changes
None.
API changes
Adds a \Drupal\sitemap\Attribute\Sitemap attribute.
Data model changes
None.
Issue fork sitemap-3500618
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 #3
berramou commentedTODO:
Delete the annotation class src/Annotation/Sitemap.php once annotations are no longer supported.
Add a note for developers to update their plugins after the change to use the attribute instead of the annotation.
Comment #5
mparker17@berramou, thank you for the excellent contributions! The code is clear, it makes good use of APIs, and doesn't miss anything as far as I can tell. Manual testing shows everything working as far as I can tell. I don't think this needs any new tests, because the existing tests cover the changes (and they all pass!).
I've rebased this onto 8.x-2.x.
Quick question before I merge this: I noticed that in some annotations in core (for example, in the
handlerssub-array of theConfigEntityTypeannotation on\Drupal\block\Entity\Block), references to other classes in annotations are done with the special::classconstant... should we also do that for Sitemap'sderiverannotation-keys in the following places?modules/sitemap_book/src/Plugin/Sitemap/Book.php,src/Plugin/Sitemap/Menu.php,src/Plugin/Sitemap/Vocabulary.php, andtests/modules/sitemap_custom_plugin_test/src/Plugin/Sitemap/DerivativeSitemapPlugin.php... for example, in
src/Plugin/Sitemap/Menu.php, should we modify the code as follows...?Comment #6
berramou commentedHello @mparker17, thanks for the review and the rebase.
You're absolutely right — since we're using attributes and targeting PHP 8+, it's preferable to use
::classfor:- IDE autocompletion
- Refactoring safety
- Compile-time class existence checks
Speaking of PHP versions and attributes, I think it's best to explicitly add a PHP requirement (
>=8.1) incomposer.json.As the module is compatible with
^10.2 || ^11of Drupal, and Drupal 10.2+ requires PHP 8.1+, this makes sense and keeps things clear.Comment #7
mparker17Makes sense: I've done that in the latest commit.
I expected the Drupal.org Composer Service (façade) a.k.a. project_composer to update
composer.jsonwith a PHP version to match what's insitemap.info.yml, but to my surprise, I couldn't find any code to do that, so I updated bothcomposer.jsonandsitemap.info.yml.If tests pass, then I'm going to merge this. Thanks!
Comment #9
mparker17Marked as Fixed.
Updated the issue summary with more information, for users of the module who are interested in the change when they find it in the release notes.
Comment #10
berramou commentedThank @mparker17 for your contributions.
Comment #12
mparker17I discovered that using the
newkeyword in the PHP annotations (i.e.:title: new TranslatableMarkup(...)) actually introduced an unstated dependency on PHP 8.1!I've created a follow-up issue, #3541553: Unstated dependency on PHP 8.1 — see that one for more details.
Comment #13
mparker17Quick update: the changes in this issue have been released in version 8.x-2.1.
Comment #14
berramou commentedthank you @mparker17 for the release!