View source
<?php
namespace Drupal\site_settings;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\site_settings\Entity\SiteSettingEntityType;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SiteSettingEntityListBuilder extends EntityListBuilder {
private $bundles = [];
private $linkGeneration;
protected $entityTypeManager;
protected $siteSettingsRender;
private $siteSettingsLoader;
protected $renderer;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, LinkGeneratorInterface $link_generator, EntityTypeManagerInterface $entity_type_manager, SiteSettingsRenderer $site_settings_reader, SiteSettingsLoader $site_settings_loader, RendererInterface $renderer) {
parent::__construct($entity_type, $storage);
$this->linkGeneration = $link_generator;
$this->entityTypeManager = $entity_type_manager;
$this->siteSettingsRender = $site_settings_reader;
$this->siteSettingsLoader = $site_settings_loader;
$this->renderer = $renderer;
}
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('link_generator'), $container
->get('entity_type.manager'), $container
->get('site_settings.renderer'), $container
->get('site_settings.loader'), $container
->get('renderer'));
}
public function buildHeader() {
$header['name'] = $this
->t('Name');
$header['fieldset'] = $this
->t('Group');
$header['value'] = $this
->t('Value');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['name'] = $this->linkGeneration
->generate($entity
->label(), new Url('entity.site_setting_entity.edit_form', [
'site_setting_entity' => $entity
->id(),
]));
$entity_bundle = $entity
->bundle();
if ($bundle = SiteSettingEntityType::load($entity_bundle)) {
$row['fieldset'] = $bundle->fieldset;
}
else {
$row['fieldset'] = $this
->t('Unknown');
}
$row['value'] = '';
$fields = $entity
->getFields();
foreach ($fields as $key => $field) {
if (method_exists(get_class($field), 'getFieldDefinition')) {
$row['value'] = $this->siteSettingsRender
->renderField($field);
}
}
return $row + parent::buildRow($entity);
}
protected function getEntityIds() {
$query = $this
->getStorage()
->getQuery()
->sort($this->entityType
->getKey('fieldset'))
->sort($this->entityType
->getKey('id'));
return $query
->execute();
}
public function render() {
$entity_type = $this->entityType
->getBundleEntityType();
$this->bundles = $this->entityTypeManager
->getStorage($entity_type)
->loadMultiple();
$missing_bundles = array_keys($this->bundles);
$variables['settings'] = $this->siteSettingsLoader
->loadAll(TRUE);
$build['table'] = [
'#type' => 'table',
'#header' => $this
->buildHeader(),
'#title' => $this
->getTitle(),
'#rows' => [],
'#empty' => $this
->t('There is no @label yet.', [
'@label' => $this->entityType
->getLabel(),
]),
'#cache' => [
'contexts' => $this->entityType
->getListCacheContexts(),
'tags' => $this->entityType
->getListCacheTags(),
],
];
$creatable_non_viewable_entities = [];
$last_fieldset = FALSE;
foreach ($this
->load() as $entity) {
$bundle_type = $entity
->getType();
$search = array_search($bundle_type, $missing_bundles);
if ($search !== FALSE) {
unset($missing_bundles[$search]);
}
if ($entity
->access('update') || $entity
->access('delete')) {
$fieldset = $entity->fieldset
->getValue()[0]['value'];
if ($fieldset != $last_fieldset) {
$heading = [
'#markup' => '<strong>' . $fieldset . '</strong>',
];
$build['table']['#rows'][$fieldset] = [
'name' => $this->renderer
->render($heading),
'fieldset' => '',
'value' => '',
'operations' => '',
];
$last_fieldset = $fieldset;
}
if ($row = $this
->buildRow($entity)) {
$build['table']['#rows'][$entity
->id()] = $row;
}
}
elseif ($entity
->access('create')) {
$creatable_non_viewable_entities[$bundle_type] = $entity;
}
}
if ($creatable_non_viewable_entities) {
foreach ($creatable_non_viewable_entities as $bundle => $entity) {
$url = new Url('entity.site_setting_entity.add_form', [
'site_setting_entity_type' => $bundle,
]);
if ($url
->access()) {
$link = [
'#type' => 'link',
'#title' => $this
->t('Create setting'),
'#url' => $url,
'#attributes' => [
'class' => [
'button',
],
],
];
array_unshift($build['table']['#rows'], [
'name' => $this->linkGeneration
->generate($entity
->label(), $url),
'fieldset' => $entity->fieldset->value,
'value' => '',
'operations' => $this->renderer
->render($link),
]);
}
}
$heading = [
'#markup' => '<strong>' . $this
->t('Settings where more can be created') . '</strong>',
];
array_unshift($build['table']['#rows'], [
'name' => $this->renderer
->render($heading),
'fieldset' => '',
'value' => '',
'operations' => '',
]);
}
if ($missing_bundles) {
usort($missing_bundles, function ($a, $b) {
if ($this->bundles[$a]->fieldset == $this->bundles[$b]->fieldset) {
return $this->bundles[$a]
->label() >= $this->bundles[$b]
->label() ? -1 : 1;
}
return $this->bundles[$a]->fieldset >= $this->bundles[$b]->fieldset ? -1 : 1;
});
$has_access_to_not_yet_created = FALSE;
foreach ($missing_bundles as $missing) {
$url = new Url('entity.site_setting_entity.add_form', [
'site_setting_entity_type' => $missing,
]);
if ($url
->access()) {
$has_access_to_not_yet_created = TRUE;
$link = [
'#type' => 'link',
'#title' => $this
->t('Create setting'),
'#url' => $url,
'#attributes' => [
'class' => [
'button',
],
],
];
array_unshift($build['table']['#rows'], [
'name' => $this->linkGeneration
->generate($this->bundles[$missing]
->label(), $url),
'fieldset' => $this->bundles[$missing]->fieldset,
'value' => '',
'operations' => $this->renderer
->render($link),
]);
}
}
if ($has_access_to_not_yet_created) {
$heading = [
'#markup' => '<strong>' . $this
->t('Settings not yet created') . '</strong>',
];
array_unshift($build['table']['#rows'], [
'name' => $this->renderer
->render($heading),
'fieldset' => '',
'value' => '',
'operations' => '',
]);
}
}
return $build;
}
public function buildOperations(EntityInterface $entity) {
$build = [
'#attributes' => [
'class' => [
'container-inline',
],
],
];
$operations = $this
->getDefaultOperations($entity);
$operations += $this
->moduleHandler()
->invokeAll('entity_operation', [
$entity,
]);
$this->moduleHandler
->alter('entity_operation', $operations, $entity);
uasort($operations, '\\Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
$build['operations'] = [
'#prefix' => '<div class="align-left clearfix">',
'#type' => 'operations',
'#links' => $operations,
'#suffix' => '</div>',
];
$entity_bundle = $entity
->bundle();
if (isset($this->bundles[$entity_bundle]) && $this->bundles[$entity_bundle]->multiple) {
$url = new Url('entity.site_setting_entity.add_form', [
'site_setting_entity_type' => $entity_bundle,
]);
if ($url
->access()) {
$build['add'] = [
'#prefix' => '<div class="align-right">',
'#type' => 'link',
'#title' => $this
->t('Add another'),
'#url' => $url,
'#attributes' => [
'class' => [
'button',
],
],
'#suffix' => '</div>',
];
}
}
return $build;
}
}