devel_wizard-2.x-dev/templates/spell/entity_type/config/list_builder.php.twig
templates/spell/entity_type/config/list_builder.php.twig
{%
include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
with {
'namespace': config.namespace,
}
%}
{%-
include '@devel_wizard/php/devel_wizard.php.file.use_statements.php.twig'
with {
'useStatements': {
'Drupal\\Core\\Config\\Entity\\DraggableListBuilder': '',
'Drupal\\Core\\Entity\\EntityInterface': '',
'Drupal\\Core\\Entity\\EntityTypeInterface': '',
'Drupal\\Core\\Entity\\EntityTypeManagerInterface': '',
'Drupal\\Core\\Form\\FormStateInterface': '',
'Drupal\\Core\\Messenger\\MessengerInterface': '',
'Drupal\\Core\\Render\\RendererInterface': '',
'Drupal\\Core\\Session\\AccountInterface': '',
'Drupal\\Core\\Url': '',
'Symfony\\Component\\DependencyInjection\\ContainerInterface': '',
},
}
%}
/**
* @property \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage
*/
class ListBuilder extends DraggableListBuilder {
protected AccountInterface $currentUser;
protected EntityTypeManagerInterface $entityTypeManager;
protected RendererInterface $renderer;
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('current_user'),
$container->get('entity_type.manager'),
$container->get('renderer'),
$container->get('messenger'),
);
}
public function __construct(
EntityTypeInterface $entity_type,
AccountInterface $current_user,
EntityTypeManagerInterface $entity_type_manager,
RendererInterface $renderer,
MessengerInterface $messenger,
) {
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
$this->messenger = $messenger;
/* @noinspection PhpUnhandledExceptionInspection */
parent::__construct(
$entity_type,
$entity_type_manager->getStorage($entity_type->id()),
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return '{{ module.machineName }}_{{ config.id }}_list_form';
}
{% if goal == 'bundleable' -%}
/**
* {@inheritdoc}
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity->access('{{ module.machineName }}.{{ config.id }}.admin')) {
$operations['list'] = [
'title' => $this->t('List {{ content.label_plural }} (not yet)'),
'weight' => 0,
'url' => $entity->toUrl('edit-form'),
];
}
$contentEntityTypeId = $entity->getEntityType()->getBundleOf();
$accessControlHandler = $this->entityTypeManager->getAccessControlHandler($contentEntityTypeId);
if ($accessControlHandler->createAccess($entity->id())) {
$operations['add'] = [
'title' => $this->t('Add {{ content.label }}'),
'weight' => 10,
'url' => Url::fromRoute('entity.{{ content.id }}.add_form', ['{{ config.id }}' => $entity->id()]),
];
}
return $operations;
}
{% endif -%}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header = [
'label' => $this->t('Label'),
'description' => $this->t('Description'),
];
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \{{ config.interface_fqn }} $entity */
$row = [
'label' => $entity->label(),
'description' => [
'data' => [
'#markup' => $entity->getDescription(),
],
],
];
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$rows = $form_state->getValue($this->entitiesKey);
if (is_iterable($rows)) {
parent::submitForm($form, $form_state);
}
$this->messenger->addStatus($this->t('The configuration options have been saved.'));
}
}
