You are here

public function SimpleMegaMenuController::revisionOverview in Simple Mega Menu 2.0.x

Same name and namespace in other branches
  1. 8 src/Controller/SimpleMegaMenuController.php \Drupal\simple_megamenu\Controller\SimpleMegaMenuController::revisionOverview()

Generates an overview table of older revisions of a Simple mega menu .

Parameters

\Drupal\simple_megamenu\Entity\SimpleMegaMenuInterface $simple_mega_menu: A Simple mega menu object.

Return value

array An array as expected by drupal_render().

File

src/Controller/SimpleMegaMenuController.php, line 101

Class

SimpleMegaMenuController
Class SimpleMegaMenuController.

Namespace

Drupal\simple_megamenu\Controller

Code

public function revisionOverview(SimpleMegaMenuInterface $simple_mega_menu) {
  $account = $this
    ->currentUser();
  $langcode = $simple_mega_menu
    ->language()
    ->getId();
  $langname = $simple_mega_menu
    ->language()
    ->getName();
  $languages = $simple_mega_menu
    ->getTranslationLanguages();
  $has_translations = count($languages) > 1;
  $simple_mega_menu_storage = $this
    ->entityTypeManager()
    ->getStorage('simple_mega_menu');
  $build['#title'] = $has_translations ? $this
    ->t('@langname revisions for %title', [
    '@langname' => $langname,
    '%title' => $simple_mega_menu
      ->label(),
  ]) : $this
    ->t('Revisions for %title', [
    '%title' => $simple_mega_menu
      ->label(),
  ]);
  $header = [
    $this
      ->t('Revision'),
    $this
      ->t('Operations'),
  ];
  $revert_permission = $account
    ->hasPermission("revert all simple mega menu revisions") || $account
    ->hasPermission('administer simple mega menu entities');
  $delete_permission = $account
    ->hasPermission("delete all simple mega menu revisions") || $account
    ->hasPermission('administer simple mega menu entities');
  $rows = [];
  $vids = $simple_mega_menu_storage
    ->revisionIds($simple_mega_menu);
  $latest_revision = TRUE;
  foreach (array_reverse($vids) as $vid) {

    /** @var \Drupal\simple_megamenu\Entity\SimpleMegaMenuInterface $revision */
    $revision = $simple_mega_menu_storage
      ->loadRevision($vid);

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

      // Use revision link to link to revisions that are not active.
      $date = $this->dateFormatter
        ->format($revision->revision_timestamp->value, 'short');
      if ($vid != $simple_mega_menu
        ->getRevisionId()) {
        $link = Link::fromTextAndUrl($date, new Url('entity.simple_mega_menu.revision', [
          'simple_mega_menu' => $simple_mega_menu
            ->id(),
          'simple_mega_menu_revision' => $vid,
        ]))
          ->toString();
      }
      else {
        $link = $simple_mega_menu
          ->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->revision_log_message->value,
              '#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.simple_mega_menu.translation_revert', [
              'simple_mega_menu' => $simple_mega_menu
                ->id(),
              'simple_mega_menu_revision' => $vid,
              'langcode' => $langcode,
            ]) : Url::fromRoute('entity.simple_mega_menu.revision_revert', [
              'simple_mega_menu' => $simple_mega_menu
                ->id(),
              'simple_mega_menu_revision' => $vid,
            ]),
          ];
        }
        if ($delete_permission) {
          $links['delete'] = [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('entity.simple_mega_menu.revision_delete', [
              'simple_mega_menu' => $simple_mega_menu
                ->id(),
              'simple_mega_menu_revision' => $vid,
            ]),
          ];
        }
        $row[] = [
          'data' => [
            '#type' => 'operations',
            '#links' => $links,
          ],
        ];
      }
      $rows[] = $row;
    }
  }
  $build['simple_mega_menu_revisions_table'] = [
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
  ];
  return $build;
}