Repository URL to install this package:
|
Version:
2.0.2 ▾
|
<?php
namespace Drupal\custom_forms;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a list controller for the custom form entity type.
*/
class CustomFormListBuilder extends EntityListBuilder {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The redirect destination service.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirectDestination;
/**
* Constructs a new CustomFormListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
* The redirect destination service.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
$this->redirectDestination = $redirect_destination;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity_type.manager')->getStorage($entity_type->id()),
$container->get('date.formatter'),
$container->get('redirect.destination')
);
}
/**
* {@inheritdoc}
*/
public function render() {
$build['table'] = parent::render();
$total = \Drupal::database()
->query('SELECT COUNT(*) FROM {custom_forms}')
->fetchField();
$build['summary']['#markup'] = $this->t('Total custom forms: @total', ['@total' => $total]);
return $build;
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('ID');
$header['title'] = $this->t('Title');
$header['status'] = $this->t('Status');
$header['created'] = $this->t('Created');
$header['changed'] = $this->t('Updated');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var $entity \Drupal\custom_forms\Entity\CustomForm */
$row['id'] = $entity->id();
$row['title'] = $entity->toLink($entity->label());
$row['status'] = $entity->isEnabled() ? $this->t('Enabled') : $this->t('Disabled');
$row['created'] = $this->dateFormatter->format($entity->getCreatedTime());
$row['changed'] = $this->dateFormatter->format($entity->getChangedTime());
return $row + parent::buildRow($entity);
}
}