View source
<?php
namespace Drupal\commerce\Element;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\FormElement;
class Conditions extends FormElement {
use CommerceElementTrait;
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#tree' => TRUE,
'#parent_entity_type' => NULL,
'#entity_types' => [],
'#default_value' => [],
'#title' => '',
'#process' => [
[
$class,
'attachElementSubmit',
],
[
$class,
'processConditions',
],
[
$class,
'processAjaxForm',
],
],
'#element_validate' => [
[
$class,
'validateElementSubmit',
],
],
'#commerce_element_submit' => [
[
$class,
'submitConditions',
],
],
'#theme_wrappers' => [
'container',
],
];
}
public static function processConditions(array &$element, FormStateInterface $form_state, array &$complete_form) {
if (empty($element['#parent_entity_type'])) {
throw new \InvalidArgumentException('The commerce_conditions element requires the #parent_entity_type property.');
}
if (empty($element['#entity_types'])) {
throw new \InvalidArgumentException('The commerce_conditions element requires the #entity_types property.');
}
if (!is_array($element['#entity_types'])) {
throw new \InvalidArgumentException('The commerce_conditions #entity_types property must be an array.');
}
if (!is_array($element['#default_value'])) {
throw new \InvalidArgumentException('The commerce_conditions #default_value property must be an array.');
}
$default_value = array_column($element['#default_value'], 'configuration', 'plugin');
$plugin_manager = \Drupal::service('plugin.manager.commerce_condition');
$definitions = $plugin_manager
->getFilteredDefinitions($element['#parent_entity_type'], $element['#entity_types']);
$grouped_definitions = [];
foreach ($definitions as $plugin_id => $definition) {
$category = $definition['category']
->getUntranslatedString();
$grouped_definitions[$category][$plugin_id] = $definition;
}
ksort($grouped_definitions);
$tab_group = implode('][', array_merge($element['#parents'], [
'conditions',
]));
$element['#attached']['library'][] = 'commerce/conditions';
$element['#after_build'] = [
[
get_called_class(),
'clearValues',
],
];
$element['#categories'] = [];
$render_vertical_tabs = count($grouped_definitions) > 1;
if ($render_vertical_tabs) {
$element['conditions'] = [
'#type' => 'vertical_tabs',
'#title' => $element['#title'],
];
}
else {
$element['conditions_title'] = [
'#type' => 'item',
'#title' => $element['#title'],
];
$element['conditions'] = [
'#type' => 'container',
];
}
foreach ($grouped_definitions as $category => $definitions) {
$category_id = preg_replace('/[^a-zA-Z\\-]/', '_', strtolower($category));
$category_label = (string) current($definitions)['category'];
$element['#categories'][] = $category_id;
$element[$category_id] = [
'#type' => $render_vertical_tabs ? 'details' : 'container',
'#title' => $category_label,
'#group' => $tab_group,
];
foreach ($definitions as $plugin_id => $definition) {
$category_parents = array_merge($element['#parents'], [
$category_id,
]);
$checkbox_parents = array_merge($category_parents, [
$plugin_id,
'enable',
]);
$checkbox_name = self::buildElementName($checkbox_parents);
$checkbox_input = NestedArray::getValue($form_state
->getUserInput(), $checkbox_parents);
$enabled = isset($default_value[$plugin_id]) || !empty($checkbox_input);
$ajax_wrapper_id = Html::getUniqueId('ajax-wrapper-' . $plugin_id);
if ($enabled && !isset($element['conditions']['#default_tab'])) {
$element['conditions']['#default_tab'] = 'edit-' . implode('-', $category_parents);
}
$element[$category_id][$plugin_id] = [
'#prefix' => '<div id="' . $ajax_wrapper_id . '">',
'#suffix' => '</div>',
];
$element[$category_id][$plugin_id]['enable'] = [
'#type' => 'checkbox',
'#title' => $definition['display_label'],
'#default_value' => $enabled,
'#attributes' => [
'class' => [
'enable',
],
],
'#ajax' => [
'callback' => [
get_called_class(),
'ajaxRefresh',
],
'wrapper' => $ajax_wrapper_id,
],
];
if ($enabled) {
$inline_form_manager = \Drupal::service('plugin.manager.commerce_inline_form');
$inline_form = $inline_form_manager
->createInstance('plugin_configuration', [
'plugin_type' => 'commerce_condition',
'plugin_id' => $plugin_id,
'plugin_configuration' => isset($default_value[$plugin_id]) ? $default_value[$plugin_id] : [],
'enforce_unique_parents' => FALSE,
]);
$element[$category_id][$plugin_id]['configuration']['#inline_form'] = $inline_form;
$element[$category_id][$plugin_id]['configuration']['#parents'] = array_merge($element['#parents'], [
$category_id,
$plugin_id,
'configuration',
]);
$element[$category_id][$plugin_id]['configuration'] = $inline_form
->buildInlineForm($element[$category_id][$plugin_id]['configuration'], $form_state);
$element[$category_id][$plugin_id]['configuration']['#states'] = [
'visible' => [
':input[name="' . $checkbox_name . '"]' => [
'checked' => TRUE,
],
],
];
$element[$category_id][$plugin_id]['configuration']['#enforce_unique_parents'] = FALSE;
}
}
}
return $element;
}
protected static function buildElementName(array $parents) {
$name = array_shift($parents);
$name .= '[' . implode('][', $parents) . ']';
return $name;
}
public static function ajaxRefresh(&$form, FormStateInterface $form_state) {
$element_parents = array_slice($form_state
->getTriggeringElement()['#array_parents'], 0, -1);
return NestedArray::getValue($form, $element_parents);
}
public static function submitConditions(array &$element, FormStateInterface $form_state) {
$value = [];
foreach ($element['#categories'] as $category_id) {
foreach (Element::getVisibleChildren($element[$category_id]) as $plugin_id) {
$plugin_element = $element[$category_id][$plugin_id];
$plugin_value = $form_state
->getValue($plugin_element['#parents']);
if ($plugin_value['enable']) {
$value[] = [
'plugin' => $plugin_id,
'configuration' => $plugin_value['configuration'],
];
}
}
}
$form_state
->setValueForElement($element, $value);
}
public static function clearValues(array $element, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
if (!$triggering_element) {
return $element;
}
$triggering_element_name = end($triggering_element['#parents']);
if ($triggering_element_name === 'enable' && !$triggering_element['#value']) {
$user_input =& $form_state
->getUserInput();
array_pop($triggering_element['#parents']);
$values = NestedArray::getValue($user_input, $triggering_element['#parents']);
unset($values['configuration']);
NestedArray::setValue($user_input, $triggering_element['#parents'], $values);
}
return $element;
}
}