You are here

public function ModerationSidebarController::sideBar in Moderation Sidebar 8

Displays information relevant to moderating an entity in-line.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: A moderated entity.

Return value

array The render array for the sidebar.

1 string reference to 'ModerationSidebarController::sideBar'
moderation_sidebar.routing.yml in ./moderation_sidebar.routing.yml
moderation_sidebar.routing.yml

File

src/Controller/ModerationSidebarController.php, line 145

Class

ModerationSidebarController
Endpoints for the Moderation Sidebar module.

Namespace

Drupal\moderation_sidebar\Controller

Code

public function sideBar(ContentEntityInterface $entity) {

  // Load the correct translation.
  $langcode = $entity
    ->language()
    ->getId();
  $entity_type_id = $entity
    ->getEntityTypeId();

  /** @var \Drupal\Core\Entity\TranslatableRevisionableStorageInterface $storage */

  // Figure ouf this is the latest revision of this entity for this language.
  $storage = \Drupal::entityTypeManager()
    ->getStorage($entity_type_id);
  $is_latest = TRUE;
  if ($storage instanceof TranslatableRevisionableStorageInterface) {
    $latest_revision_id = $storage
      ->getLatestTranslationAffectedRevisionId($entity
      ->id(), $langcode);

    // If the latest revision is the same as the entity revision, it is the
    // last revision. If it is older, then that means that there is a newer
    // default revision from another language, use the default revision in
    // that case.
    if ($latest_revision_id < $entity
      ->getRevisionId()) {
      $entity = $storage
        ->load($entity
        ->id())
        ->getTranslation($langcode);
      $is_latest = TRUE;
    }
    elseif ($latest_revision_id == $entity
      ->getRevisionId()) {
      $is_latest = TRUE;
    }
    else {
      $is_latest = FALSE;
    }
  }
  $build = [
    '#theme' => 'moderation_sidebar_container',
    '#attributes' => [
      'class' => [
        'moderation-sidebar-container',
      ],
    ],
  ];

  // Add information about this Entity to the top of the bar.
  $build['info'] = [
    '#theme' => 'moderation_sidebar_info',
    '#state' => $this
      ->getStateLabel($entity),
  ];
  if ($entity instanceof RevisionLogInterface) {

    // Entity could implement RevisionLogInterface, but not having a revision
    // user or creation time set, i.e. taxonomy_term created before 8.7.0.
    // @see https://www.drupal.org/node/2897789
    if ($user = $entity
      ->getRevisionUser()) {
      $build['info']['#revision_author'] = $user
        ->getDisplayName();
      $build['info']['#revision_author_link'] = $user
        ->toLink()
        ->toRenderable();
    }
    if ($time = (int) $entity
      ->getRevisionCreationTime()) {
      $time_pretty = $this
        ->getPrettyTime($time);
      $build['info']['#revision_time'] = $time;
      $build['info']['#revision_time_pretty'] = $time_pretty;
    }
  }
  $build['actions'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'moderation-sidebar-actions',
      ],
    ],
  ];
  $build['actions']['secondary'] = [];
  if ($this->moderationInformation
    ->isModeratedEntity($entity)) {

    // Provide a link to the latest entity.
    if ($this->moderationInformation
      ->hasPendingRevision($entity) && $entity
      ->isDefaultRevision()) {
      $build['actions']['primary']['view_latest'] = [
        '#title' => $this
          ->t('View existing draft'),
        '#type' => 'link',
        '#url' => Url::fromRoute("entity.{$entity_type_id}.latest_version", [
          $entity_type_id => $entity
            ->id(),
        ]),
        '#attributes' => [
          'class' => [
            'moderation-sidebar-link',
            'button',
          ],
        ],
      ];
    }

    // Provide a link to the default display of the entity.
    if ($this->moderationInformation
      ->hasPendingRevision($entity) && !$entity
      ->isDefaultRevision()) {
      $build['actions']['primary']['view_default'] = [
        '#title' => $this
          ->t('View live content'),
        '#type' => 'link',
        '#url' => $entity
          ->toLink()
          ->getUrl(),
        '#attributes' => [
          'class' => [
            'moderation-sidebar-link',
            'button',
          ],
        ],
      ];
    }

    // Show an edit link.
    if ($entity
      ->access('update')) {
      $build['actions']['primary']['edit'] = [
        '#title' => !$this->moderationInformation
          ->hasPendingRevision($entity) ? $this
          ->t('Edit content') : $this
          ->t('Edit draft'),
        '#type' => 'link',
        '#url' => $entity
          ->toLink(NULL, 'edit-form')
          ->getUrl(),
        '#attributes' => [
          'class' => [
            'moderation-sidebar-link',
            'button',
          ],
        ],
      ];
    }

    // Only show the entity delete action on the default revision.
    if ($entity
      ->isDefaultRevision() && $entity
      ->access('delete')) {
      $build['actions']['primary']['delete'] = [
        '#title' => $this
          ->t('Delete content'),
        '#type' => 'link',
        '#url' => $entity
          ->toLink(NULL, 'delete-form')
          ->getUrl(),
        '#attributes' => [
          'class' => [
            'moderation-sidebar-link',
            'button',
            'button--danger',
          ],
        ],
        '#weight' => 1,
      ];
    }

    // We maintain our own inline revisions tab.
    if ($entity_type_id === 'node' && \Drupal::service('access_check.node.revision')
      ->checkAccess($entity, \Drupal::currentUser()
      ->getAccount())) {
      $build['actions']['secondary']['version_history'] = [
        '#title' => $this
          ->t('Show revisions'),
        '#type' => 'link',
        '#url' => Url::fromRoute('moderation_sidebar.node.version_history', [
          'node' => $entity
            ->id(),
        ], [
          'query' => [
            'latest' => $is_latest,
          ],
        ]),
        '#attributes' => [
          'class' => [
            'moderation-sidebar-link',
            'button',
            'use-ajax',
          ],
          'data-dialog-type' => 'dialog',
          'data-dialog-renderer' => 'off_canvas',
        ],
      ];
    }

    // We maintain our own inline translate tab.
    if ($this
      ->moduleHandler()
      ->moduleExists('content_translation') && \Drupal::service('content_translation.manager')
      ->isSupported($entity_type_id)) {
      $build['actions']['secondary']['translate'] = [
        '#title' => $this
          ->t('Translate'),
        '#type' => 'link',
        '#url' => Url::fromRoute($is_latest ? 'moderation_sidebar.translate_latest' : 'moderation_sidebar.translate', [
          'entity_type' => $entity_type_id,
          'entity' => $entity
            ->id(),
        ], [
          'query' => [
            'latest' => $is_latest,
          ],
        ]),
        '#attributes' => [
          'class' => [
            'moderation-sidebar-link',
            'button',
            'use-ajax',
          ],
          'data-dialog-type' => 'dialog',
          'data-dialog-renderer' => 'off_canvas',
        ],
      ];
    }

    // Provide a list of actions representing transitions for this revision.
    if ($is_latest) {
      $build['actions']['primary']['quick_draft_form'] = $this
        ->formBuilder()
        ->getForm(QuickTransitionForm::class, $entity);
    }
  }

  // Add a list of (non duplicated) local tasks.
  $build['actions']['secondary'] += $this
    ->getLocalTasks($entity);

  // Allow other module to alter our build.
  $this->moduleHandler
    ->alter('moderation_sidebar', $build, $entity);
  return $build;
}