OpignoModuleListBuilder.php in Opigno module 3.x
File
src/OpignoModuleListBuilder.php
View source
<?php
namespace Drupal\opigno_module;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\opigno_module\Entity\OpignoModule;
class OpignoModuleListBuilder extends EntityListBuilder {
protected function getEntities() {
$query = $this
->getStorage()
->getQuery()
->sort($this->entityType
->getKey('id'));
$ids = $query
->execute();
$entities = OpignoModule::loadMultiple($ids);
$entities = array_filter($entities, function ($entity) {
return $entity
->access('update');
});
return $entities;
}
protected function getTotalCount() {
return count($this
->getEntities());
}
protected function getEntityIds() {
$ids = array_keys($this
->getEntities());
if ($this->limit) {
$page = \Drupal::request()->query
->get('page', 0);
$limit = $this->limit;
$start = $limit * $page;
$ids = array_slice($ids, $start, $limit);
}
return $ids;
}
public function render() {
$pager_manager = \Drupal::service('pager.manager');
$pager_manager
->createPager($this
->getTotalCount(), $this->limit);
$build = parent::render();
return $build;
}
public function buildHeader() {
$header['id'] = $this
->t('Module ID');
$header['name'] = $this
->t('Name');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['id'] = $entity
->id();
$row['name'] = Link::createFromRoute($entity
->label(), 'opigno_module.edit', [
'opigno_module' => $entity
->id(),
]);
$ops = parent::buildRow($entity);
$destination = $_SERVER['REQUEST_URI'];
$ops['operations']['data']['#links']['edit']['url'] = new Url('opigno_module.edit', [
'opigno_module' => $entity
->id(),
], [
'query' => [
'destination' => $destination,
],
]);
return $row + $ops;
}
}