Problem/Motivation
Linkchecker provides the option Search published contents only which is
intended to ignore links in unpublished content. However, this option does not work
correctly for links found inside Paragraph entities.
Paragraphs are extracted as independent entities during the analysis phase. When
getQuery() builds the list of entities to process, it correctly filters
out unpublished nodes — but it does not consider whether a paragraph's parent node
is published or not. As a result, links found inside paragraphs that belong to
unpublished nodes are still extracted and appear in the broken links report, even
when Search published contents only is enabled.
Steps to reproduce
- Enable the Search published contents only option in the Linkchecker
settings. - Create a node with a paragraph that contains a link. Unpublish the node.
- Run
drush linkchecker:clearand
drush linkchecker:analyze.
- Run
drush linkchecker:check. - The link from the paragraph inside the unpublished node still appears in the
broken links report at Reports → Broken links.
Proposed resolution
Add a new protected helper method isRootParentPublished() to
LinkExtractorService that walks up the parent chain of an entity
(via getParentEntity()) until it reaches the root entity, then checks
whether that root entity implements EntityPublishedInterface and is
published.
In extractFromEntity(), add an early return at the top: if
Search published contents only is enabled and the root parent is
unpublished, return an empty array immediately without extracting any links.
This approach works for any depth of nesting (paragraphs inside paragraphs) and
is not specific to the Paragraphs module — any entity type that implements
getParentEntity() benefits from this fix.
Remaining tasks
- Review and testing
| Comment | File | Size | Author |
|---|
Issue fork linkchecker-3607125
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
andrerb commentedComment #4
andrerb commentedComment #6
codebymikey commentedAttached static patch, as well as one that applies on top of the latest #3614972: Provide option to only check links found in default translation MR.
Comment #8
codebymikey commentedReattached the latest version of the MR.
Because the previous version throws a fatal error on Smart Date fields which also have a
getParentEntity()method, but returnFALSErather thanNULL, so made the check a bit more strict (it now checks if the return value was an entity).