You are here

public static function PluginConfiguration::processPluginConfiguration in Commerce Core 8.2

Processes the plugin configuration form element.

Parameters

array $element: The form element to process.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form structure.

Return value

array The processed element.

Throws

\InvalidArgumentException Thrown for missing #plugin_type or malformed #default_value properties.

File

src/Element/PluginConfiguration.php, line 78

Class

PluginConfiguration
Provides a form element for configuring plugins.

Namespace

Drupal\commerce\Element

Code

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;
}