Refactor quick form route building to a single route
This commit is contained in:
parent
8903ff44bc
commit
3505e80124
|
@ -1,2 +1,9 @@
|
||||||
farm.quick:
|
farm_quick.quick_form:
|
||||||
deriver: Drupal\farm_quick\Plugin\Derivative\QuickFormTaskLink
|
title: Quick Form
|
||||||
|
route_name: farm_quick.quick_form
|
||||||
|
base_route: farm_quick.quick_form
|
||||||
|
|
||||||
|
farm_quick.configure:
|
||||||
|
title: Configure
|
||||||
|
route_name: farm_quick.configure
|
||||||
|
base_route: farm_quick.quick_form
|
||||||
|
|
|
@ -20,12 +20,9 @@ function farm_quick_help($route_name, RouteMatchInterface $route_match) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load help text for individual quick forms.
|
// Load help text for individual quick forms.
|
||||||
if (strpos($route_name, 'farm.quick.') === 0) {
|
if ($route_name == 'farm_quick.quick_form') {
|
||||||
$quick_form_id = $route_match->getParameter('id');
|
if (($quick_form_id = $route_match->getParameter('quick_form')) && $quick_form = \Drupal::service('quick_form.instance_manager')->createInstance($quick_form_id)) {
|
||||||
if ($route_name == 'farm.quick.' . $quick_form_id) {
|
if ($help_text = $quick_form->getHelpText()) {
|
||||||
$quick_form = \Drupal::service('quick_form.instance_manager')->createInstance($quick_form_id);
|
|
||||||
$help_text = $quick_form->getHelpText();
|
|
||||||
if (!empty($help_text)) {
|
|
||||||
$output .= '<p>' . $help_text . '</p>';
|
$output .= '<p>' . $help_text . '</p>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,18 @@ farm.quick:
|
||||||
requirements:
|
requirements:
|
||||||
_permission: 'view quick forms index'
|
_permission: 'view quick forms index'
|
||||||
|
|
||||||
|
farm_quick.quick_form:
|
||||||
|
path: /quick/{quick_form}
|
||||||
|
defaults:
|
||||||
|
_form: \Drupal\farm_quick\Form\QuickForm
|
||||||
|
_title_callback: \Drupal\farm_quick\Form\QuickForm::getTitle
|
||||||
|
requirements:
|
||||||
|
_custom_access: \Drupal\farm_quick\Form\QuickForm::access
|
||||||
|
options:
|
||||||
|
parameters:
|
||||||
|
quick_form:
|
||||||
|
type: string
|
||||||
|
|
||||||
farm_quick.configure:
|
farm_quick.configure:
|
||||||
path: /quick/{quick_form}/configure
|
path: /quick/{quick_form}/configure
|
||||||
defaults:
|
defaults:
|
||||||
|
@ -17,6 +29,3 @@ farm_quick.configure:
|
||||||
parameters:
|
parameters:
|
||||||
quick_form:
|
quick_form:
|
||||||
type: string
|
type: string
|
||||||
|
|
||||||
route_callbacks:
|
|
||||||
- '\Drupal\farm_quick\Routing\QuickFormRoutes::routes'
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ class QuickFormController extends ControllerBase {
|
||||||
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
||||||
$items = [];
|
$items = [];
|
||||||
foreach ($quick_forms as $id => $quick_form) {
|
foreach ($quick_forms as $id => $quick_form) {
|
||||||
$url = Url::fromRoute('farm.quick.' . $id);
|
$url = Url::fromRoute('farm_quick.quick_form', ['quick_form' => $id]);
|
||||||
if ($url->access()) {
|
if ($url->access()) {
|
||||||
$items[] = [
|
$items[] = [
|
||||||
'title' => $quick_form->getLabel(),
|
'title' => $quick_form->getLabel(),
|
||||||
|
|
|
@ -61,7 +61,7 @@ class QuickForm extends FormBase implements BaseFormIdInterface {
|
||||||
*/
|
*/
|
||||||
public function getFormId() {
|
public function getFormId() {
|
||||||
$form_id = $this->getBaseFormId();
|
$form_id = $this->getBaseFormId();
|
||||||
$id = $this->getRouteMatch()->getParameter('id');
|
$id = $this->getRouteMatch()->getParameter('quick_form');
|
||||||
if (!is_null($id)) {
|
if (!is_null($id)) {
|
||||||
$form_id .= '_' . $this->quickFormInstanceManager->createInstance($id)->getPlugin()->getFormId();
|
$form_id .= '_' . $this->quickFormInstanceManager->createInstance($id)->getPlugin()->getFormId();
|
||||||
}
|
}
|
||||||
|
@ -71,14 +71,14 @@ class QuickForm extends FormBase implements BaseFormIdInterface {
|
||||||
/**
|
/**
|
||||||
* Get the title of the quick form.
|
* Get the title of the quick form.
|
||||||
*
|
*
|
||||||
* @param string $id
|
* @param string $quick_form
|
||||||
* The quick form ID.
|
* The quick form ID.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
* Quick form title.
|
* Quick form title.
|
||||||
*/
|
*/
|
||||||
public function getTitle(string $id) {
|
public function getTitle(string $quick_form) {
|
||||||
return $this->quickFormInstanceManager->createInstance($id)->getLabel();
|
return $this->quickFormInstanceManager->createInstance($quick_form)->getLabel();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -86,26 +86,26 @@ class QuickForm extends FormBase implements BaseFormIdInterface {
|
||||||
*
|
*
|
||||||
* @param \Drupal\Core\Session\AccountInterface $account
|
* @param \Drupal\Core\Session\AccountInterface $account
|
||||||
* Run access checks for this account.
|
* Run access checks for this account.
|
||||||
* @param string $id
|
* @param string $quick_form
|
||||||
* The quick form ID.
|
* The quick form ID.
|
||||||
*
|
*
|
||||||
* @return \Drupal\Core\Access\AccessResultInterface
|
* @return \Drupal\Core\Access\AccessResultInterface
|
||||||
* The access result.
|
* The access result.
|
||||||
*/
|
*/
|
||||||
public function access(AccountInterface $account, string $id) {
|
public function access(AccountInterface $account, string $quick_form) {
|
||||||
return $this->quickFormInstanceManager->createInstance($id)->getPlugin()->access($account);
|
return $this->quickFormInstanceManager->createInstance($quick_form)->getPlugin()->access($account);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) {
|
public function buildForm(array $form, FormStateInterface $form_state, $quick_form = NULL) {
|
||||||
|
|
||||||
// Save the quick form ID.
|
// Save the quick form ID.
|
||||||
$this->quickFormId = $id;
|
$this->quickFormId = $quick_form;
|
||||||
|
|
||||||
// Load the quick form.
|
// Load the quick form.
|
||||||
$form = $this->quickFormInstanceManager->createInstance($id)->getPlugin()->buildForm($form, $form_state);
|
$form = $this->quickFormInstanceManager->createInstance($quick_form)->getPlugin()->buildForm($form, $form_state);
|
||||||
|
|
||||||
// Add a submit button, if one wasn't provided.
|
// Add a submit button, if one wasn't provided.
|
||||||
if (empty($form['actions']['submit'])) {
|
if (empty($form['actions']['submit'])) {
|
||||||
|
|
|
@ -63,7 +63,10 @@ class QuickFormMenuLink extends DeriverBase implements ContainerDeriverInterface
|
||||||
$links[$route_id] = [
|
$links[$route_id] = [
|
||||||
'title' => $quick_form->getLabel(),
|
'title' => $quick_form->getLabel(),
|
||||||
'parent' => 'farm.quick:farm.quick',
|
'parent' => 'farm.quick:farm.quick',
|
||||||
'route_name' => $route_id,
|
'route_name' => 'farm_quick.quick_form',
|
||||||
|
'route_parameters' => [
|
||||||
|
'quick_form' => $id,
|
||||||
|
],
|
||||||
] + $base_plugin_definition;
|
] + $base_plugin_definition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,80 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Drupal\farm_quick\Plugin\Derivative;
|
|
||||||
|
|
||||||
use Drupal\Component\Plugin\Derivative\DeriverBase;
|
|
||||||
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
|
|
||||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
|
||||||
use Drupal\farm_quick\QuickFormInstanceManagerInterface;
|
|
||||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides task links for farmOS Quick Forms.
|
|
||||||
*/
|
|
||||||
class QuickFormTaskLink extends DeriverBase implements ContainerDeriverInterface {
|
|
||||||
|
|
||||||
use StringTranslationTrait;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The quick form instance manager.
|
|
||||||
*
|
|
||||||
* @var \Drupal\farm_quick\QuickFormInstanceManagerInterface
|
|
||||||
*/
|
|
||||||
protected $quickFormInstanceManager;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* QuickFormTaskLink constructor.
|
|
||||||
*
|
|
||||||
* @param \Drupal\farm_quick\QuickFormInstanceManagerInterface $quick_form_instance_manager
|
|
||||||
* The quick form plugin manager.
|
|
||||||
*/
|
|
||||||
public function __construct(QuickFormInstanceManagerInterface $quick_form_instance_manager) {
|
|
||||||
$this->quickFormInstanceManager = $quick_form_instance_manager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public static function create(ContainerInterface $container, $base_plugin_id) {
|
|
||||||
return new static(
|
|
||||||
$container->get('quick_form.instance_manager')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getDerivativeDefinitions($base_plugin_definition) {
|
|
||||||
$links = [];
|
|
||||||
|
|
||||||
// Load quick forms.
|
|
||||||
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
|
||||||
|
|
||||||
// Add links for each quick form.
|
|
||||||
foreach ($quick_forms as $id => $quick_form) {
|
|
||||||
$route_name = 'farm.quick.' . $id;
|
|
||||||
$links[$route_name] = [
|
|
||||||
'title' => $this->t('Quick form'),
|
|
||||||
'route_name' => $route_name,
|
|
||||||
'base_route' => $route_name,
|
|
||||||
'weight' => 0,
|
|
||||||
] + $base_plugin_definition;
|
|
||||||
|
|
||||||
// If the quick form is configurable, add a link to the config form.
|
|
||||||
if ($quick_form->getPlugin()->isConfigurable()) {
|
|
||||||
$links["farm.quick.$id.configure"] = [
|
|
||||||
'title' => $this->t('Configure'),
|
|
||||||
'route_name' => 'farm_quick.configure',
|
|
||||||
'route_parameters' => [
|
|
||||||
'quick_form' => $id,
|
|
||||||
],
|
|
||||||
'base_route' => $route_name,
|
|
||||||
'weight' => 100,
|
|
||||||
] + $base_plugin_definition;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $links;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,73 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Drupal\farm_quick\Routing;
|
|
||||||
|
|
||||||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
|
||||||
use Drupal\farm_quick\Form\ConfigureQuickForm;
|
|
||||||
use Drupal\farm_quick\Form\QuickForm;
|
|
||||||
use Drupal\farm_quick\QuickFormInstanceManagerInterface;
|
|
||||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
||||||
use Symfony\Component\Routing\Route;
|
|
||||||
use Symfony\Component\Routing\RouteCollection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Defines quick form routes.
|
|
||||||
*/
|
|
||||||
class QuickFormRoutes implements ContainerInjectionInterface {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The quick form instance manager.
|
|
||||||
*
|
|
||||||
* @var \Drupal\farm_quick\QuickFormInstanceManagerInterface
|
|
||||||
*/
|
|
||||||
protected $quickFormInstanceManager;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a QuickFormRoutes object.
|
|
||||||
*
|
|
||||||
* @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 static function create(ContainerInterface $container) {
|
|
||||||
return new static(
|
|
||||||
$container->get('quick_form.instance_manager'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides routes for quick forms.
|
|
||||||
*
|
|
||||||
* @return \Symfony\Component\Routing\RouteCollection
|
|
||||||
* Returns a route collection.
|
|
||||||
*/
|
|
||||||
public function routes(): RouteCollection {
|
|
||||||
$route_collection = new RouteCollection();
|
|
||||||
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface[] $quick_forms */
|
|
||||||
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
|
||||||
foreach ($quick_forms as $id => $quick_form) {
|
|
||||||
|
|
||||||
// Build a route for the quick form.
|
|
||||||
$route = new Route(
|
|
||||||
"/quick/$id",
|
|
||||||
[
|
|
||||||
'_form' => QuickForm::class,
|
|
||||||
'_title_callback' => QuickForm::class . '::getTitle',
|
|
||||||
'id' => $id,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'_custom_access' => QuickForm::class . '::access',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
$route_collection->add("farm.quick.$id", $route);
|
|
||||||
}
|
|
||||||
return $route_collection;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue