Refactor QuickFormInstanceManager to return config entities.
This commit is contained in:
parent
6e34a09a9f
commit
11c6e5be7e
|
@ -48,7 +48,7 @@ class QuickFormController extends ControllerBase {
|
|||
* Returns a render array.
|
||||
*/
|
||||
public function index(): array {
|
||||
/** @var \Drupal\farm_quick\Plugin\QuickForm\QuickFormInterface[] $quick_forms */
|
||||
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface[] $quick_forms */
|
||||
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
||||
$items = [];
|
||||
foreach ($quick_forms as $id => $quick_form) {
|
||||
|
|
|
@ -63,7 +63,7 @@ class QuickForm extends FormBase implements BaseFormIdInterface {
|
|||
$form_id = $this->getBaseFormId();
|
||||
$id = $this->getRouteMatch()->getParameter('id');
|
||||
if (!is_null($id)) {
|
||||
$form_id .= '_' . $this->quickFormInstanceManager->createInstance($id)->getFormId();
|
||||
$form_id .= '_' . $this->quickFormInstanceManager->createInstance($id)->getPlugin()->getFormId();
|
||||
}
|
||||
return $form_id;
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ class QuickForm extends FormBase implements BaseFormIdInterface {
|
|||
* The access result.
|
||||
*/
|
||||
public function access(AccountInterface $account, string $id) {
|
||||
return $this->quickFormInstanceManager->createInstance($id)->access($account);
|
||||
return $this->quickFormInstanceManager->createInstance($id)->getPlugin()->access($account);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,9 +105,7 @@ class QuickForm extends FormBase implements BaseFormIdInterface {
|
|||
$this->quickFormId = $id;
|
||||
|
||||
// Load the quick form.
|
||||
/** @var \Drupal\farm_quick\Plugin\QuickForm\QuickFormInterface $quick_form */
|
||||
$quick_form = $this->quickFormInstanceManager->createInstance($id);
|
||||
$form = $quick_form->buildForm($form, $form_state);
|
||||
$form = $this->quickFormInstanceManager->createInstance($id)->getPlugin()->buildForm($form, $form_state);
|
||||
|
||||
// Add a submit button, if one wasn't provided.
|
||||
if (empty($form['actions']['submit'])) {
|
||||
|
@ -128,14 +126,14 @@ class QuickForm extends FormBase implements BaseFormIdInterface {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->quickFormInstanceManager->createInstance($this->quickFormId)->validateForm($form, $form_state);
|
||||
$this->quickFormInstanceManager->createInstance($this->quickFormId)->getPlugin()->validateForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->quickFormInstanceManager->createInstance($this->quickFormId)->submitForm($form, $form_state);
|
||||
$this->quickFormInstanceManager->createInstance($this->quickFormId)->getPlugin()->submitForm($form, $form_state);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ class QuickFormMenuLink extends DeriverBase implements ContainerDeriverInterface
|
|||
$links = [];
|
||||
|
||||
// Load quick forms.
|
||||
/** @var \Drupal\farm_quick\Plugin\QuickForm\QuickFormInterface[] $quick_forms */
|
||||
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface[] $quick_forms */
|
||||
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
||||
|
||||
// Add a top level menu parent.
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\farm_quick;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\farm_quick\Entity\QuickFormInstance;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
|
@ -56,8 +57,17 @@ class QuickFormInstanceManager implements QuickFormInstanceManagerInterface {
|
|||
// Iterate through quick form plugin definitions.
|
||||
foreach ($this->quickFormPluginManager->getDefinitions() as $plugin) {
|
||||
|
||||
// Instantiate a quick form for the plugin.
|
||||
$instances[$plugin['id']] = $this->quickFormPluginManager->createInstance($plugin['id']);
|
||||
// Load quick form instance configuration entities for this plugin.
|
||||
$entities = $this->entityTypeManager->getStorage('quick_form')->loadByProperties(['plugin' => $plugin['id']]);
|
||||
if (!empty($entities)) {
|
||||
$instances += $entities;
|
||||
}
|
||||
|
||||
// If there are no config entities, create a new (unsaved) config entity
|
||||
// with default values from the plugin.
|
||||
else {
|
||||
$instances[$plugin['id']] = QuickFormInstance::create(['id' => $plugin['id'], 'plugin' => $plugin['id']]);
|
||||
}
|
||||
}
|
||||
|
||||
return $instances;
|
||||
|
@ -67,7 +77,16 @@ class QuickFormInstanceManager implements QuickFormInstanceManagerInterface {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function createInstance($id) {
|
||||
return $this->quickFormPluginManager->createInstance($id);
|
||||
|
||||
// First attempt to load a quick form instance config entity.
|
||||
$entity = $this->entityTypeManager->getStorage('quick_form')->load($id);
|
||||
if (!empty($entity)) {
|
||||
return $entity;
|
||||
}
|
||||
|
||||
// Otherwise, create a new (unsaved) config entity with default values from
|
||||
// the plugin.
|
||||
return QuickFormInstance::create(['id' => $id, 'plugin' => $id]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,10 +10,10 @@ interface QuickFormInstanceManagerInterface {
|
|||
/**
|
||||
* Get all quick form instances.
|
||||
*
|
||||
* @return array
|
||||
* @return \Drupal\farm_quick\Entity\QuickFormInstanceInterface[]
|
||||
* An array of quick form instances.
|
||||
*/
|
||||
public function getInstances(): array;
|
||||
public function getInstances();
|
||||
|
||||
/**
|
||||
* Create an instance of a quick form.
|
||||
|
@ -21,7 +21,7 @@ interface QuickFormInstanceManagerInterface {
|
|||
* @param string $id
|
||||
* The quick form ID.
|
||||
*
|
||||
* @return \Drupal\farm_quick\Plugin\QuickForm\QuickFormInterface|null
|
||||
* @return \Drupal\farm_quick\Entity\QuickFormInstanceInterface|null
|
||||
* Returns an instantiated quick form object.
|
||||
*/
|
||||
public function createInstance($id);
|
||||
|
|
|
@ -48,7 +48,7 @@ class QuickFormRoutes implements ContainerInjectionInterface {
|
|||
*/
|
||||
public function routes(): RouteCollection {
|
||||
$route_collection = new RouteCollection();
|
||||
/** @var \Drupal\farm_quick\Plugin\QuickForm\QuickFormInterface[] $quick_forms */
|
||||
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface[] $quick_forms */
|
||||
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
||||
foreach ($quick_forms as $id => $quick_form) {
|
||||
$route = new Route(
|
||||
|
|
|
@ -62,7 +62,7 @@ class QuickFormTest extends KernelTestBase {
|
|||
public function testQuickFormDiscovery() {
|
||||
|
||||
// Load quick forms.
|
||||
/** @var \Drupal\farm_quick\Plugin\QuickForm\QuickFormInterface[] $quick_forms */
|
||||
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface[] $quick_forms */
|
||||
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
||||
|
||||
// Confirm that one quick form was discovered.
|
||||
|
@ -72,7 +72,7 @@ class QuickFormTest extends KernelTestBase {
|
|||
$this->assertEquals('Test quick form', $quick_forms['test']->getLabel());
|
||||
$this->assertEquals('Test quick form description.', $quick_forms['test']->getDescription());
|
||||
$this->assertEquals('Test quick form help text.', $quick_forms['test']->getHelpText());
|
||||
$this->assertEquals(['create test log'], $quick_forms['test']->getPermissions());
|
||||
$this->assertEquals(['create test log'], $quick_forms['test']->getPlugin()->getPermissions());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue