You are here

public function ConsentAgreementController::revisionOverview in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_consent/src/Controller/ConsentAgreementController.php \Drupal\gdpr_consent\Controller\ConsentAgreementController::revisionOverview()
  2. 3.0.x modules/gdpr_consent/src/Controller/ConsentAgreementController.php \Drupal\gdpr_consent\Controller\ConsentAgreementController::revisionOverview()

Generates an overview table of older revisions of a Consent Agreement .

Parameters

\Drupal\gdpr_consent\Entity\ConsentAgreement $gdpr_consent_agreement: A Consent Agreement object.

Return value

array An array as expected by drupal_render().

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

1 string reference to 'ConsentAgreementController::revisionOverview'
gdpr_consent.routing.yml in modules/gdpr_consent/gdpr_consent.routing.yml
modules/gdpr_consent/gdpr_consent.routing.yml

File

modules/gdpr_consent/src/Controller/ConsentAgreementController.php, line 129

Class

ConsentAgreementController
Class ConsentAgreementController.

Namespace

Drupal\gdpr_consent\Controller

Code

public function revisionOverview(ConsentAgreement $gdpr_consent_agreement) {
  $account = $this
    ->currentUser();

  /** @var \Drupal\gdpr_consent\ConsentAgreementStorageInterface $storage */
  $storage = $this->entityTypeManager
    ->getStorage('gdpr_consent_agreement');
  $build['#title'] = $this
    ->t('Revisions for %title', [
    '%title' => $gdpr_consent_agreement->title->value,
  ]);
  $header = [
    $this
      ->t('Revision'),
    $this
      ->t('Operations'),
  ];
  $revert_permission = $account
    ->hasPermission('manage gdpr agreements');
  $delete_permission = $account
    ->hasPermission('manage gdpr agreements');
  $rows = [];
  $vids = $storage
    ->revisionIds($gdpr_consent_agreement);
  $latest_revision = TRUE;
  foreach (\array_reverse($vids) as $vid) {

    /** @var \Drupal\gdpr_consent\Entity\ConsentAgreement $revision */
    $revision = $storage
      ->loadRevision($vid);
    $username = [
      '#theme' => 'username',
      '#account' => $revision
        ->getRevisionUser(),
    ];

    // Use revision link to link to revisions that are not active.
    $date = $this->dateFormatter
      ->format($revision
      ->getRevisionCreationTime(), 'short');
    if ($vid !== $gdpr_consent_agreement
      ->getRevisionId()) {
      $link = Link::fromTextAndUrl($date, new Url('entity.gdpr_consent_agreement.revision', [
        'gdpr_consent_agreement' => $gdpr_consent_agreement
          ->id(),
        'gdpr_consent_agreement_revision' => $vid,
      ]))
        ->toRenderable();
      $link = $this->renderer
        ->renderPlain($link);
    }
    else {
      $link = $gdpr_consent_agreement
        ->link($date);
    }
    $row = [];
    $column = [
      'data' => [
        '#type' => 'inline_template',
        '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
        '#context' => [
          'date' => $link,
          'username' => $this->renderer
            ->renderPlain($username),
          'message' => [
            '#markup' => $revision
              ->getRevisionLogMessage(),
            '#allowed_tags' => Xss::getHtmlTagList(),
          ],
        ],
      ],
    ];
    $row[] = $column;
    if ($latest_revision) {
      $row[] = [
        'data' => [
          '#prefix' => '<em>',
          '#markup' => $this
            ->t('Current revision'),
          '#suffix' => '</em>',
        ],
      ];
      foreach ($row as &$current) {
        $current['class'] = [
          'revision-current',
        ];
      }
      unset($current);
      $latest_revision = FALSE;
    }
    else {
      $links = [];
      if ($revert_permission) {
        $links['revert'] = [
          'title' => $this
            ->t('Revert'),
          'url' => Url::fromRoute('entity.gdpr_consent_agreement.revision_revert', [
            'gdpr_consent_agreement' => $gdpr_consent_agreement
              ->id(),
            'gdpr_consent_agreement_revision' => $vid,
          ]),
        ];
      }
      if ($delete_permission) {
        $links['delete'] = [
          'title' => $this
            ->t('Delete'),
          'url' => Url::fromRoute('entity.gdpr_consent_agreement.revision_delete', [
            'gdpr_consent_agreement' => $gdpr_consent_agreement
              ->id(),
            'gdpr_consent_agreement_revision' => $vid,
          ]),
        ];
      }
      $row[] = [
        'data' => [
          '#type' => 'operations',
          '#links' => $links,
        ],
      ];
    }
    $rows[] = $row;
  }
  $build['gdpr_consent_agreement_revisions_table'] = [
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
  ];
  return $build;
}