Linkit: EntityMatcher::buildStatus loads wrong entity language for translations

Created on 22 July 2025, 6 days ago

Problem/motivation

The \Drupal\linkit\Plugin\Linkit\Matcher\EntityMatcher::buildStatus method incorrectly loads the entity's default language when determining its publication status, even when a specific translation is passed as a parameter. This leads to an inaccurate status being displayed for translated entities, showing them as unpublished when their translation is actually published.

This issue stems from the line $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity->id()); within the buildStatus method. The entity is reloaded without considering the language of the $entity parameter that was already passed, overriding the correct translation context.

This change was introduced in the issue https://www.drupal.org/project/linkit/issues/3094710 Distinguish autocomplete results with "Unpublished" state Fixed .

The problematic code snippet is:

  /**
   * Builds the status string used in the suggestion.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   * The matched entity.
   *
   * @return string
   * The status for this entity.
   */
  protected function buildStatus(EntityInterface $entity) {
    $entity_type = $entity->getEntityTypeId();
    if ($entity->getEntityType()->hasKey('status')) {
      $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity->id());
      return $entity->isPublished() ? 'published' : 'unpublished';
    }
    return '';
  }

Steps to reproduce

  1. Ensure you have a multilingual Drupal site set up (e.g., English and Spanish).
  2. Create a new content type (e.g., "Basic page") and enable translation for it.
  3. Create a new node in the default language (e.g., English) and set its status to unpublished. Save the node.
  4. Add a translation for this node (e.g., Spanish) and set its status to published. Save the translation.
  5. Create a new content item (e.g., another "Basic page") on the same language as the published translation.
  6. In the CKEditor (or any text editor integrated with Linkit), try to add a link using Linkit.
  7. Search for the translated node created in step 4 (e.g., search for the title of the Spanish translation).
  8. Observe: The Linkit suggestion for the translated node will incorrectly appear as "unpublished", even though its translation is published. This is because buildStatus reloads the default language version, which is unpublished.

Proposed resolution

The buildStatus method should use the $entity object already passed as a parameter, as it correctly represents the entity in the desired language context. Reloading the entity via $entity->id() without specifying the language causes the issue.

The proposed change is to remove the unnecessary entity reload and directly use the provided $entity object to check its publication status.

  /**
   * Builds the status string used in the suggestion.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   * The matched entity.
   *
   * @return string
   * The status for this entity.
   */
  protected function buildStatus(EntityInterface $entity) {
    // The entity is already provided in the correct language context.
    // No need to reload it, which can lead to loading the wrong language.
    if ($entity->getEntityType()->hasKey('status')) {
      return $entity->isPublished() ? 'published' : 'unpublished';
    }
    return '';
  }

🐛 Bug report
Status

Active

Version

7.0

Component

Code

Created by

🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

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

Comments & Activities

Production build 0.71.5 2024