You are here

public function RemotePostWebformHandler::buildConfigurationForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformHandler/RemotePostWebformHandler.php \Drupal\webform\Plugin\WebformHandler\RemotePostWebformHandler::buildConfigurationForm()

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 WebformHandlerBase::buildConfigurationForm

File

src/Plugin/WebformHandler/RemotePostWebformHandler.php, line 184

Class

RemotePostWebformHandler
Webform submission remote post handler.

Namespace

Drupal\webform\Plugin\WebformHandler

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $webform = $this
    ->getWebform();

  // States.
  $states = [
    WebformSubmissionInterface::STATE_COMPLETED => [
      'state' => $this
        ->t('completed'),
      'label' => $this
        ->t('Completed'),
      'description' => $this
        ->t('Post data when <b>submission is completed</b>.'),
      'access' => TRUE,
    ],
    WebformSubmissionInterface::STATE_UPDATED => [
      'state' => $this
        ->t('updated'),
      'label' => $this
        ->t('Updated'),
      'description' => $this
        ->t('Post data when <b>submission is updated</b>.'),
      'access' => $this
        ->isResultsEnabled(),
    ],
    WebformSubmissionInterface::STATE_DELETED => [
      'state' => $this
        ->t('deleted'),
      'label' => $this
        ->t('Deleted'),
      'description' => $this
        ->t('Post data when <b>submission is deleted</b>.'),
      'access' => $this
        ->isResultsEnabled(),
    ],
    WebformSubmissionInterface::STATE_DRAFT_CREATED => [
      'state' => $this
        ->t('draft created'),
      'label' => $this
        ->t('Draft created'),
      'description' => $this
        ->t('Post data when <b>draft is created.</b>'),
      'access' => $this
        ->isDraftEnabled(),
    ],
    WebformSubmissionInterface::STATE_DRAFT_UPDATED => [
      'state' => $this
        ->t('draft updated'),
      'label' => $this
        ->t('Draft updated'),
      'description' => $this
        ->t('Post data when <b>draft is updated.</b>'),
      'access' => $this
        ->isDraftEnabled(),
    ],
    WebformSubmissionInterface::STATE_CONVERTED => [
      'state' => $this
        ->t('converted'),
      'label' => $this
        ->t('Converted'),
      'description' => $this
        ->t('Post data when anonymous <b>submission is converted</b> to authenticated.'),
      'access' => $this
        ->isConvertEnabled(),
    ],
  ];
  foreach ($states as $state => $state_item) {
    $state_url = $state . '_url';
    $state_custom_data = $state . '_custom_data';
    $t_args = [
      '@state' => $state_item['state'],
      '@title' => $state_item['label'],
      '@url' => 'http://www.mycrm.com/form_' . $state . '_handler.php',
    ];
    $form[$state] = [
      '#type' => 'details',
      '#open' => $state === WebformSubmissionInterface::STATE_COMPLETED,
      '#title' => $state_item['label'],
      '#description' => $state_item['description'],
      '#access' => $state_item['access'],
    ];
    $form[$state][$state_url] = [
      '#type' => 'url',
      '#title' => $this
        ->t('@title URL', $t_args),
      '#description' => $this
        ->t('The full URL to POST to when an existing webform submission is @state. (e.g. @url)', $t_args),
      '#required' => $state === WebformSubmissionInterface::STATE_COMPLETED,
      '#maxlength' => NULL,
      '#default_value' => $this->configuration[$state_url],
    ];
    $form[$state][$state_custom_data] = [
      '#type' => 'webform_codemirror',
      '#mode' => 'yaml',
      '#title' => $this
        ->t('@title custom data', $t_args),
      '#description' => $this
        ->t('Enter custom data that will be included when a webform submission is @state.', $t_args),
      '#states' => [
        'visible' => [
          ':input[name="settings[' . $state_url . ']"]' => [
            'filled' => TRUE,
          ],
        ],
      ],
      '#default_value' => $this->configuration[$state_custom_data],
    ];
    if ($state === WebformSubmissionInterface::STATE_COMPLETED) {
      $form[$state]['token'] = [
        '#type' => 'webform_message',
        '#message_message' => $this
          ->t('Response data can be passed to the submission data using [webform:handler:{machine_name}:{state}:{key}] tokens. (i.e. [webform:handler:remote_post:completed:confirmation_number])'),
        '#message_type' => 'info',
      ];
    }
  }

  // Additional.
  $form['additional'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Additional settings'),
  ];
  $form['additional']['method'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Method'),
    '#description' => $this
      ->t('The <b>POST</b> request method requests that a web server accept the data enclosed in the body of the request message. It is often used when uploading a file or when submitting a completed webform. In contrast, the HTTP <b>GET</b> request method retrieves information from the server.'),
    '#required' => TRUE,
    '#options' => [
      'POST' => 'POST',
      'PUT' => 'PUT',
      'PATCH' => 'PATCH',
      'GET' => 'GET',
    ],
    '#default_value' => $this->configuration['method'],
  ];
  $form['additional']['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 webforms. You also have the option to post data in <a href="http://www.json.org/">JSON</a> format.'),
    '#options' => [
      'x-www-form-urlencoded' => $this
        ->t('x-www-form-urlencoded'),
      'json' => $this
        ->t('JSON'),
    ],
    '#states' => [
      '!visible' => [
        ':input[name="settings[method]"]' => [
          'value' => 'GET',
        ],
      ],
      '!required' => [
        ':input[name="settings[method]"]' => [
          'value' => 'GET',
        ],
      ],
    ],
    '#default_value' => $this->configuration['type'],
  ];
  $form['additional']['file_data'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Include files as Base64 encoded post data'),
    '#description' => $this
      ->t('If checked, uploaded and attached file data will be included using Base64 encoding.'),
    '#return_value' => TRUE,
    '#default_value' => $this->configuration['file_data'],
    '#access' => $this
      ->getWebform()
      ->hasAttachments(),
  ];
  $form['additional']['cast'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Cast posted element value and custom data'),
    '#description' => $this
      ->t('If checked, posted element values will be cast to integers, floats, and booleans as needed. Custom data can be cast by placing the desired type in parentheses before the value or token. (i.e. "(int) [webform_submission:value:total]" or "(int) 100")') . '<br/>' . '<br/>' . $this
      ->t('For custom data, the casts allowed are:') . '<ul>' . '<li>' . $this
      ->t('@cast - cast to @type', [
      '@cast' => '(int), (integer)',
      '@type' => 'integer',
    ]) . '</li>' . '<li>' . $this
      ->t('@cast - cast to @type', [
      '@cast' => '(float), (double), (real)',
      '@type' => 'float',
    ]) . '</li>' . '<li>' . $this
      ->t('@cast - cast to @type', [
      '@cast' => '(bool), (boolean)',
      '@type' => 'boolean',
    ]) . '</li>' . '</ul>',
    '#return_value' => TRUE,
    '#default_value' => $this->configuration['cast'],
  ];
  $form['additional']['custom_data'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Custom data'),
    '#description' => $this
      ->t('Enter custom data that will be included in all remote post requests.'),
    '#default_value' => $this->configuration['custom_data'],
  ];
  $form['additional']['custom_options'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Custom options'),
    '#description' => $this
      ->t('Enter custom <a href=":href">request options</a> that will be used by the Guzzle HTTP client. Request options can include custom headers.', [
      ':href' => 'http://docs.guzzlephp.org/en/stable/request-options.html',
    ]),
    '#default_value' => $this->configuration['custom_options'],
  ];
  $form['additional']['message'] = [
    '#type' => 'webform_html_editor',
    '#title' => $this
      ->t('Custom error response message'),
    '#description' => $this
      ->t('This message is displayed when the response status code is not 2xx.') . '<br/><br/>' . $this
      ->t('Defaults to: %value', [
      '%value' => $this->messageManager
        ->render(WebformMessageManagerInterface::SUBMISSION_EXCEPTION_MESSAGE),
    ]),
    '#default_value' => $this->configuration['message'],
  ];
  $form['additional']['messages_token'] = [
    '#type' => 'webform_message',
    '#message_message' => $this
      ->t('Response data can be passed to response message using [webform:handler:{machine_name}:{key}] tokens. (i.e. [webform:handler:remote_post:message])'),
    '#message_type' => 'info',
  ];
  $form['additional']['messages'] = [
    '#type' => 'webform_multiple',
    '#title' => $this
      ->t('Custom response messages'),
    '#description' => $this
      ->t('Enter custom response messages for specific status codes.'),
    '#empty_items' => 0,
    '#no_items_message' => $this
      ->t('No error response messages entered. Please add messages below.'),
    '#add' => FALSE,
    '#element' => [
      'code' => [
        '#type' => 'webform_select_other',
        '#title' => $this
          ->t('Response status code'),
        '#options' => [
          '200' => $this
            ->t('200 OK'),
          '201' => $this
            ->t('201 Created'),
          '204' => $this
            ->t('204 No Content'),
          '400' => $this
            ->t('400 Bad Request'),
          '401' => $this
            ->t('401 Unauthorized'),
          '403' => $this
            ->t('403 Forbidden'),
          '404' => $this
            ->t('404 Not Found'),
          '500' => $this
            ->t('500 Internal Server Error'),
          '502' => $this
            ->t('502 Bad Gateway'),
          '503' => $this
            ->t('503 Service Unavailable'),
          '504' => $this
            ->t('504 Gateway Timeout'),
        ],
        '#other__type' => 'number',
        '#other__description' => $this
          ->t('<a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes">List of HTTP status codes</a>.'),
      ],
      'message' => [
        '#type' => 'webform_html_editor',
        '#title' => $this
          ->t('Response message'),
      ],
    ],
    '#default_value' => $this->configuration['messages'],
  ];
  $form['additional']['error_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom error response redirect URL'),
    '#description' => $this
      ->t('The URL or path to redirect to when a remote fails.'),
    '#default_value' => $this->configuration['error_url'],
    '#pattern' => '(https?:\\/\\/|\\/).+',
  ];

  // Development.
  $form['development'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Development settings'),
  ];
  $form['development']['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'],
  ];

  // Submission data.
  $form['submission_data'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Submission data'),
  ];

  // Display warning about file uploads.
  if ($this
    ->getWebform()
    ->hasManagedFile()) {
    $form['submission_data']['managed_file_message'] = [
      '#type' => 'webform_message',
      '#message_message' => $this
        ->t('Upload files will include the file\'s id, name, uri, and data (<a href=":href">Base64</a> encode).', [
        ':href' => 'https://en.wikipedia.org/wiki/Base64',
      ]),
      '#message_type' => 'warning',
      '#message_close' => TRUE,
      '#message_id' => 'webform_node.references',
      '#message_storage' => WebformMessage::STORAGE_SESSION,
      '#states' => [
        'visible' => [
          ':input[name="settings[file_data]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['submission_data']['managed_file_message_no_data'] = [
      '#type' => 'webform_message',
      '#message_message' => $this
        ->t('Upload files will include the file\'s id, name and uri.'),
      '#message_type' => 'warning',
      '#message_close' => TRUE,
      '#message_id' => 'webform_node.references',
      '#message_storage' => WebformMessage::STORAGE_SESSION,
      '#states' => [
        'visible' => [
          ':input[name="settings[file_data]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
  }
  $form['submission_data']['excluded_data'] = [
    '#type' => 'webform_excluded_columns',
    '#title' => $this
      ->t('Posted data'),
    '#title_display' => 'invisible',
    '#webform_id' => $webform
      ->id(),
    '#required' => TRUE,
    '#default_value' => $this->configuration['excluded_data'],
  ];
  $this
    ->elementTokenValidate($form);
  return $this
    ->setSettingsParents($form);
}