Refactor ConfigureQuickForm to use EntityForm
This commit is contained in:
parent
fa6329f579
commit
8903ff44bc
|
@ -6,5 +6,17 @@ farm.quick:
|
|||
requirements:
|
||||
_permission: 'view quick forms index'
|
||||
|
||||
farm_quick.configure:
|
||||
path: /quick/{quick_form}/configure
|
||||
defaults:
|
||||
_entity_form: quick_form.configure
|
||||
_title_callback: \Drupal\farm_quick\Form\ConfigureQuickForm::getTitle
|
||||
requirements:
|
||||
_custom_access: \Drupal\farm_quick\Form\ConfigureQuickForm::access
|
||||
options:
|
||||
parameters:
|
||||
quick_form:
|
||||
type: string
|
||||
|
||||
route_callbacks:
|
||||
- '\Drupal\farm_quick\Routing\QuickFormRoutes::routes'
|
||||
|
|
|
@ -20,6 +20,9 @@ use Drupal\farm_quick\QuickFormPluginCollection;
|
|||
* plural = "@count quick forms",
|
||||
* ),
|
||||
* handlers = {
|
||||
* "form" = {
|
||||
* "configure" = "Drupal\farm_quick\Form\ConfigureQuickForm",
|
||||
* },
|
||||
* },
|
||||
* entity_keys = {
|
||||
* "id" = "id",
|
||||
|
|
|
@ -3,34 +3,69 @@
|
|||
namespace Drupal\farm_quick\Form;
|
||||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Entity\EntityForm;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Form\SubformState;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\farm_quick\Plugin\QuickForm\ConfigurableQuickFormInterface;
|
||||
use Drupal\farm_quick\QuickFormInstanceManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Form that renders quick form configuration forms.
|
||||
*
|
||||
* @ingroup farm
|
||||
*/
|
||||
class ConfigureQuickForm extends QuickForm {
|
||||
class ConfigureQuickForm extends EntityForm {
|
||||
|
||||
/**
|
||||
* The entity being used by this form.
|
||||
*
|
||||
* @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface
|
||||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* The quick form instance manager.
|
||||
*
|
||||
* @var \Drupal\farm_quick\QuickFormInstanceManagerInterface
|
||||
*/
|
||||
protected $quickFormInstanceManager;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param \Drupal\farm_quick\QuickFormInstanceManagerInterface $quick_form_instance_manager
|
||||
* The quick form instance manager.
|
||||
*/
|
||||
public function __construct(QuickFormInstanceManagerInterface $quick_form_instance_manager) {
|
||||
$this->quickFormInstanceManager = $quick_form_instance_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBaseFormId() {
|
||||
return 'configure_quick_form';
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('quick_form.instance_manager'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the title of the quick form.
|
||||
*
|
||||
* @param string $id
|
||||
* @param string $quick_form
|
||||
* The quick form ID.
|
||||
*
|
||||
* @return string
|
||||
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
|
||||
* Quick form title.
|
||||
*/
|
||||
public function getTitle(string $id) {
|
||||
$quick_form_title = $this->quickFormInstanceManager->createInstance($id)->getLabel();
|
||||
public function getTitle(string $quick_form) {
|
||||
$quick_form_title = NULL;
|
||||
if ($quick_form = $this->getQuickFormInstance($quick_form)) {
|
||||
$quick_form_title = $quick_form->getLabel();
|
||||
}
|
||||
return $this->t('Configure @quick_form', ['@quick_form' => $quick_form_title]);
|
||||
}
|
||||
|
||||
|
@ -39,38 +74,44 @@ class ConfigureQuickForm extends QuickForm {
|
|||
*
|
||||
* @param \Drupal\Core\Session\AccountInterface $account
|
||||
* Run access checks for this account.
|
||||
* @param string $id
|
||||
* @param string|null $quick_form
|
||||
* The quick form ID.
|
||||
*
|
||||
* @return \Drupal\Core\Access\AccessResultInterface
|
||||
* The access result.
|
||||
*/
|
||||
public function access(AccountInterface $account, string $id) {
|
||||
public function access(AccountInterface $account, string $quick_form = NULL) {
|
||||
|
||||
// Get a quick form config entity.
|
||||
if ($quick_form !== NULL) {
|
||||
$quick_form = $this->getQuickFormInstance($quick_form);
|
||||
}
|
||||
|
||||
// Deny access if no quick form exists. This is the case with a quick form
|
||||
// ID that is not a valid quick form plugin ID.
|
||||
if ($quick_form === NULL) {
|
||||
return AccessResult::forbidden();
|
||||
}
|
||||
|
||||
// Deny access if the quick form plugin is not configurable.
|
||||
if (!$quick_form->getPlugin() instanceof ConfigurableQuickFormInterface) {
|
||||
return AccessResult::forbidden();
|
||||
}
|
||||
|
||||
// Check the configure quick forms permission.
|
||||
$configure_form_access = AccessResult::allowedIfHasPermissions($account, ['configure quick forms']);
|
||||
return parent::access($account, $id)->andIf($configure_form_access);
|
||||
return $quick_form->getPlugin()->access($account)->andIf($configure_form_access);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) {
|
||||
|
||||
// Save the quick form ID.
|
||||
$this->quickFormId = $id;
|
||||
|
||||
// Load the quick form's configuration form.
|
||||
$form = $this->quickFormInstanceManager->createInstance($id)->getPlugin()->buildConfigurationForm($form, $form_state);
|
||||
|
||||
// Add a submit button.
|
||||
$form['actions'] = [
|
||||
'#type' => 'actions',
|
||||
'#weight' => 1000,
|
||||
public function form(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::form($form, $form_state);
|
||||
$form['settings'] = [
|
||||
'#tree' => TRUE,
|
||||
];
|
||||
$form['actions']['submit'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save'),
|
||||
];
|
||||
|
||||
$form['settings'] = $this->entity->getPlugin()->buildConfigurationForm($form['settings'], SubformState::createForSubform($form['settings'], $form, $form_state));
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
@ -78,14 +119,40 @@ class ConfigureQuickForm extends QuickForm {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->quickFormInstanceManager->createInstance($this->quickFormId)->getPlugin()->validateConfigurationForm($form, $form_state);
|
||||
parent::validateForm($form, $form_state);
|
||||
$this->entity->getPlugin()->validateConfigurationForm($form['settings'], SubformState::createForSubform($form['settings'], $form, $form_state));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->quickFormInstanceManager->createInstance($this->quickFormId)->getPlugin()->submitConfigurationForm($form, $form_state);
|
||||
parent::submitForm($form, $form_state);
|
||||
$this->entity->getPlugin()->submitConfigurationForm($form['settings'], SubformState::createForSubform($form['settings'], $form, $form_state));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
|
||||
$entity = NULL;
|
||||
if ($route_match->getRawParameter($entity_type_id) !== NULL) {
|
||||
$entity = $this->getQuickFormInstance($route_match->getParameter($entity_type_id));
|
||||
}
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get a quick form instance.
|
||||
*
|
||||
* @param string $quick_form_id
|
||||
* The quick form ID.
|
||||
*
|
||||
* @return \Drupal\farm_quick\Entity\QuickFormInstanceInterface|null
|
||||
* The quick form instance or NULL if does not exist.
|
||||
*/
|
||||
protected function getQuickFormInstance(string $quick_form_id) {
|
||||
return $this->quickFormInstanceManager->createInstance($quick_form_id);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -62,9 +62,12 @@ class QuickFormTaskLink extends DeriverBase implements ContainerDeriverInterface
|
|||
|
||||
// If the quick form is configurable, add a link to the config form.
|
||||
if ($quick_form->getPlugin()->isConfigurable()) {
|
||||
$links[$route_name . '.configure'] = [
|
||||
$links["farm.quick.$id.configure"] = [
|
||||
'title' => $this->t('Configure'),
|
||||
'route_name' => $route_name . '.configure',
|
||||
'route_name' => 'farm_quick.configure',
|
||||
'route_parameters' => [
|
||||
'quick_form' => $id,
|
||||
],
|
||||
'base_route' => $route_name,
|
||||
'weight' => 100,
|
||||
] + $base_plugin_definition;
|
||||
|
|
|
@ -66,23 +66,6 @@ class QuickFormRoutes implements ContainerInjectionInterface {
|
|||
],
|
||||
);
|
||||
$route_collection->add("farm.quick.$id", $route);
|
||||
|
||||
// If the quick form is configurable, build a route for the configuration
|
||||
// form.
|
||||
if ($quick_form->getPlugin()->isConfigurable()) {
|
||||
$route = new Route(
|
||||
"/quick/$id/configure",
|
||||
[
|
||||
'_form' => ConfigureQuickForm::class,
|
||||
'_title_callback' => ConfigureQuickForm::class . '::getTitle',
|
||||
'id' => $id,
|
||||
],
|
||||
[
|
||||
'_custom_access' => ConfigureQuickForm::class . '::access',
|
||||
],
|
||||
);
|
||||
$route_collection->add("farm.quick.$id.configure", $route);
|
||||
}
|
||||
}
|
||||
return $route_collection;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue