Add a requiresEntity boolean to quick form plugins to optionally prevent creating default instances.

This commit is contained in:
Michael Stenta 2023-08-04 10:23:25 -04:00
parent 9811f7c7f7
commit 554afce229
4 changed files with 34 additions and 5 deletions

View File

@ -52,4 +52,11 @@ class QuickForm extends Plugin {
*/
public $permissions;
/**
* Require a quick form instance entity to instantiate.
*
* @var bool
*/
public $requiresEntity;
}

View File

@ -105,6 +105,13 @@ class QuickFormBase extends PluginBase implements QuickFormInterface, ContainerF
return $this->pluginDefinition['helpText'] ?? '';
}
/**
* {@inheritdoc}
*/
public function requiresEntity() {
return $this->pluginDefinition['requiresEntity'] ?? FALSE;
}
/**
* {@inheritdoc}
*/

View File

@ -71,6 +71,14 @@ interface QuickFormInterface extends FormInterface {
*/
public function getPermissions();
/**
* Whether the plugin requires a quick form instance configuration entity.
*
* @return bool
* Boolean.
*/
public function requiresEntity();
/**
* Checks access for the quick form.
*

View File

@ -64,9 +64,10 @@ class QuickFormInstanceManager implements QuickFormInstanceManagerInterface {
$instances += $entities;
}
// If there are no config entities, create a new (unsaved) config entity
// with default values from the plugin.
else {
// Or, if this plugin does not require a quick form instance configuration
// entity, then add a new (unsaved) config entity with default values from
// the plugin.
elseif (empty($plugin['requiresEntity'])) {
$instances[$plugin['id']] = QuickFormInstance::create(['id' => $plugin['id'], 'plugin' => $plugin['id']]);
}
}
@ -86,9 +87,15 @@ class QuickFormInstanceManager implements QuickFormInstanceManagerInterface {
return $entity;
}
// Otherwise, create a new (unsaved) config entity with default values from
// Or, if this plugin does not require a quick form instance configuration
// entity, then add a new (unsaved) config entity with default values from
// the plugin.
return QuickFormInstance::create(['id' => $id, 'plugin' => $id]);
elseif (empty($plugin['requiresEntity'])) {
return QuickFormInstance::create(['id' => $id, 'plugin' => $id]);
}
// No quick form could be instantiated.
return NULL;
}
}