public function HubspotWebformHandler::buildConfigurationForm in HubSpot 8
Same name and namespace in other branches
- 3.x src/Plugin/WebformHandler/HubspotWebformHandler.php \Drupal\hubspot\Plugin\WebformHandler\HubspotWebformHandler::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/ HubspotWebformHandler.php, line 72
Class
- HubspotWebformHandler
- Webform submission remote post handler.
Namespace
Drupal\hubspot\Plugin\WebformHandlerCode
public function buildConfigurationForm(array $form, FormStateInterface $form_state) : array {
// First check if hubspot is connected.
if (!$this->hubspot
->isConfigured()) {
$form['mapping']['notice'] = [
'#type' => 'item',
'#title' => $this
->t('Notice'),
'#markup' => $this
->t('Your site account is not connected to a Hubspot account, please @admin_link first.', [
'@admin_link' => Link::createFromRoute('connect to Hubspot', 'hubspot.admin_settings'),
]),
];
return $form;
}
$settings = $this
->getSettings();
$default_hubspot_guid = $settings['form_guid'] ?? NULL;
$this->webform = $this
->getWebform();
$form['mapping'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Field Mapping'),
];
$options = [
'--donotmap--' => 'Do Not Map',
];
try {
$hubspot_forms = $this->hubspot
->getHubspotForms();
} catch (GuzzleException $e) {
$this
->messenger()
->addWarning('Unable to load hubspot form info.');
return $form;
}
$hubspot_forms = array_column($hubspot_forms, NULL, 'guid');
$options = array_column($hubspot_forms, 'name', 'guid');
// Sort $options alphabetically and retain key (guid).
asort($options, SORT_STRING | SORT_FLAG_CASE);
// Select list of forms on hubspot.
$form['mapping']['hubspot_form'] = [
'#type' => 'select',
'#title' => $this
->t('Choose a hubspot form:'),
'#options' => $options,
'#default_value' => $default_hubspot_guid,
'#ajax' => [
'callback' => [
$this,
'showWebformFields',
],
'event' => 'change',
'wrapper' => 'field_mapping_list',
],
];
$form['mapping']['original_hubspot_id'] = [
'#type' => 'hidden',
'#value' => $default_hubspot_guid,
];
// Fieldset to contain mapping fields.
$form['mapping']['field_group'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Fields to map for form: @label', [
'@label' => $this->webform
->label(),
]),
'#states' => [
'invisible' => [
':input[name="settings[mapping][hubspot_form]"]' => [
'value' => '--donotmap--',
],
],
],
];
$form['mapping']['field_group']['fields'] = [
'#type' => 'container',
'#prefix' => '<div id="field_mapping_list">',
'#suffix' => '</div>',
'#markup' => '',
];
$form_values = $form_state
->getValues();
// Apply default values if available.
if (!empty($form_values['mapping']['hubspot_form']) || !empty($default_hubspot_guid)) {
// Generally, these elements cannot be submitted to HubSpot.
$exclude_elements = [
'webform_actions',
'webform_flexbox',
'webform_markup',
'webform_more',
'webform_section',
'webform_wizard_page',
'webform_message',
'webform_horizontal_rule',
'webform_terms_of_service',
'webform_computed_token',
'webform_computed_twig',
'webform_element',
'processed_text',
'captcha',
'container',
'details',
'fieldset',
'item',
'label',
];
if (!empty($form_values['mapping']['hubspot_form'])) {
$hubspot_guid = $form_values['mapping']['hubspot_form'];
}
else {
$hubspot_guid = $default_hubspot_guid;
}
$hubspot_fields = $hubspot_forms[$hubspot_guid] ?? [];
$options = [
'--donotmap--' => 'Do Not Map',
];
foreach ($hubspot_fields['formFieldGroups'] as $hubspot_field) {
foreach ($hubspot_field['fields'] as $field) {
$options[$field['name']] = $field['label'];
}
}
$components = $this->webform
->getElementsInitializedAndFlattened();
foreach ($components as $webform_field => $value) {
if (!in_array($value['#type'], $exclude_elements)) {
if ($value['#webform_composite']) {
// Loop through a composite field to get all fields.
foreach ($value['#webform_composite_elements'] as $composite_field => $composite_value) {
$key = $webform_field . ':' . $composite_field;
$form['mapping']['field_group']['fields'][$key] = [
'#title' => (@$webform_field . ':' . $composite_value['#title'] ?: $key) . ' (' . $composite_value['#type'] . ')',
'#type' => 'select',
'#options' => $options,
];
if (isset($settings['field_mapping'][$key])) {
$form['mapping']['field_group']['fields'][$key]['#default_value'] = $settings['field_mapping'][$key];
}
}
}
else {
// Non composite element.
$form['mapping']['field_group']['fields'][$webform_field] = [
'#title' => (@$value['#title'] ?: $webform_field) . ' (' . $value['#type'] . ')',
'#type' => 'select',
'#options' => $options,
];
if (isset($settings['field_mapping'][$webform_field])) {
$form['mapping']['field_group']['fields'][$webform_field]['#default_value'] = $settings['field_mapping'][$webform_field];
}
}
}
}
}
return $form;
}