FormatterListBuilder.php in Custom Formatters 8.3
File
src/FormatterListBuilder.php
View source
<?php
namespace Drupal\custom_formatters;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FormatterListBuilder extends ConfigEntityListBuilder {
protected $formatterTypes;
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('plugin.manager.custom_formatters.formatter_type'));
}
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, FormatterTypeManager $formatter_type_manager) {
parent::__construct($entity_type, $storage);
$this->formatterTypes = $formatter_type_manager
->getDefinitions();
}
public function buildHeader() {
$header['label'] = $this
->t('Label');
$header['type'] = $this
->t('Type');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$formatter = $entity;
$row['label'] = $formatter
->label();
$row['type'] = $this->formatterTypes[$formatter
->get('type')]['label'];
return $row + parent::buildRow($formatter);
}
public function render() {
$build = parent::render();
$build['table']['#empty'] = $this
->t('There are no @labels yet. Add a @add_link.', [
'@label' => $this->entityType
->getLabel(),
'@add_link' => Link::createFromRoute($this->entityType
->getLabel(), 'custom_formatters.add_page')
->toString(),
]);
return $build;
}
}