Add quick form setup page

This commit is contained in:
Paul Weidner 2024-01-29 19:23:16 -08:00 committed by Michael Stenta
parent 7033224a0d
commit e5894bdce1
4 changed files with 147 additions and 0 deletions

View File

@ -8,5 +8,6 @@ dependencies:
- drupal:taxonomy
- farm:asset
- farm:farm_log_quantity
- farm:farm_setup
- farm:quantity
- log:log

View File

@ -1,3 +1,8 @@
farm.quick:
class: Drupal\Core\Menu\MenuLinkDefault
deriver: Drupal\farm_quick\Plugin\Derivative\QuickFormMenuLink
farm.quick_setup:
title: Quick Forms
description: Quick forms make it easy to record common activities.
parent: farm.setup
route_name: entity.quick_form.collection

View File

@ -23,15 +23,23 @@ use Drupal\farm_quick\QuickFormPluginCollection;
* handlers = {
* "access" = "\Drupal\entity\EntityAccessControlHandler",
* "permission_provider" = "\Drupal\entity\EntityPermissionProvider",
* "list_builder" = "Drupal\farm_quick\QuickFormListBuilder",
* "form" = {
* "configure" = "Drupal\farm_quick\Form\ConfigureQuickForm",
* },
* "route_provider" = {
* "default" = "Drupal\entity\Routing\DefaultHtmlRouteProvider",
* },
* },
* admin_permission = "administer quick_form",
* entity_keys = {
* "id" = "id",
* "status" = "status",
* "label" = "label",
* },
* links = {
* "collection" = "/setup/quick"
* },
* config_export = {
* "id",
* "plugin",

View File

@ -0,0 +1,133 @@
<?php
namespace Drupal\farm_quick;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a listing of template entities.
*/
class QuickFormListBuilder extends ConfigEntityListBuilder {
/**
* The quick form instance manager.
*
* @var \Drupal\farm_quick\QuickFormInstanceManagerInterface
*/
protected $quickFormInstanceManager;
/**
* Constructs a new QuickFormListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\farm_quick\QuickFormInstanceManagerInterface $quick_form_instance_manager
* The quick form instance manager.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, QuickFormInstanceManagerInterface $quick_form_instance_manager) {
parent::__construct($entity_type, $storage);
$this->quickFormInstanceManager = $quick_form_instance_manager;
}
/**
* {@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('quick_form.instance_manager'),
);
}
/**
* {@inheritdoc}
*/
public function load() {
return $this->quickFormInstanceManager->getInstances();
}
/**
* {@inheritdoc}
*/
public function render() {
$render['table'] = [
'#type' => 'table',
'#header' => $this->buildHeader(),
'#caption' => $this->t('Configured quick forms'),
'#rows' => [],
'#empty' => $this->t('There are no configured @label.', ['@label' => $this->entityType->getPluralLabel()]),
'#cache' => [
'contexts' => $this->entityType->getListCacheContexts(),
'tags' => $this->entityType->getListCacheTags(),
],
];
$render['default'] = [
'#type' => 'table',
'#header' => $this->buildHeader(),
'#caption' => $this->t('Default quick forms'),
'#rows' => [],
'#empty' => $this->t('There are no default @label.', ['@label' => $this->entityType->getPluralLabel()]),
];
// Load all quick form instances into proper table.
$quick_form_instances = $this->load();
foreach ($quick_form_instances as $entity) {
$target = $entity->isNew() ? 'default' : 'table';
if ($row = $this->buildRow($entity)) {
$render[$target][$entity->id()] = $row;
}
}
return $render;
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['enabled'] = $this->t('Enabled');
$header['type'] = $this->t('Plugin');
$header['label'] = $this->t('Label');
$header['id'] = $this->t('ID');
$header['description'] = $this->t('Description');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface $quick_form */
$quick_form = $entity;
$row['enabled'] = [
'#type' => 'checkbox',
'#checked' => $quick_form->status(),
'#attributes' => [
'disabled' => 'disabled',
],
];
$row['type'] = [
'#markup' => $quick_form->getPlugin()->getLabel(),
];
$row['label'] = [
'#markup' => $quick_form->getLabel(),
];
$row['id'] = [
'#markup' => $quick_form->id(),
];
$row['description'] = [
'#markup' => $quick_form->getDescription(),
];
return $row + parent::buildRow($entity);
}
}