You are here

public function ProjectController::revisionOverview in Drupal PM (Project Management) 4.x

Generates an overview table of older revisions of a Project.

Parameters

\Drupal\pm_project\Entity\ProjectInterface $pm_project: A Project object.

Return value

array An array as expected by drupal_render().

File

modules/pm_project/src/Controller/ProjectController.php, line 88

Class

ProjectController
Class ProjectController.

Namespace

Drupal\pm_project\Controller

Code

public function revisionOverview(ProjectInterface $pm_project) {
  $account = $this
    ->currentUser();
  $pm_project_storage = $this
    ->entityTypeManager()
    ->getStorage('pm_project');
  $build['#title'] = $this
    ->t('Revisions for %title', [
    '%title' => $pm_project
      ->label(),
  ]);
  $header = [
    $this
      ->t('Revision'),
    $this
      ->t('Operations'),
  ];
  $revert_permission = $account
    ->hasPermission("revert all project revisions") || $account
    ->hasPermission('administer project entities');
  $delete_permission = $account
    ->hasPermission("delete all project revisions") || $account
    ->hasPermission('administer project entities');
  $rows = [];
  $vids = $pm_project_storage
    ->revisionIds($pm_project);
  $latest_revision = TRUE;
  foreach (array_reverse($vids) as $vid) {

    /** @var \Drupal\pm_project\ProjectInterface $revision */
    $revision = $pm_project_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 != $pm_project
      ->getRevisionId()) {
      $link = Link::fromTextAndUrl($date, new Url('entity.pm_project.revision', [
        'pm_project' => $pm_project
          ->id(),
        'pm_project_revision' => $vid,
      ]));
    }
    else {
      $link = $pm_project
        ->toLink()
        ->toString($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',
        ];
      }
      $latest_revision = FALSE;
    }
    else {
      $links = [];
      if ($revert_permission) {
        $links['revert'] = [
          'title' => $this
            ->t('Revert'),
          'url' => Url::fromRoute('entity.pm_project.revision_revert', [
            'pm_project' => $pm_project
              ->id(),
            'pm_project_revision' => $vid,
          ]),
        ];
      }
      if ($delete_permission) {
        $links['delete'] = [
          'title' => $this
            ->t('Delete'),
          'url' => Url::fromRoute('entity.pm_project.revision_delete', [
            'pm_project' => $pm_project
              ->id(),
            'pm_project_revision' => $vid,
          ]),
        ];
      }
      $row[] = [
        'data' => [
          '#type' => 'operations',
          '#links' => $links,
        ],
      ];
    }
    $rows[] = $row;
  }
  $build['pm_project_revisions_table'] = [
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
  ];
  return $build;
}