Add quick form add form
This commit is contained in:
parent
ad3965208f
commit
78a78b3aaa
|
@ -0,0 +1,5 @@
|
|||
farm_quick.add_page:
|
||||
title: 'Add quick form'
|
||||
route_name: farm_quick.add_page
|
||||
appears_on:
|
||||
- entity.quick_form.collection
|
|
@ -18,5 +18,24 @@ farm_quick.configure:
|
|||
quick_form:
|
||||
type: string
|
||||
|
||||
farm_quick.add_page:
|
||||
path: 'setup/quick/add'
|
||||
defaults:
|
||||
_controller: \Drupal\farm_quick\Controller\QuickFormAddPage::addPage
|
||||
_title: 'Add quick form'
|
||||
requirements:
|
||||
_permission: 'create quick_form'
|
||||
|
||||
farm_quick.add_form:
|
||||
path: '/setup/quick/add/{plugin}'
|
||||
defaults:
|
||||
_entity_form: quick_form.add
|
||||
requirements:
|
||||
_permission: 'create quick_form'
|
||||
options:
|
||||
parameters:
|
||||
plugin:
|
||||
type: string
|
||||
|
||||
route_callbacks:
|
||||
- '\Drupal\farm_quick\Routing\QuickFormRoutes::routes'
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_quick\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Drupal\Core\Link;
|
||||
use Drupal\farm_quick\QuickFormPluginManager;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Page that renders links to create instances of quick form plugins.
|
||||
*/
|
||||
class QuickFormAddPage extends ControllerBase {
|
||||
|
||||
/**
|
||||
* The quick form plugin manager.
|
||||
*
|
||||
* @var \Drupal\farm_quick\QuickFormPluginManager
|
||||
*/
|
||||
protected $quickFormPluginManager;
|
||||
|
||||
/**
|
||||
* Constructs a new QuickFormAddPage object.
|
||||
*/
|
||||
public function __construct(QuickFormPluginManager $quick_form_plugin_manager) {
|
||||
$this->quickFormPluginManager = $quick_form_plugin_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('plugin.manager.quick_form'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add quick form page callback.
|
||||
*
|
||||
* @return array
|
||||
* Render array.
|
||||
*/
|
||||
public function addPage(): array {
|
||||
|
||||
$render = [
|
||||
'#theme' => 'entity_add_list',
|
||||
'#bundles' => [],
|
||||
'#cache' => [
|
||||
'tags' => $this->quickFormPluginManager->getCacheTags(),
|
||||
],
|
||||
];
|
||||
|
||||
// Filter to configurable quick form plugins.
|
||||
$plugins = array_filter($this->quickFormPluginManager->getDefinitions(), function (array $plugin) {
|
||||
if (($instance = $this->quickFormPluginManager->createInstance($plugin['id'])) && $instance->isConfigurable()) {
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
});
|
||||
|
||||
// Add link for each configurable plugin.
|
||||
foreach ($plugins as $plugin_id => $plugin) {
|
||||
$render['#bundles'][$plugin_id] = [
|
||||
'label' => $plugin['label'],
|
||||
'description' => $plugin['description'] ?? '',
|
||||
'add_link' => Link::createFromRoute($plugin['label'], 'farm_quick.add_form', ['plugin' => $plugin_id]),
|
||||
];
|
||||
}
|
||||
|
||||
return $render;
|
||||
}
|
||||
|
||||
}
|
|
@ -25,6 +25,7 @@ use Drupal\farm_quick\QuickFormPluginCollection;
|
|||
* "permission_provider" = "\Drupal\entity\EntityPermissionProvider",
|
||||
* "list_builder" = "Drupal\farm_quick\QuickFormListBuilder",
|
||||
* "form" = {
|
||||
* "add" = "Drupal\farm_quick\Form\QuickFormEntityForm",
|
||||
* "edit" = "Drupal\farm_quick\Form\QuickFormEntityForm",
|
||||
* "configure" = "Drupal\farm_quick\Form\ConfigureQuickForm",
|
||||
* "delete" = "\Drupal\Core\Entity\EntityDeleteForm",
|
||||
|
|
|
@ -5,6 +5,11 @@ namespace Drupal\farm_quick\Form;
|
|||
use Drupal\Core\Entity\EntityForm;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Form\SubformState;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\farm_quick\Entity\QuickFormInstance;
|
||||
use Drupal\farm_quick\QuickFormPluginManager;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Form that renders quick form configuration forms.
|
||||
|
@ -18,6 +23,32 @@ class QuickFormEntityForm extends EntityForm {
|
|||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* The quick form plugin manager.
|
||||
*
|
||||
* @var \Drupal\farm_quick\QuickFormPluginManager
|
||||
*/
|
||||
protected $quickFormPluginManager;
|
||||
|
||||
/**
|
||||
* Constructs a new QuickFormEntityForm object.
|
||||
*
|
||||
* @param \Drupal\farm_quick\QuickFormPluginManager $quick_form_plugin_manager
|
||||
* The quick form plugin manager.
|
||||
*/
|
||||
public function __construct(QuickFormPluginManager $quick_form_plugin_manager) {
|
||||
$this->quickFormPluginManager = $quick_form_plugin_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('plugin.manager.quick_form'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -55,7 +86,6 @@ class QuickFormEntityForm extends EntityForm {
|
|||
$form['label'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Label'),
|
||||
'#default_value' => $this->entity->label(),
|
||||
'#maxlength' => 255,
|
||||
'#required' => TRUE,
|
||||
'#group' => $tab_group,
|
||||
|
@ -63,14 +93,31 @@ class QuickFormEntityForm extends EntityForm {
|
|||
|
||||
$form['id'] = [
|
||||
'#type' => 'machine_name',
|
||||
'#default_value' => $this->entity->id(),
|
||||
'#machine_name' => [
|
||||
'exists' => '\Drupal\farm_quick\Entity\QuickFormInstance::load',
|
||||
],
|
||||
'#disabled' => !$this->entity->isNew(),
|
||||
'#disabled' => !$this->entity->isNew() || $this->getRequest()->get('override'),
|
||||
'#group' => $tab_group,
|
||||
];
|
||||
|
||||
// Provide default label and ID for existing config entities
|
||||
// or if the override parameter is set.
|
||||
if (!$this->entity->isNew() || $this->getRequest()->get('override')) {
|
||||
$form['label']['#default_value'] = $this->entity->label();
|
||||
$form['id']['#default_value'] = $this->entity->id();
|
||||
}
|
||||
|
||||
// Adjust form title.
|
||||
if ($this->entity->isNew()) {
|
||||
$form['#title'] = $this->t('Add quick form: @label', ['@label' => $this->entity->getPlugin()->getLabel()]);
|
||||
if ($this->getRequest()->get('override')) {
|
||||
$form['#title'] = $this->t('Override quick form: @label', ['@label' => $this->entity->getPlugin()->getLabel()]);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$form['#title'] = $this->t('Edit quick form: @label', ['@label' => $this->entity->label()]);
|
||||
}
|
||||
|
||||
$form['description'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Description'),
|
||||
|
@ -122,4 +169,30 @@ class QuickFormEntityForm extends EntityForm {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
|
||||
|
||||
// Get existing quick form entity from route parameter.
|
||||
if ($route_match->getRawParameter($entity_type_id) !== NULL) {
|
||||
$entity = $route_match->getParameter($entity_type_id);
|
||||
}
|
||||
// Else create a new quick form entity, the plugin must be specified.
|
||||
else {
|
||||
if (($plugin = $route_match->getRawParameter('plugin')) && $this->quickFormPluginManager->hasDefinition($plugin)) {
|
||||
$entity = QuickFormInstance::create(['plugin' => $plugin]);
|
||||
if ($this->getRequest()->get('override')) {
|
||||
$entity->set('id', $plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($entity)) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
|
|||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
|
@ -130,4 +131,27 @@ class QuickFormListBuilder extends ConfigEntityListBuilder {
|
|||
return $row + parent::buildRow($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultOperations(EntityInterface $entity) {
|
||||
$operations = parent::getDefaultOperations($entity);
|
||||
|
||||
// Override operations for default quick form instances.
|
||||
if ($entity->isNew()) {
|
||||
|
||||
// Remove edit operation.
|
||||
unset($operations['edit']);
|
||||
|
||||
// Add override operation.
|
||||
$operations['override'] = [
|
||||
'title' => $this->t('Override'),
|
||||
'weight' => 0,
|
||||
'url' => $this->ensureDestination(Url::fromRoute('farm_quick.add_form', ['plugin' => $entity->getPluginId()], ['query' => ['override' => TRUE]])),
|
||||
];
|
||||
}
|
||||
|
||||
return $operations;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue