You are here

final class Update330 in Lightning Workflow 8.3

Contains optional updates targeting Lightning Workflow 3.3.0.

Plugin annotation

@Update("3.3.0");

Hierarchy

Expanded class hierarchy of Update330

1 file declares its use of Update330
Update330Test.php in tests/src/Kernel/Update330Test.php

File

src/Update/Update330.php, line 18

Namespace

Drupal\lightning_workflow\Update
View source
final class Update330 implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  private $entityTypeManager;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  private $moduleHandler;

  /**
   * The moderation information service.
   *
   * @var \Drupal\content_moderation\ModerationInformationInterface
   */
  private $moderationInformation;

  /**
   * Update330 constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information
   *   The moderation information service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, ModerationInformationInterface $moderation_information) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
    $this->moderationInformation = $moderation_information;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('module_handler'), $container
      ->get('content_moderation.moderation_information'));
  }

  /**
   * Fixes timestamp and author references in the Moderation History view.
   *
   * @param \Symfony\Component\Console\Style\StyleInterface $io
   *   The I/O style.
   *
   * @update
   */
  public function fixModerationHistory(StyleInterface $io) {

    // If moderation is not enabled, the moderation_state field will not exist
    // and there's nothing else to do. Or, if Views is not installed, the
    // moderation_history view definitely will not exist, and there's nothing
    // else to do.
    if (!$this
      ->isModerationEnabled() || !$this->moduleHandler
      ->moduleExists('views')) {
      return;
    }
    $view_storage = $this->entityTypeManager
      ->getStorage('view');

    /** @var \Drupal\views\Entity\View $view */
    $view = $view_storage
      ->load('moderation_history');

    // If the moderation_history view is deleted or otherwise unavailable, don't
    // even bother trying to update it.
    if (!$view) {
      return;
    }
    $question = (string) $this
      ->t('Do you want to fix the Moderation History view to prevent incorrect timestamps and authors from being displayed?');
    if (!$io
      ->confirm($question)) {
      return;
    }
    $display =& $view
      ->getDisplay('default');
    if (isset($display['display_options']['fields'])) {
      $fields =& $display['display_options']['fields'];
      $fields['revision_uid'] = $fields['uid'];
      $fields['revision_uid']['id'] = 'revision_uid';
      $fields['revision_uid']['table'] = 'node_revision';
      $fields['revision_uid']['field'] = 'revision_uid';
      $fields['revision_uid']['admin_label'] = '';
      $fields['revision_uid']['entity_field'] = 'revision_uid';
      $fields['revision_timestamp'] = $fields['created'];
      $fields['revision_timestamp']['id'] = 'revision_timestamp';
      $fields['revision_timestamp']['table'] = 'node_revision';
      $fields['revision_timestamp']['field'] = 'revision_timestamp';
      $fields['revision_timestamp']['admin_label'] = '';
      $fields['revision_timestamp']['entity_field'] = 'revision_timestamp';
      $fields['moderation_state']['alter']['text'] = 'Set to <strong>{{ moderation_state }}</strong> on {{ revision_timestamp }} by {{ revision_uid }}';
      unset($fields['uid'], $fields['created']);
    }
    if (isset($display['display_options']['relationships'])) {
      $display['display_options']['relationships']['revision_uid'] = [
        'id' => 'revision_uid',
        'table' => 'node_revision',
        'field' => 'revision_uid',
        'relationship' => 'none',
        'group_type' => 'group',
        'admin_label' => 'revision user',
        'required' => FALSE,
        'entity_type' => 'node',
        'entity_field' => 'revision_uid',
        'plugin_id' => 'standard',
      ];
    }
    $view_storage
      ->save($view);
  }

  /**
   * Checks if moderation is enabled for content items.
   *
   * @return bool
   *   TRUE if moderation is enabled for content items, FALSE otherwise.
   */
  private function isModerationEnabled() {
    $entity_type = $this->entityTypeManager
      ->getDefinition('node');
    return $this->moderationInformation
      ->isModeratedEntityType($entity_type);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Update330::$entityTypeManager private property The entity type manager.
Update330::$moderationInformation private property The moderation information service.
Update330::$moduleHandler private property The module handler.
Update330::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
Update330::fixModerationHistory public function Fixes timestamp and author references in the Moderation History view.
Update330::isModerationEnabled private function Checks if moderation is enabled for content items.
Update330::__construct public function Update330 constructor.