class PluginConfiguration in Commerce Core 8.2
Same name in this branch
- 8.2 src/Element/PluginConfiguration.php \Drupal\commerce\Element\PluginConfiguration
- 8.2 src/Plugin/Commerce/InlineForm/PluginConfiguration.php \Drupal\commerce\Plugin\Commerce\InlineForm\PluginConfiguration
Provides a form element for configuring plugins.
Usage example:
$form['configuration'] = [
'#type' => 'commerce_plugin_configuration',
'#plugin_type' => 'commerce_promotion',
'#plugin_id' => 'order_total_price',
'#default_value' => [
'operator' => '<',
'amount' => [
'number' => '10.00',
'currency_code' => 'USD',
],
],
];
Plugin annotation
@FormElement("commerce_plugin_configuration");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\commerce\Element\PluginConfiguration uses CommerceElementTrait
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of PluginConfiguration
File
- src/
Element/ PluginConfiguration.php, line 29
Namespace
Drupal\commerce\ElementView source
class PluginConfiguration extends FormElement {
use CommerceElementTrait;
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#tree' => TRUE,
'#plugin_type' => NULL,
'#plugin_id' => NULL,
'#enforce_unique_parents' => TRUE,
'#default_value' => [],
'#process' => [
[
$class,
'attachElementSubmit',
],
[
$class,
'processPluginConfiguration',
],
[
$class,
'processAjaxForm',
],
],
'#element_validate' => [
[
$class,
'validateElementSubmit',
],
[
$class,
'validatePluginConfiguration',
],
],
'#commerce_element_submit' => [
[
$class,
'submitPluginConfiguration',
],
],
'#theme_wrappers' => [
'container',
],
];
}
/**
* Processes the plugin configuration form element.
*
* @param array $element
* The form element to process.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The processed element.
*
* @throws \InvalidArgumentException
* Thrown for missing #plugin_type or malformed #default_value properties.
*/
public static function processPluginConfiguration(array &$element, FormStateInterface $form_state, array &$complete_form) {
if (empty($element['#plugin_type'])) {
throw new \InvalidArgumentException('The commerce_plugin_configuration #plugin_type property is required.');
}
if (!is_array($element['#default_value'])) {
throw new \InvalidArgumentException('The commerce_plugin_configuration #default_value must be an array.');
}
if (empty($element['#plugin_id'])) {
// A plugin hasn't been selected yet.
return $element;
}
/** @var \Drupal\Core\Executable\ExecutableManagerInterface $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.' . $element['#plugin_type']);
/** @var \Drupal\Core\Plugin\PluginFormInterface $plugin */
$plugin = $plugin_manager
->createInstance($element['#plugin_id'], $element['#default_value']);
$element['form'] = [];
if (!empty($element['#enforce_unique_parents'])) {
// NestedArray::setValue() crashes when switching between two plugins
// that share a configuration element of the same name, but not the
// same type (e.g. "amount" of type number/commerce_price).
// Configuration must be keyed by plugin ID in $form_state to prevent
// that, either on this level, or in a parent form element.
$element['form']['#parents'] = array_merge($element['#parents'], [
$element['#plugin_id'],
]);
}
$element['form'] = $plugin
->buildConfigurationForm($element['form'], $form_state);
return $element;
}
/**
* Validates the plugin configuration.
*
* @param array $element
* An associative array containing the properties of the element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*/
public static function validatePluginConfiguration(array &$element, FormStateInterface $form_state, array &$complete_form) {
if (!empty($element['#plugin_id'])) {
/** @var \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.' . $element['#plugin_type']);
$plugin = $plugin_manager
->createInstance($element['#plugin_id'], $element['#default_value']);
$plugin
->validateConfigurationForm($element['form'], $form_state);
}
}
/**
* Submits the plugin configuration.
*
* @param array $element
* An associative array containing the properties of the element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public static function submitPluginConfiguration(array &$element, FormStateInterface $form_state) {
if (!empty($element['#plugin_id'])) {
/** @var \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.' . $element['#plugin_type']);
$plugin = $plugin_manager
->createInstance($element['#plugin_id'], $element['#default_value']);
$plugin
->submitConfigurationForm($element['form'], $form_state);
$form_state
->setValueForElement($element, $plugin
->getConfiguration());
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommerceElementTrait:: |
public static | function | Attaches the #commerce_element_submit functionality. | |
CommerceElementTrait:: |
protected static | function | Calls the #commerce_element_submit callbacks recursively. | |
CommerceElementTrait:: |
public static | function | Submits elements by calling their #commerce_element_submit callbacks. | |
CommerceElementTrait:: |
protected static | function | Checks whether #commerce_element_submit callbacks should be executed. | |
CommerceElementTrait:: |
public static | function | Confirms that #commerce_element_submit handlers can be run. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
FormElement:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElementInterface:: |
15 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
PluginConfiguration:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
PluginConfiguration:: |
public static | function | Processes the plugin configuration form element. | |
PluginConfiguration:: |
public static | function | Submits the plugin configuration. | |
PluginConfiguration:: |
public static | function | Validates the plugin configuration. | |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |