View source
<?php
namespace Drupal\name;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\name\Entity\NameListFormat;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Render\FormattableMarkup;
class NameListFormatListBuilder extends ConfigEntityListBuilder {
protected $formatter;
protected $parser;
protected $generator;
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('name.formatter'), $container
->get('name.format_parser'), $container
->get('name.generator'));
}
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, NameFormatterInterface $formatter, NameFormatParser $parser, NameGeneratorInterface $generator) {
parent::__construct($entity_type, $storage);
$this->parser = $parser;
$this->formatter = $formatter;
$this->generator = $generator;
}
public function buildHeader() {
$row = [];
$row['label'] = $this
->t('Label');
$row['id'] = $this
->t('Machine name');
$row['settings'] = $this
->t('Settings');
$row['examples'] = $this
->t('Examples');
$row['operations'] = $this
->t('Operations');
return $row;
}
public function buildRow(EntityInterface $entity) {
$row = [];
$row['label'] = $entity
->label();
$row['id'] = $entity
->id();
$settings = $entity
->listSettings();
$and_options = $this->formatter
->getLastDelimitorTypes();
$and_delimiter = isset($and_options[$settings['and']]) ? $and_options[$settings['and']] : $this
->t('-- invalid option --');
$and_behavior_options = $this->formatter
->getLastDelimitorBehaviors(FALSE);
$and_behavior = isset($and_behavior_options[$settings['delimiter_precedes_last']]) ? $and_behavior_options[$settings['delimiter_precedes_last']] : $this
->t('-- invalid option --');
if ($settings['el_al_min']) {
$behavior = $this
->t('Reduce after @max items and show @min items followed by <em>el al</em>.', [
'@max' => $settings['el_al_min'],
'@min' => $settings['el_al_first'],
]);
}
else {
$behavior = $this
->t('Show all names.');
}
$summary = [
$behavior,
$this
->t('Delimiters: "@delimiter" and @last', [
'@delimiter' => $settings['delimiter'],
'@last' => $and_delimiter,
]),
$this
->t('Last delimiter: @delimiter', [
'@delimiter' => $and_behavior,
]),
];
if ($entity
->isLocked()) {
$summary[] = t('Default format (locked)');
}
$row['settings'] = new FormattableMarkup(implode('<br>', $summary), []);
$row['examples'] = $this
->examples($entity);
$operations = $this
->buildOperations($entity);
$row['operations']['data'] = $operations;
return $row;
}
protected function examples(NameListFormat $entity) {
$examples = [];
foreach ([
1,
2,
3,
4,
] as $num) {
$names = $this->generator
->generateSampleNames($num);
$examples[] = $this
->t('(@num) %list', [
'@num' => $num,
'%list' => $this->formatter
->formatList($names, 'family', $entity
->id()),
]);
}
return new FormattableMarkup(implode('<br>', $examples), []);
}
}