View source
<?php
namespace Drupal\pardot\Plugin\PardotFormMapFormatterPlugin;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\pardot\Plugin\PardotFormMapFormatterPluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\pardot\PluginSubFormHelpers;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_ui\Form\WebformUiElementAddForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormState;
class WebformSubmissionValue extends PardotFormMapFormatterPluginBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $pluginManagerWebformElement;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
$instance->entityTypeManager = $container
->get('entity_type.manager');
$instance->pluginManagerWebformElement = $container
->get('plugin.manager.webform.element');
return $instance;
}
public function getPluginId() {
return 'webform_submission_value';
}
public function getPluginDefinition() {
return $this->pluginDefinition;
}
public function defaultConfiguration() {
return [
'element' => '',
'single_format' => 'value',
'multiple_format' => 'comma',
'hash' => rand(),
'disallowed_field_types' => [
'webform_actions',
],
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$elements = $this
->getWebformElements();
$wrapper_hash = $this->configuration['hash'];
$element_type = $this->configuration['element'] ?? '';
if ($form_state
->get("element_type__{$wrapper_hash}")) {
$element_type_state = $form_state
->get("element_type__{$wrapper_hash}");
}
else {
$element_type_state = $element_type;
}
$form['settings_container'] = [
'#type' => 'container',
'#prefix' => "<div id='webform_submission_value_wrapper__{$wrapper_hash}'>",
'#suffix' => '</div>',
];
$form['settings_container']['element'] = [
'#type' => 'select',
'#title' => 'Element',
'#default_value' => $element_type_state,
'#options' => $this
->getElementCollection(),
'#required' => TRUE,
'#ajax' => [
'event' => 'change',
'callback' => [
$this,
'updateFieldMap',
],
'wrapper' => "webform_submission_value_wrapper__{$wrapper_hash}",
'trigger_as' => [
'name' => "element_type_select__{$wrapper_hash}",
],
],
];
$form['settings_container']['element_type_select'] = [
'#submit' => [
[
$this,
'pluginTypeChangeSubmit',
],
],
'#type' => 'submit',
'#attributes' => [
'class' => [
'visually-hidden',
],
],
'#wrapper_hash' => $wrapper_hash,
'#ajax' => [
'callback' => [
$this,
'ajaxChangeFieldMap',
],
'wrapper' => "webform_submission_value_wrapper__{$wrapper_hash}",
'effect' => 'fade',
],
'#limit_validation_errors' => [],
'#name' => "element_type_select__{$wrapper_hash}",
];
if ($element_type_state && $element_type_state !== 'none') {
$webform_element_manager = $this->pluginManagerWebformElement;
$webform = $this
->getWebform();
$plugin = $webform_element_manager
->getElementInstance($elements[$element_type_state], $webform);
$plugin_form_base = WebformUiElementAddForm::create(\Drupal::getContainer());
$plugin_form_base
->buildForm([], $form_state, $webform, $element_type_state);
$new_form_state = new FormState();
$new_form_state
->set('element', $elements[$element_type_state]);
$new_form_state
->setFormObject($plugin_form_base);
$plugin_form = PluginSubFormHelpers::buildPluginForm($plugin, $form, $new_form_state, [
'plugin',
]);
unset($plugin_form['display']['format_attributes']);
unset($plugin_form['display']['item']['format']['#options']['custom']);
unset($plugin_form['display']['items']['format_items']['#options']['custom']);
unset($plugin_form['display']['items']['format_items']['#options']['ol']);
unset($plugin_form['display']['items']['format_items']['#options']['ul']);
$form['settings_container']['format'] = [
'#type' => 'select',
'#title' => 'Single Format',
'#default_value' => $this->configuration['properties']['format'],
'#options' => $plugin_form['display']['item']['format']['#options'] ?? [],
];
$form['settings_container']['format_items'] = [
'#type' => 'select',
'#title' => 'Multiple Format',
'#default_value' => $this->configuration['properties']['format_items'],
'#options' => $plugin_form['display']['items']['format_items']['#options'] ?? [],
];
}
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$container = $values['settings_container'];
$this->configuration['element'] = $container['element'];
$this->configuration['properties']['format'] = $container['format'] ?? '';
$this->configuration['properties']['format_items'] = $container['format_items'] ?? '';
}
public function getElementCollection() {
$fields = [];
$disallowed_types = $this->configuration['disallowed_field_types'] ?? [];
$elements = $this
->getWebformElements();
foreach ($elements as $id => $element) {
if ($disallowed_types && !in_array($element['#type'], $disallowed_types)) {
$fields[$id] = (string) $element['#title'];
}
elseif (!$disallowed_types) {
$fields[$id] = (string) $element['#title'];
}
}
$fields = [
'none' => 'None',
] + $fields;
return $fields;
}
public function getWebformElements() {
$webform = $this
->getWebform();
if ($webform) {
return $this
->getWebform()
->getElementsDecodedAndFlattened();
}
return [];
}
public function getWebform() {
$entity_id = explode(':', $this->configuration['entity_id'])[1] ?? '';
return $this->entityTypeManager
->getStorage('webform')
->load($entity_id);
}
public function getFormattedValue(FormStateInterface $form_state) {
$options = $this->configuration['properties'];
$element = $this->configuration['element'];
$webform_submission = $form_state
->getFormObject()
->getEntity();
$webform = $webform_submission
->getWebform();
$webform_element_manager = $this->pluginManagerWebformElement;
$elements = $webform
->getElementsDecodedAndFlattened();
$element_plugin = $webform_element_manager
->getElementInstance($elements[$element], $webform);
$webform_options['webform_key'] = $element;
$element = $elements[$element];
$element['#format'] = $options['format'];
$element['#format_items'] = $options['format_items'];
return $element_plugin
->formatText($element, $webform_submission, $webform_options);
}
public function ajaxChangeFieldMap(array &$form, FormStateInterface $form_state) {
$trigger_element = $form_state
->getTriggeringElement();
$parents = array_slice($trigger_element['#array_parents'], 0, -1);
return NestedArray::getValue($form, $parents);
}
public function pluginTypeChangeSubmit(array &$form, FormStateInterface $form_state) {
$trigger_element = $form_state
->getTriggeringElement();
$parents = array_slice($trigger_element['#array_parents'], 0, -1);
$parents[] = 'element';
$wrapper_hash = $trigger_element['#wrapper_hash'];
$plugin_type = NestedArray::getValue($form, $parents)['#value'];
$form_state
->set("element_type__{$wrapper_hash}", $plugin_type);
$form_state
->setRebuild();
}
}