WorkspaceListBuilder.php in Workspace 8.2
File
src/WorkspaceListBuilder.php
View source
<?php
namespace Drupal\workspace;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\workspace\Plugin\RepositoryHandler\NullRepositoryHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkspaceListBuilder extends EntityListBuilder {
protected $workspaceManager;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, WorkspaceManagerInterface $workspace_manager) {
parent::__construct($entity_type, $storage);
$this->workspaceManager = $workspace_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.manager')
->getStorage($entity_type
->id()), $container
->get('workspace.manager'));
}
public function buildHeader() {
$header['label'] = $this
->t('Workspace');
$header['uid'] = $this
->t('Owner');
$header['status'] = $this
->t('Status');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['label'] = $this
->t('@label (@id)', [
'@label' => $entity
->label(),
'@id' => $entity
->id(),
]);
$row['owner'] = $entity
->getOwner()
->getDisplayname();
$active_workspace = $this->workspaceManager
->getActiveWorkspace()
->id();
$row['status'] = $active_workspace == $entity
->id() ? $this
->t('Active') : $this
->t('Inactive');
return $row + parent::buildRow($entity);
}
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if (isset($operations['edit'])) {
$operations['edit']['query']['destination'] = $entity
->toUrl('collection')
->toString();
}
$active_workspace = $this->workspaceManager
->getActiveWorkspace();
if ($entity
->id() != $active_workspace
->id()) {
$operations['activate'] = [
'title' => $this
->t('Set Active'),
'weight' => 0,
'url' => $entity
->toUrl('activate-form', [
'query' => [
'destination' => $entity
->toUrl('collection')
->toString(),
],
]),
];
}
if (!$entity
->getRepositoryHandler() instanceof NullRepositoryHandler) {
$operations['deploy'] = [
'title' => $this
->t('Deploy content'),
'weight' => $entity
->id() == $active_workspace
->id() ? 0 : 20,
'url' => $entity
->toUrl('deploy-form', [
'query' => [
'destination' => $entity
->toUrl('collection')
->toString(),
],
]),
];
}
return $operations;
}
}