Commit c42cc99e authored by Antonio Savorelli's avatar Antonio Savorelli
Browse files

Issue #3256400: Better way to load current/latest revision in admin form

parent b08da335
Loading
Loading
Loading
Loading
+10 −17
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@
namespace Drupal\lingotek_overrides\Plugin\lingotek_overrides\FormField;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\RevisionableStorageInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Plugin\Context\Context;

/**
 * Defines a Lingotek form-field plugin for the moderation state.
@@ -44,22 +44,15 @@ class ModerationState extends Status {
    $statuses = [];

    if ($entity instanceof TranslatableInterface) {
      /** @var \Drupal\Core\Entity\TranslatableRevisionableStorageInterface $storage */
      $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
      $entity_id = $entity->id();

      if ($storage instanceof RevisionableStorageInterface) {
        $languages = array_keys($this->languageManager->getLanguages());

        foreach ($languages as $language) {
          if ($revision_id = $storage->getLatestTranslationAffectedRevisionId($entity_id, $language)) {
            /** @var \Drupal\Core\Entity\TranslatableInterface $revision */
            if ($revision = $storage->loadRevision($revision_id)) {
              /** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
              $translation = $revision->getTranslation($language);
              $statuses[$this->getStatus($translation)][] = $language;
            }
          }
      $languages = $this->languageManager->getLanguages();

      foreach ($languages as $langcode => $language) {
        $language_context = Context::createFromContext($this->languageContext, $language);
        /** @var \Drupal\Core\Entity\ContentEntityInterface $active */
        $active = $this->entityRepository->getActive($entity->getEntityTypeId(), $entity->id(), [$this::CURRENT_LANGUAGE_CONTEXT_ID => $language_context]);
        // Make sure the active entity is not a language fallback.
        if ($active->language()->getId() == $langcode) {
          $statuses[$this->getStatus($active)][] = $langcode;
        }
      }
    }
+44 −4
Original line number Diff line number Diff line
@@ -7,10 +7,15 @@ use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Entity\TranslatableRevisionableStorageInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\ContextProvider\CurrentLanguageContext;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\lingotek_overrides\Plugin\lingotek_overrides\FormFieldBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
@@ -30,6 +35,18 @@ use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
 */
class Status extends FormFieldBase {

  /**
   * The ID of the current-language context.
   */
  const CURRENT_LANGUAGE_CONTEXT_ID = '@language.current_language_context:' . LanguageInterface::TYPE_CONTENT;

  /**
   * The entity.repository service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * The language_manager service.
   *
@@ -37,6 +54,13 @@ class Status extends FormFieldBase {
   */
  protected $languageManager;

  /**
   * The current-language context.
   *
   * @var \Drupal\Core\Plugin\Context\ContextInterface
   */
  protected $languageContext;

  /**
   * The module_handler service.
   *
@@ -66,6 +90,8 @@ class Status extends FormFieldBase {
   *   The database service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language_manager service.
   * @param \Drupal\Core\Language\ContextProvider\CurrentLanguageContext $language_context_provier
   *   The language.current_language_context service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module_handler service.
   * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information
@@ -77,12 +103,17 @@ class Status extends FormFieldBase {
    $plugin_definition,
    EntityTypeManagerInterface $entity_type_manager,
    Connection $connection,
    EntityRepositoryInterface $entity_repository,
    LanguageManagerInterface $language_manager,
    CurrentLanguageContext $language_context_provider,
    ModuleHandlerInterface $module_handler,
    ModerationInformationInterface $moderation_information = NULL
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $connection);
    $this->entityRepository = $entity_repository;
    $this->languageManager = $language_manager;
    $language_contexts = $language_context_provider->getAvailableContexts();
    $this->languageContext = reset($language_contexts);
    $this->moduleHandler = $module_handler;
    $this->moderationInformation = $moderation_information;
  }
@@ -104,7 +135,9 @@ class Status extends FormFieldBase {
      $plugin_definition,
      $container->get('entity_type.manager'),
      $container->get('database'),
      $container->get('entity.repository'),
      $container->get('language_manager'),
      $container->get('language.current_language_context'),
      $container->get('module_handler'),
      $moderation_information
    );
@@ -128,11 +161,18 @@ class Status extends FormFieldBase {
    $statuses = [];

    if ($entity instanceof TranslatableInterface) {
      $languages = array_keys($entity->getTranslationLanguages());
      $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());

      foreach ($languages as $language) {
        $translation = $entity->getTranslation($language);
        $statuses[$this->getStatus($translation)][] = $language;
      if ($storage instanceof TranslatableRevisionableStorageInterface) {
        $languages = $this->languageManager->getLanguages();

        foreach ($languages as $langcode => $language) {
          $language_context = Context::createFromContext($this->languageContext, $language);
          $canonical = $this->entityRepository->getCanonical($entity->getEntityTypeId(), $entity->id(), [$this::CURRENT_LANGUAGE_CONTEXT_ID => $language_context]);
          if ($canonical->language()->getId() == $langcode) {
            $statuses[$this->getStatus($canonical)][] = $langcode;
          }
        }
      }
    }