You are here

public function RemotePostYamlFormHandler::buildConfigurationForm in YAML Form 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides YamlFormHandlerBase::buildConfigurationForm

File

src/Plugin/YamlFormHandler/RemotePostYamlFormHandler.php, line 117

Class

RemotePostYamlFormHandler
Form submission remote post handler.

Namespace

Drupal\yamlform\Plugin\YamlFormHandler

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $yamlform = $this
    ->getYamlForm();
  $results_disabled = $yamlform
    ->getSetting('results_disabled');
  $form['insert_url'] = [
    '#type' => 'url',
    '#title' => $this
      ->t('Insert URL'),
    '#description' => $this
      ->t('The full URL to POST to when a new form submission is saved. E.g. http://www.mycrm.com/form_insert_handler.php'),
    '#required' => TRUE,
    '#default_value' => $this->configuration['insert_url'],
  ];
  $form['update_url'] = [
    '#type' => 'url',
    '#title' => $this
      ->t('Update URL'),
    '#description' => $this
      ->t('The full URL to POST to when an existing form submission is updated. E.g. http://www.mycrm.com/form_insert_handler.php'),
    '#default_value' => $this->configuration['update_url'],
    '#access' => !$results_disabled,
  ];
  $form['delete_url'] = [
    '#type' => 'url',
    '#title' => $this
      ->t('Save URL'),
    '#description' => $this
      ->t('The full URL to POST to call when a form submission is deleted. E.g. http://www.mycrm.com/form_delete_handler.php'),
    '#default_value' => $this->configuration['delete_url'],
    '#access' => !$results_disabled,
  ];
  $form['type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Post type'),
    '#description' => $this
      ->t('Use x-www-form-urlencoded if unsure, as it is the default format for HTML forms. You also have the option to post data in <a href="http://www.json.org/" target="_blank">JSON</a> format.'),
    '#options' => [
      'x-www-form-urlencoded' => $this
        ->t('x-www-form-urlencoded'),
      'json' => $this
        ->t('JSON'),
    ],
    '#required' => TRUE,
    '#default_value' => $this->configuration['type'],
  ];
  $form['submission_data'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Submission data'),
  ];
  $form['submission_data']['excluded_data'] = [
    '#type' => 'yamlform_excluded_columns',
    '#title' => $this
      ->t('Posted data'),
    '#title_display' => 'invisible',
    '#yamlform' => $yamlform,
    '#required' => TRUE,
    '#parents' => [
      'settings',
      'excluded_data',
    ],
    '#default_value' => $this->configuration['excluded_data'],
  ];
  $form['custom_data'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom data'),
    '#description' => $this
      ->t('Custom data will take precedence over submission data. You may use tokens.'),
  ];
  $form['custom_data']['custom_data'] = [
    '#type' => 'yamlform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Custom data'),
    '#description' => $this
      ->t('Enter custom data that will be included in all remote post requests.'),
    '#parents' => [
      'settings',
      'custom_data',
    ],
    '#default_value' => $this->configuration['custom_data'],
  ];
  $form['custom_data']['insert_custom_data'] = [
    '#type' => 'yamlform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Insert data'),
    '#description' => $this
      ->t("Enter custom data that will be included when a new form submission is saved."),
    '#parents' => [
      'settings',
      'insert_custom_data',
    ],
    '#states' => [
      'visible' => [
        [
          ':input[name="settings[update_url]"]' => [
            'filled' => TRUE,
          ],
        ],
        'or',
        [
          ':input[name="settings[delete_url]"]' => [
            'filled' => TRUE,
          ],
        ],
      ],
    ],
    '#default_value' => $this->configuration['insert_custom_data'],
  ];
  $form['custom_data']['update_custom_data'] = [
    '#type' => 'yamlform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Update data'),
    '#description' => $this
      ->t("Enter custom data that will be included when a form submission is updated."),
    '#parents' => [
      'settings',
      'update_custom_data',
    ],
    '#states' => [
      'visible' => [
        ':input[name="settings[update_url]"]' => [
          'filled' => TRUE,
        ],
      ],
    ],
    '#default_value' => $this->configuration['update_custom_data'],
  ];
  $form['custom_data']['delete_custom_data'] = [
    '#type' => 'yamlform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Delete data'),
    '#description' => $this
      ->t("Enter custom data that will be included when a form submission is deleted."),
    '#parents' => [
      'settings',
      'delete_custom_data',
    ],
    '#states' => [
      'visible' => [
        ':input[name="settings[delete_url]"]' => [
          'filled' => TRUE,
        ],
      ],
    ],
    '#default_value' => $this->configuration['delete_custom_data'],
  ];
  $form['custom_data']['token_tree_link'] = $this->tokenManager
    ->buildTreeLink();
  $form['debug'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable debugging'),
    '#description' => $this
      ->t('If checked, posted submissions will be displayed onscreen to all users.'),
    '#return_value' => TRUE,
    '#default_value' => $this->configuration['debug'],
  ];
  return $form;
}