You are here

public function DataPolicy::revisionsOverviewPage in Data Policy 8

Generates an overview table of older revisions of a Data policy.

Return value

array An array as expected by drupal_render().

File

src/Controller/DataPolicy.php, line 200

Class

DataPolicy
Class DataPolicy.

Namespace

Drupal\data_policy\Controller

Code

public function revisionsOverviewPage($entity_id = NULL) {
  $build = [
    'data_policy_revisions_table' => [
      '#theme' => 'table',
      '#header' => [
        $this
          ->t('Revision'),
        $this
          ->t('Operations'),
      ],
      '#rows' => [],
      '#empty' => $this
        ->t('List is empty.'),
    ],
  ];
  if (!$entity_id) {
    return $build;
  }

  /** @var \Drupal\data_policy\DataPolicyStorageInterface $data_policy_storage */
  $data_policy_storage = $this
    ->entityTypeManager()
    ->getStorage('data_policy');

  /** @var \Drupal\data_policy\Entity\DataPolicyInterface $data_policy */
  $data_policy = $data_policy_storage
    ->load($entity_id);
  $account = $this
    ->currentUser();
  $langcode = $data_policy
    ->language()
    ->getId();
  $languages = $data_policy
    ->getTranslationLanguages();
  $has_translations = count($languages) > 1;
  $revert_permission = $account
    ->hasPermission('revert all data policy revisions') || $account
    ->hasPermission('administer data policy entities');
  $delete_permission = $account
    ->hasPermission('delete all data policy revisions') || $account
    ->hasPermission('administer data policy entities');
  $vids = $data_policy_storage
    ->revisionIds($data_policy);
  foreach (array_reverse($vids) as $vid) {

    /** @var \Drupal\data_policy\Entity\DataPolicyInterface $revision */
    $revision = $data_policy_storage
      ->loadRevision($vid);

    // Only show revisions that are affected by the language that is being
    // displayed.
    if (!$revision
      ->hasTranslation($langcode) && $revision
      ->getTranslation($langcode)
      ->isRevisionTranslationAffected()) {
      continue;
    }
    $username = [
      '#theme' => 'username',
      '#account' => $revision
        ->getRevisionUser(),
    ];

    // Use revision link to link to revisions that are not active.
    $date = $this->dateFormatter
      ->format($revision
      ->getRevisionCreationTime(), 'short');
    $row = [];
    $column = [
      'data' => [
        '#theme' => 'data_policy_data_policy_revision',
        '#date' => $date,
        '#username' => $this->renderer
          ->renderPlain($username),
        '#current' => $revision
          ->isDefaultRevision(),
        '#message' => [
          '#markup' => Unicode::truncate($revision
            ->getRevisionLogMessage(), 80, TRUE, TRUE),
          '#allowed_tags' => Xss::getHtmlTagList(),
        ],
      ],
    ];
    $row[] = $column;
    $links = [];
    $links['view'] = [
      'title' => $this
        ->t('View'),
      'url' => Url::fromRoute('entity.data_policy.revision', [
        'data_policy' => $data_policy
          ->id(),
        'data_policy_revision' => $vid,
        'entity_id' => $entity_id,
      ]),
    ];
    if ($this
      ->revisionEditAccess($vid)
      ->isAllowed()) {
      $links['edit'] = [
        'title' => $this
          ->t('Edit'),
        'url' => Url::fromRoute('entity.data_policy.revision_edit', [
          'data_policy' => $data_policy
            ->id(),
          'data_policy_revision' => $vid,
          'entity_id' => $entity_id,
        ]),
      ];
    }
    if (!$revision
      ->isDefaultRevision()) {
      if ($revert_permission) {
        $links['revert'] = [
          'title' => $this
            ->t('Revert'),
          'url' => $has_translations ? Url::fromRoute('entity.data_policy.translation_revert', [
            'data_policy' => $data_policy
              ->id(),
            'data_policy_revision' => $vid,
            'langcode' => $langcode,
          ]) : Url::fromRoute('entity.data_policy.revision_revert', [
            'data_policy' => $data_policy
              ->id(),
            'data_policy_revision' => $vid,
            'entity_id' => $entity_id,
          ]),
        ];
      }
      if ($delete_permission) {
        $links['delete'] = [
          'title' => $this
            ->t('Delete'),
          'url' => Url::fromRoute('entity.data_policy.revision_delete', [
            'data_policy' => $data_policy
              ->id(),
            'data_policy_revision' => $vid,
            'entity_id' => $entity_id,
          ]),
        ];
      }
    }
    $row[] = [
      'data' => [
        '#type' => 'operations',
        '#links' => $links,
      ],
    ];
    if ($revision
      ->isDefaultRevision()) {
      foreach ($row as &$current) {
        $current['class'] = [
          'revision-current',
        ];
      }
    }
    $build['data_policy_revisions_table']['#rows'][] = $row;
  }
  return $build;
}