class EntityListController implements EntityListControllerInterface, EntityControllerInterface {
protected $storage;
protected $moduleHandler;
protected $entityType;
protected $entityInfo;
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static($entity_type, $entity_info, $container
->get('plugin.manager.entity')
->getStorageController($entity_type), $container
->get('module_handler'));
}
public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler) {
$this->entityType = $entity_type;
$this->storage = $storage;
$this->entityInfo = $entity_info;
$this->moduleHandler = $module_handler;
}
public function getStorageController() {
return $this->storage;
}
public function load() {
return $this->storage
->load();
}
public function getOperations(EntityInterface $entity) {
$uri = $entity
->uri();
$operations['edit'] = array(
'title' => t('Edit'),
'href' => $uri['path'] . '/edit',
'options' => $uri['options'],
'weight' => 10,
);
$operations['delete'] = array(
'title' => t('Delete'),
'href' => $uri['path'] . '/delete',
'options' => $uri['options'],
'weight' => 100,
);
return $operations;
}
public function buildHeader() {
$row['label'] = t('Label');
$row['id'] = t('Machine name');
$row['operations'] = t('Operations');
return $row;
}
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity
->label();
$row['id'] = $entity
->id();
$operations = $this
->buildOperations($entity);
$row['operations']['data'] = $operations;
return $row;
}
public function buildOperations(EntityInterface $entity) {
$operations = $this
->getOperations($entity);
$this->moduleHandler
->alter('entity_operation', $operations, $entity);
uasort($operations, 'drupal_sort_weight');
$build = array(
'#type' => 'operations',
'#links' => $operations,
);
return $build;
}
public function render() {
$build = array(
'#theme' => 'table',
'#header' => $this
->buildHeader(),
'#rows' => array(),
'#empty' => t('There is no @label yet.', array(
'@label' => $this->entityInfo['label'],
)),
);
foreach ($this
->load() as $entity) {
$build['#rows'][$entity
->id()] = $this
->buildRow($entity);
}
return $build;
}
}