Fatal Error: Call to getEntityTypeId() on null in LinkExtractorService->saveLink()

Created on 29 August 2025, 19 days ago

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. Add null checks: Verify that the entity object exists before calling methods on it
  2. Implement proper error handling: Gracefully handle cases where the entity is null
  3. Add logging: Log warnings when null entities are encountered for debugging purposes
  4. Validate entity states: Ensure entities are properly loaded and accessible before processing

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States davidburns Philadelphia

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

Comments & Activities

  • Issue created by @davidburns
  • πŸ‡ΊπŸ‡ΈUnited States davidburns Philadelphia
  • πŸ‡³πŸ‡΄Norway eiriksm Norway

    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! πŸš€

  • πŸ‡ΊπŸ‡ΈUnited States davidburns Philadelphia

    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 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.

  • πŸ‡ΊπŸ‡ΈUnited States davidburns Philadelphia
Production build 0.71.5 2024