You are here

public function HomeboxLayoutController::revisionOverview in Homebox 8

Generates an overview table of older revisions of a Homebox Layout .

Parameters

\Drupal\homebox\Entity\HomeboxLayoutInterface $homebox_layout: A Homebox Layout object.

Return value

array An array as expected by drupal_render().

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityMalformedException

File

src/Controller/HomeboxLayoutController.php, line 284

Class

HomeboxLayoutController
Class HomeboxLayoutController.

Namespace

Drupal\homebox\Controller

Code

public function revisionOverview(HomeboxLayoutInterface $homebox_layout) {
  $account = $this
    ->currentUser();
  $langcode = $homebox_layout
    ->language()
    ->getId();
  $langname = $homebox_layout
    ->language()
    ->getName();
  $languages = $homebox_layout
    ->getTranslationLanguages();
  $has_translations = count($languages) > 1;

  /* @var \Drupal\homebox\HomeboxLayoutStorageInterface $homebox_layout_storage */
  $homebox_layout_storage = $this
    ->entityTypeManager()
    ->getStorage('homebox_layout');
  $build['#title'] = $has_translations ? $this
    ->t('@langname revisions for %title', [
    '@langname' => $langname,
    '%title' => $homebox_layout
      ->label(),
  ]) : $this
    ->t('Revisions for %title', [
    '%title' => $homebox_layout
      ->label(),
  ]);
  $header = [
    $this
      ->t('Revision'),
    $this
      ->t('Operations'),
  ];
  $revert_permission = $account
    ->hasPermission('administer homebox');
  $delete_permission = $account
    ->hasPermission('administer homebox');
  $rows = [];
  $vids = $homebox_layout_storage
    ->revisionIds($homebox_layout);
  $latest_revision = TRUE;
  foreach (array_reverse($vids) as $vid) {

    /** @var \Drupal\homebox\Entity\HomeboxLayoutInterface $revision */
    $revision = $homebox_layout_storage
      ->loadRevision($vid);

    // Only show revisions that are affected by the language that is being
    // displayed.
    if ($revision
      ->hasTranslation($langcode)) {
      $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 != $homebox_layout
        ->getRevisionId()) {
        $link = $this->linkGenerator
          ->generate($date, new Url('entity.homebox_layout.revision', [
          'homebox_layout' => $homebox_layout
            ->id(),
          'homebox_layout_revision' => $vid,
        ]));
      }
      else {
        $link = $homebox_layout
          ->toLink($date)
          ->toString();
      }
      $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' => $has_translations ? Url::fromRoute('entity.homebox_layout.translation_revert', [
              'homebox_layout' => $homebox_layout
                ->id(),
              'homebox_layout_revision' => $vid,
              'langcode' => $langcode,
            ]) : Url::fromRoute('entity.homebox_layout.revision_revert', [
              'homebox_layout' => $homebox_layout
                ->id(),
              'homebox_layout_revision' => $vid,
            ]),
          ];
        }
        if ($delete_permission) {
          $links['delete'] = [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('entity.homebox_layout.revision_delete', [
              'homebox_layout' => $homebox_layout
                ->id(),
              'homebox_layout_revision' => $vid,
            ]),
          ];
        }
        $row[] = [
          'data' => [
            '#type' => 'operations',
            '#links' => $links,
          ],
        ];
      }
      $rows[] = $row;
    }
  }
  $build['homebox_layout_revisions_table'] = [
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
  ];
  return $build;
}