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 #3094710: Distinguish autocomplete results with "Unpublished" state .
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
- Ensure you have a multilingual Drupal site set up (e.g., English and Spanish).
- Create a new content type (e.g., "Basic page") and enable translation for it.
- Create a new node in the default language (e.g., English) and set its status to unpublished. Save the node.
- Add a translation for this node (e.g., Spanish) and set its status to published. Save the translation.
- Create a new content item (e.g., another "Basic page") on the same language as the published translation.
- In the CKEditor (or any text editor integrated with Linkit), try to add a link using Linkit.
- Search for the translated node created in step 4 (e.g., search for the title of the Spanish translation).
- 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 '';
}
Issue fork linkit-3537304
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
eduardo morales albertiComment #5
liam morlandMarkup code and link to issue.
Comment #6
mark_fullmerReviewing the original issue that added the code, I concur that there really does not seem to be any reason to do that entity reload. The proposed code change makes sense, and tests pass. Merging!
Comment #7
mark_fullmer