3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Provide a configuration entity type for quick form instances.

This commit is contained in:
Michael Stenta 2023-03-15 11:37:32 -04:00
parent 78943d239d
commit bce6654428
5 changed files with 290 additions and 0 deletions

View file

@ -0,0 +1,4 @@
# Quick form settings data type for quick form configuration entity.
quick_form_settings:
type: mapping
label: 'Quick form settings'

View file

@ -0,0 +1,25 @@
# Schema for quick form instance configuration entities.
farm_quick.quick_form.*:
type: config_entity
label: 'Quick form instance'
mapping:
id:
type: string
label: 'Machine-readable name'
plugin:
type: string
label: 'Plugin'
label:
type: label
label: 'Label'
description:
type: text
label: 'Description'
helpText:
type: text
label: 'Help text'
settings:
type: farm_quick.settings.[%parent.plugin]
farm_quick.settings.*:
type: quick_form_settings

View file

@ -0,0 +1,153 @@
<?php
namespace Drupal\farm_quick\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\farm_quick\QuickFormPluginCollection;
/**
* Defines the quick form instance config entity.
*
* @ConfigEntityType(
* id = "quick_form",
* label = @Translation("Quick form"),
* label_collection = @Translation("Quick forms"),
* label_singular = @Translation("quick form"),
* label_plural = @Translation("quick forms"),
* label_count = @PluralTranslation(
* singular = "@count quick form",
* plural = "@count quick forms",
* ),
* handlers = {
* },
* entity_keys = {
* "id" = "id",
* "status" = "status"
* },
* config_export = {
* "id",
* "plugin",
* "label",
* "description",
* "helpText",
* "settings",
* },
* )
*/
class QuickFormInstance extends ConfigEntityBase implements QuickFormInstanceInterface, EntityWithPluginCollectionInterface {
/**
* The ID of the quick form instance.
*
* @var string
*/
protected $id;
/**
* The plugin instance ID.
*
* @var string
*/
protected $plugin;
/**
* The plugin collection that holds the quick form plugin for this entity.
*
* @var \Drupal\farm_quick\QuickFormPluginCollection
*/
protected $pluginCollection;
/**
* The quick form label.
*
* @var string
*/
protected $label;
/**
* A brief description of the quick form.
*
* @var string
*/
protected $description;
/**
* Help text for the quick form.
*
* @var string
*/
protected $helpText;
/**
* The plugin instance settings.
*
* @var array
*/
protected $settings = [];
/**
* {@inheritdoc}
*/
public function getPlugin() {
return $this->getPluginCollection()->get($this->plugin);
}
/**
* Encapsulates the creation of the farm_quick's plugin collection.
*
* @return \Drupal\Component\Plugin\LazyPluginCollection
* The block's plugin collection.
*/
protected function getPluginCollection() {
if (!$this->pluginCollection) {
$this->pluginCollection = new QuickFormPluginCollection(\Drupal::service('plugin.manager.quick_form'), $this->plugin, $this->get('settings'), $this->id());
}
return $this->pluginCollection;
}
/**
* {@inheritdoc}
*/
public function getPluginCollections() {
return [
'settings' => $this->getPluginCollection(),
];
}
/**
* {@inheritdoc}
*/
public function getPluginId() {
return $this->plugin;
}
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->label ?? $this->getPlugin()->getLabel();
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->description ?? $this->getPlugin()->getDescription();
}
/**
* {@inheritdoc}
*/
public function getHelpText() {
return $this->helpText ?? $this->getPlugin()->getHelpText();
}
/**
* {@inheritdoc}
*/
public function getSettings() {
return $this->settings;
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace Drupal\farm_quick\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface for defining quick form instance config entities.
*/
interface QuickFormInstanceInterface extends ConfigEntityInterface {
/**
* Returns the plugin instance.
*
* @return \Drupal\farm_quick\Plugin\QuickForm\QuickFormInterface
* The plugin instance for this quick form.
*/
public function getPlugin();
/**
* Returns the plugin ID.
*
* @return string
* The plugin ID for this quick form.
*/
public function getPluginId();
/**
* Returns the quick form label.
*
* @return string
* The label for this quick form.
*/
public function getLabel();
/**
* Returns the quick form description.
*
* @return string
* The description for this quick form.
*/
public function getDescription();
/**
* Returns the quick form help text.
*
* @return string
* The help text for this quick form.
*/
public function getHelpText();
/**
* Returns the quick form settings.
*
* @return array
* An associative array of settings.
*/
public function getSettings();
}

View file

@ -0,0 +1,48 @@
<?php
namespace Drupal\farm_quick;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
/**
* Provides a collection of quick form plugins.
*/
class QuickFormPluginCollection extends DefaultSingleLazyPluginCollection {
/**
* The quick form ID this plugin collection belongs to.
*
* @var string
*/
protected $quickFormId;
/**
* Constructs a new QuickFormPluginCollection.
*
* @param \Drupal\Component\Plugin\PluginManagerInterface $manager
* The manager to be used for instantiating plugins.
* @param string $instance_id
* The ID of the plugin instance.
* @param array $configuration
* An array of configuration.
* @param string $quick_form_id
* The unique ID of the quick form entity using this plugin.
*/
public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration, $quick_form_id) {
parent::__construct($manager, $instance_id, $configuration);
$this->quickFormId = $quick_form_id;
}
/**
* {@inheritdoc}
*/
protected function initializePlugin($instance_id) {
if (!$instance_id) {
throw new PluginException("The quick form '{$this->quickFormId}' did not specify a plugin.");
}
parent::initializePlugin($instance_id);
}
}