You are here

class PluginConfiguration in Commerce Core 8.2

Same name in this branch
  1. 8.2 src/Element/PluginConfiguration.php \Drupal\commerce\Element\PluginConfiguration
  2. 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

Expanded class hierarchy of PluginConfiguration

File

src/Element/PluginConfiguration.php, line 29

Namespace

Drupal\commerce\Element
View 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

Namesort descending Modifiers Type Description Overrides
CommerceElementTrait::attachElementSubmit public static function Attaches the #commerce_element_submit functionality.
CommerceElementTrait::doExecuteSubmitHandlers protected static function Calls the #commerce_element_submit callbacks recursively.
CommerceElementTrait::executeElementSubmitHandlers public static function Submits elements by calling their #commerce_element_submit callbacks.
CommerceElementTrait::shouldExecuteElementSubmit protected static function Checks whether #commerce_element_submit callbacks should be executed.
CommerceElementTrait::validateElementSubmit public static function Confirms that #commerce_element_submit handlers can be run.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
FormElement::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementInterface::valueCallback 15
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
PluginConfiguration::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
PluginConfiguration::processPluginConfiguration public static function Processes the plugin configuration form element.
PluginConfiguration::submitPluginConfiguration public static function Submits the plugin configuration.
PluginConfiguration::validatePluginConfiguration public static function Validates the plugin configuration.
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.