You are here

public function WebformSubmissionValue::buildConfigurationForm in Pardot Integration 2.x

File

src/Plugin/PardotFormMapFormatterPlugin/WebformSubmissionValue.php, line 82

Class

WebformSubmissionValue
Plugin to generate a text field and consume tokens for mappings.

Namespace

Drupal\pardot\Plugin\PardotFormMapFormatterPlugin

Code

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') {

    // Get the Webform object.

    /** @var \Drupal\webform\Plugin\WebformElementManager $webform_element_manager */
    $webform_element_manager = $this->pluginManagerWebformElement;
    $webform = $this
      ->getWebform();

    // Get element instance and build a fresh form.
    $plugin = $webform_element_manager
      ->getElementInstance($elements[$element_type_state], $webform);
    $plugin_form_base = WebformUiElementAddForm::create(\Drupal::getContainer());

    // No idea why but you need this line but don't remove it.
    $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);

    // Build the form.
    $plugin_form = PluginSubFormHelpers::buildPluginForm($plugin, $form, $new_form_state, [
      'plugin',
    ]);

    // Remove unwanted items from the elements formatting options.
    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']);

    // Set the formatter options to the form.
    $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;
}