- Issue created by @eduardo morales alberti
- @eduardo-morales-alberti opened merge request.
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 '';
}
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 '';
}
Active
7.0
Code