You are here

public function WorkspaceListBuilder::getDefaultOperations in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/WorkspaceListBuilder.php \Drupal\workspaces\WorkspaceListBuilder::getDefaultOperations()

Gets this list's default operations.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity the operations are for.

Return value

array The array structure is identical to the return value of self::getOperations().

Overrides EntityListBuilder::getDefaultOperations

File

core/modules/workspaces/src/WorkspaceListBuilder.php, line 138

Class

WorkspaceListBuilder
Defines a class to build a listing of workspace entities.

Namespace

Drupal\workspaces

Code

public function getDefaultOperations(EntityInterface $entity) {

  /** @var \Drupal\workspaces\WorkspaceInterface $entity */
  $operations = parent::getDefaultOperations($entity);
  if (isset($operations['edit'])) {
    $operations['edit']['query']['destination'] = $entity
      ->toUrl('collection')
      ->toString();
  }
  $active_workspace = $this->workspaceManager
    ->getActiveWorkspace();
  if (!$active_workspace || $entity
    ->id() != $active_workspace
    ->id()) {
    $operations['activate'] = [
      'title' => $this
        ->t('Switch to @workspace', [
        '@workspace' => $entity
          ->label(),
      ]),
      // Use a weight lower than the one of the 'Edit' operation because we
      // want the 'Activate' operation to be the primary operation.
      'weight' => 0,
      'url' => $entity
        ->toUrl('activate-form', [
        'query' => [
          'destination' => $entity
            ->toUrl('collection')
            ->toString(),
        ],
      ]),
    ];
  }
  if (!$entity
    ->hasParent()) {
    $operations['publish'] = [
      'title' => $this
        ->t('Publish content'),
      // The 'Publish' operation should be the default one for the currently
      // active workspace.
      'weight' => $active_workspace && $entity
        ->id() == $active_workspace
        ->id() ? 0 : 20,
      'url' => Url::fromRoute('entity.workspace.publish_form', [
        'workspace' => $entity
          ->id(),
      ], [
        'query' => [
          'destination' => $entity
            ->toUrl('collection')
            ->toString(),
        ],
      ]),
    ];
  }
  else {

    /** @var \Drupal\workspaces\WorkspaceInterface $parent */
    $parent = $entity->parent->entity;
    $operations['merge'] = [
      'title' => $this
        ->t('Merge into @target_label', [
        '@target_label' => $parent
          ->label(),
      ]),
      'weight' => 5,
      'url' => Url::fromRoute('entity.workspace.merge_form', [
        'source_workspace' => $entity
          ->id(),
        'target_workspace' => $parent
          ->id(),
      ], [
        'query' => [
          'destination' => $entity
            ->toUrl('collection')
            ->toString(),
        ],
      ]),
    ];
  }
  $operations['manage'] = [
    'title' => $this
      ->t('Manage'),
    'weight' => 5,
    'url' => $entity
      ->toUrl(),
  ];
  return $operations;
}