function WorkspaceListBuilder::getDefaultOperations

Same name in other branches
  1. 8.9.x core/modules/workspaces/src/WorkspaceListBuilder.php \Drupal\workspaces\WorkspaceListBuilder::getDefaultOperations()
  2. 10 core/modules/workspaces/src/WorkspaceListBuilder.php \Drupal\workspaces\WorkspaceListBuilder::getDefaultOperations()
  3. 11.x core/modules/workspaces/src/WorkspaceListBuilder.php \Drupal\workspaces\WorkspaceListBuilder::getDefaultOperations()

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.