You are here

OpignoModuleListBuilder.php in Opigno module 8

Same filename and directory in other branches
  1. 3.x src/OpignoModuleListBuilder.php

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;

/**
 * Defines a class to build a listing of Module entities.
 *
 * @ingroup opigno_module
 */
class OpignoModuleListBuilder extends EntityListBuilder {

  /**
   * Returns module list.
   */
  protected function getEntities() {
    $query = $this
      ->getStorage()
      ->getQuery()
      ->sort($this->entityType
      ->getKey('id'));
    $ids = $query
      ->execute();

    // Filter entities that user has edit access.
    $entities = OpignoModule::loadMultiple($ids);
    $entities = array_filter($entities, function ($entity) {
      return $entity
        ->access('update');
    });
    return $entities;
  }

  /**
   * Returns modules count.
   */
  protected function getTotalCount() {
    return count($this
      ->getEntities());
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntityIds() {
    $ids = array_keys($this
      ->getEntities());

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $page = \Drupal::request()->query
        ->get('page', 0);
      $limit = $this->limit;
      $start = $limit * $page;
      $ids = array_slice($ids, $start, $limit);
    }
    return $ids;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $pager_manager = \Drupal::service('pager.manager');
    $pager_manager
      ->createPager($this
      ->getTotalCount(), $this->limit);
    $build = parent::render();
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['id'] = $this
      ->t('Module ID');
    $header['name'] = $this
      ->t('Name');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    /* @var $entity \Drupal\opigno_module\Entity\OpignoModule */
    $row['id'] = $entity
      ->id();
    $row['name'] = Link::createFromRoute($entity
      ->label(), 'opigno_module.edit', [
      'opigno_module' => $entity
        ->id(),
    ]);

    // Change default edit link.
    $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;
  }

}

Classes

Namesort descending Description
OpignoModuleListBuilder Defines a class to build a listing of Module entities.