You are here

public function Update330::fixModerationHistory in Lightning Workflow 8.3

Fixes timestamp and author references in the Moderation History view.

@update

Parameters

\Symfony\Component\Console\Style\StyleInterface $io: The I/O style.

File

src/Update/Update330.php, line 78

Class

Update330
Contains optional updates targeting Lightning Workflow 3.3.0.

Namespace

Drupal\lightning_workflow\Update

Code

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);
}