Problem/Motivation

The linkchecker module is throwing a fatal PHP error when attempting to save links during the link extraction process. The error occurs in the LinkExtractorService::saveLink() method at line 376, where the code attempts to call getEntityTypeId() on a null object.
Error: Call to a member function getEntityTypeId() on null in Drupal\linkchecker\LinkExtractorService->saveLink() (line 376 of /var/lib/tugboat/web/modules/contrib/linkchecker/src/LinkExtractorService.php)
This error prevents the link checking functionality from working properly and can cause site crashes or incomplete link extraction processes.

Steps to reproduce

  1. Enable the linkchecker module
  2. Configure link checking for content entities
  3. Create or update content that contains links
  4. Trigger the link extraction process (either manually or via cron)
  5. Observe the fatal error in logs

Proposed resolution

The error suggests that an entity object is expected but null is being passed instead. The fix should include:

  1. Validate entity states: Ensure entities are properly loaded and accessible before processing
  2. Add null checks: Verify that the entity object exists before calling methods on it

Remaining tasks

User interface changes

API changes

Data model changes

Comments

davidburns created an issue. See original summary.

davidburns’s picture

eiriksm’s picture

Status: Active » Needs work
Issue tags: +Needs tests, +Needs issue summary update, +Needs steps to reproduce

Thanks for the patch, that looks very correct. 👌🌈

However, the steps to reproduce seem inaccurate? And the suggested fix does not align with what is the proposed solution? Based on the content of the text it seems to be generated by some LLM, so not surprised about this inaccuracy 🤓

For sure we have tests that covers exactly the steps to reproduce. So I feel pretty confident that is not the case. Which makes also the patch and issue hard to QA and verify.

In addition. Once we have actual accurate steps to reproduce it should be easy to add a test for this as well.

So the status is "needs work" based on

- needs steps to reproduce
- needs test
- needs issue summary update

Thanks for the contribution! 🚀

davidburns’s picture

Hi @eiriksm,

You are correct that I used AI to generate the ticket description.

The only piece that was really missing from the description was that linkchecker was working fine up until I ran the committed codebase through PHPCS and PHPSTAN then fixed a massive amount of coding standards in custom themes and modules. I committed the code to the git provider where all our checks and tests run. The test that failed was when it ran cron on our Tugboat environment. I was able to confirm this error when I ran cron locally with the latest database. The site that the test ran on did have A LOT of broken links across many different entity types and I didn't capture exactly where it failed.

After creating and applying this patch all tests and checks passed.

davidburns’s picture

Issue summary: View changes
enchufe’s picture

Version 2.1.0 solves the issue, but I've reworked the patch #2 to improve the code and prevent other related errors.

enchufe’s picture

Status: Needs work » Needs review