WorkspaceListBuilder.php in Workspace 8
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\multiversion\Workspace\WorkspaceManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkspaceListBuilder extends EntityListBuilder {
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 __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, WorkspaceManagerInterface $workspace_manager) {
parent::__construct($entity_type, $storage);
$this->workspaceManager = $workspace_manager;
}
public function buildHeader() {
$header['label'] = t('Workspace');
$header['uid'] = t('Owner');
$header['type'] = t('Type');
$header['status'] = t('Status');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity
->label() . ' (' . $entity
->getMachineName() . ')';
$row['owner'] = $entity
->getOwner()
->getDisplayname();
$type = $entity
->get('type')
->first()->entity;
$row['type'] = $type ? $type
->label() : '';
$active_workspace = $this->workspaceManager
->getActiveWorkspaceId();
$row['status'] = $active_workspace == $entity
->id() ? 'Active' : '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
->getActiveWorkspaceId();
if ($entity
->id() != $active_workspace) {
$operations['activate'] = [
'title' => $this
->t('Set Active'),
'weight' => 20,
'url' => $entity
->toUrl('activate-form', [
'query' => [
'destination' => $entity
->toUrl('collection')
->toString(),
],
]),
];
}
$operations['changes'] = [
'title' => $this
->t('View Changes'),
'weight' => 21,
'url' => $entity
->toUrl('changes', [
'workspace' => $entity
->id(),
]),
];
$operations['conflicts'] = [
'title' => $this
->t('View Conflicts'),
'weight' => 22,
'url' => $entity
->toUrl('conflicts', [
'workspace' => $entity
->id(),
]),
];
return $operations;
}
protected function getEntityIds() {
$query = $this
->getStorage()
->getQuery()
->sort($this->entityType
->getKey('id'))
->condition('published', TRUE)
->condition('queued_for_delete', FALSE);
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
}