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

  1. Enable the Search published contents only option in the Linkchecker
    settings.
  2. Create a node with a paragraph that contains a link. Unpublish the node.
  3. Run drush linkchecker:clear and
    drush
      linkchecker:analyze

    .

  4. Run drush linkchecker:check.
  5. 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
Command icon 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

andrerb created an issue. See original summary.

andrerb’s picture

Status: Active » Needs review
andrerb’s picture

Assigned: andrerb » Unassigned

codebymikey made their first commit to this issue’s fork.

codebymikey changed the visibility of the branch 3607125-3614972-search-published-contents to hidden.

codebymikey’s picture

Reattached 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 return FALSE rather than NULL, so made the check a bit more strict (it now checks if the return value was an entity).