Argument #2 ($sitemap) must be of type SimpleSitemapInterface

Created on 3 May 2024, 15 days ago
Updated 7 May 2024, 11 days ago

Problem/Motivation

Error when using hook:

TypeError: mymodule_simple_sitemap_links_alter(): Argument #2 ($sitemap) must be of type SimpleSitemapInterface, Drupal\simple_sitemap\Entity\SimpleSitemap given, called in /var/www/html/docroot/core/lib/Drupal/Core/Extension/ModuleHandler.php on line 562 in mymodule_simple_sitemap_links_alter() (line 94 of /var/www/html/docroot/modules/custom/mymodule/mymodule.module).

Steps to reproduce

  1. Install module.
  2. Copy hook_simple_sitemap_links_alter() code from simple_sitemap.api.php to custom module mymodule.
  3. Rename hook accordingly.
  4. Clear cache.
  5. Generate sitemap.

There is a similar error for these hooks:

  • hook_simple_sitemap_arbitrary_links_alter
  • hook_simple_sitemap_attributes_alter

But not for:

  • hook_simple_sitemap_index_attributes_alter()

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Fixed

Version

4.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium tvb

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @tvb
  • πŸ‡©πŸ‡ͺGermany gbyte Berlin

    Did you try your steps above to reproduce? Drupal\simple_sitemap\Entity\SimpleSitemap is of type SitempInterface so this error should not be a thing. Please check if you have updated the module correctly.

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Probably didnt import SimpleSitemapInterface to the file.

    In my modules, I typically reference classes in my api.php files fully, with no imports, so we dont get into support problems like this.

    e.g.

    function hook_simple_sitemap_links_alter(array &$links, SimpleSitemapInterface $sitemap) {

    to

    function hook_simple_sitemap_links_alter(array &$links, \Drupal\simple_sitemap\Entity\SimpleSitemapInterface $sitemap) {

    so things are copy pasteable.

    @gbyte Up to you, but it avoids issues like this.

  • πŸ‡¬πŸ‡§United Kingdom aaron.ferris

    aaron.ferris β†’ made their first commit to this issue’s fork.

  • πŸ‡¬πŸ‡§United Kingdom aaron.ferris

    Seems to be a mixed bag in Core, obviously maintainers prerogative but pushed a change in case we want to go with this.

  • Merge request !95Issue #3445102: use full references β†’ (Open) created by aaron.ferris
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 14 days ago
    32 pass
  • Pipeline finished with Success
    14 days ago
    Total: 333s
    #164285
  • πŸ‡§πŸ‡ͺBelgium tvb

    Probably didnt import SimpleSitemapInterface to the file.

    Exactly.

    Adding use \Drupal\simple_sitemap\Entity\SimpleSitemapInterface; to the module file fixes the issue, as do the modifications with full references (in merge request !95).

    Thank you!

  • Status changed to Fixed 11 days ago
  • πŸ‡·πŸ‡ΊRussia WalkingDexter

    No changes are needed to the module. There is a coding standard β†’ .

    API documentation (in .api.php files) should use full class names. Note that if a class is used more than once in multiple hook signatures, it must still be "use"ed, and then only the short names of the class should be used in the function.

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Well, that page is less a coding standard, just a wiki page. It was added by Crell in a 2012 revision, without any references to discussions.

    The standard is the living Coder project.

    To me, these two parts contradict eachother:

    - API documentation (in .api.php files) should use full class names.
    - Note that if a class is used more than once in multiple hook signatures, it must still be "use"ed, and then only the short names of the class should be used in the function.

    Is the second part referring to outside the .api.php file?

  • πŸ‡·πŸ‡ΊRussia WalkingDexter

    The standard is the living Coder project.

    OK, using short class names in hook signatures is not a violation according to Coder.

  • πŸ‡©πŸ‡ͺGermany gbyte Berlin
Production build 0.67.2 2024