Problem/Motivation

The 'Page entity label' Views field conveniently checks the type of the entity containing the link and if it's a paragraph, displays the label of the paragraph's parent, and also links to it if there is a canonical link.

Unfortunately this doesn't handle nested paragraphs - e.g. a paragraph within a paragraphs field (e.g. some kind of container/layout paragraph). Patch attached continues getting paragraph parents while they're available, until the root parent is obtained (which will usually be of a type with a canonical link).

The 'Page entity link' Views field is also not functional, but requires similar logic in place to resolve the parent's canonical link.

Steps to reproduce

Attempt to make use of the Page entity label and Page entity link views plugins incorporating nested paragraphs with a module like views_data_export.

Proposed resolution

1. Ensure the Linkchecker has an API for returning the top-most parent entity (so it can be reused in other parts of the code) - see #3611596: [PP-1] Orphaned paragraphs are reported by linkchecker
2. Update the 'Page entity label' and 'Page entity link' Views field so that they use the top-most parent entity for the links.

Remaining tasks

Provide MR and translation tests

User interface changes

N/A

API changes

New LinkCheckerLinkInterface::getTopMostParentEntity() method. (see change record)

Data model changes

The link_to_entity property on the Page entity label field is now a boolean rather than an integer.

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

bgilhome created an issue. See original summary.

devkinetic’s picture

I second this patch, and implemented similar logic over on #3294846: Expose the url to the entity for Views

kle’s picture

PLEASE PLEASE
add this small change to LinkcheckerLinkPageEntityLabel.php

even in the Drupal-10 Version
########################

vistree’s picture

I agree. The problem still exists in current version. The patch seems to be clear and small. Why not commit to stable version of the module?

gwvoigt’s picture

+1 for commiting

eiriksm’s picture

Status: Needs review » Needs work
Issue tags: +Needs merge request, +Needs tests

This needs to be in a merge request, and it needs tests before being committed

Thanks everyone for working on it and testing ✌️🤓

vistree’s picture

I recognised today that the language of the parent node is not always the correct one. I have a site with DE + EN - where DE is the default language.
If the parent entity is also a paragraph, I will always get a link to the DE version of the top level node - even the link is on the EN version.

Could we use something like:

while ($linked_entity->getEntityTypeId() === 'paragraph' && $linked_entity->getParentEntity() !== NULL) {
      $parent = $linked_entity->getParentEntity();
      if ($parent instanceof TranslatableInterface && $linked_entity instanceof TranslatableInterface) {
        $parent = \Drupal::getContainer()->get('entity.repository')->getTranslationFromContext($parent, $linked_entity->language()->getId());
      }
      $linked_entity = $parent;
    }

    if ($linked_entity instanceof TranslatableInterface) {
      $langcode = $linkchecker_link->get('entity_langcode')->value;
      $linked_entity = \Drupal::getContainer()->get('entity.repository')->getTranslationFromContext($linked_entity, $langcode);
    }

    if (!empty($this->options['link_to_entity'])) {
      try {
        $url_options = [];
        if ($linked_entity instanceof TranslatableInterface) {
          $url_options['language'] = $linked_entity->language();
        }
        $this->options['alter']['url'] = $linked_entity->toUrl('canonical', $url_options);
        $this->options['alter']['make_link'] = TRUE;
      }
      catch (UndefinedLinkTemplateException $e) {
        $this->options['alter']['make_link'] = FALSE;
vistree’s picture

Please ignore my last comment. I figured out that not the translation is the problem - instead orphaned paragraphs are processed by linkchecker. I will try to find a fix for this!

claudiu.cristea’s picture

I would use the same code from LinkcheckerLinkPageEntityLink

    while ($linked_entity instanceof ParagraphInterface && $linked_entity->getParentEntity() !== NULL) {
      $linked_entity = $linked_entity->getParentEntity();
    }
saidatom’s picture

Assigned: Unassigned » saidatom
saidatom’s picture

Version: 8.x-1.x-dev » 2.1.x-dev

saidatom’s picture

Assigned: saidatom » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs merge request, -Needs tests

Created the merge request and added test coverage to verify the issue: https://git.drupalcode.org/issue/linkchecker-3135654/-/jobs/11146328

alorenc’s picture

Patch changes a single-level if check into a while loop walking up nested paragraphs via instanceof ParagraphInterface.

alorenc’s picture

Assigned: Unassigned » alorenc
alorenc’s picture

Assigned: alorenc » Unassigned
Status: Needs review » Reviewed & tested by the community

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

codebymikey’s picture

Title: Handle nested paragraphs in 'Page entity label' Views field » Handle nested paragraphs in 'Page entity label' and 'Page entity link' Views fields
Issue summary: View changes
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new18.72 KB

Increased the scope of the ticket to also address the non-functional Page entity link views fields since it requires similar logic. Also updated the tests to include basic translation support as per #3506219: Create tests for showing the translated entity link.

The test-only changes should also be failing.

NB: you need to add schema.yml to test-only extra pattern, otherwise it will fail at the config schema level