You are here

public function WebformEntityHandler::buildConfigurationForm in Webform Entity Handler 8

Same name and namespace in other branches
  1. 2.x src/Plugin/WebformHandler/WebformEntityHandler.php \Drupal\webform_entity_handler\Plugin\WebformHandler\WebformEntityHandler::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/WebformEntityHandler.php, line 131

Class

WebformEntityHandler
Create or update an entity with a webform submission values.

Namespace

Drupal\webform_entity_handler\Plugin\WebformHandler

Code

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

  // Get the webform elements options array.
  $webform_elements = $this
    ->getElements();

  // Entity settings.
  $form['entity_settings'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Entity settings'),
    '#collapsible' => FALSE,
  ];
  $form['entity_settings']['operation'] = [
    '#type' => 'webform_select_other',
    '#title' => $this
      ->t('Entity operation'),
    '#description' => $this
      ->t('If the entity ID is empty a new entity will be created and then updated with the new entity ID.'),
    '#options' => [
      self::DEFAULT_VALUE => $this
        ->t('Create a new entity'),
      $this
        ->t('or update entity ID stored in the following submission element:')
        ->__toString() => $webform_elements,
      WebformSelectOther::OTHER_OPTION => $this
        ->t('Update custom entity ID…'),
    ],
    '#empty_option' => $this
      ->t('- Select -'),
    '#required' => TRUE,
    '#default_value' => $this->configuration['operation'],
    '#parents' => [
      'settings',
      'operation',
    ],
  ];
  $form['entity_settings']['entity_type_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity type'),
    '#options' => $this
      ->getEntityTypes(),
    '#empty_option' => $this
      ->t('- Select -'),
    '#required' => TRUE,
    '#default_value' => $this->configuration['entity_type_id'],
    '#ajax' => [
      'callback' => [
        get_called_class(),
        'updateEntityFields',
      ],
      'wrapper' => 'webform-entity-handler--entity-values',
      'progress' => [
        'type' => 'throbber',
        'message' => $this
          ->t('Loading fields...'),
      ],
    ],
    '#parents' => [
      'settings',
      'entity_type_id',
    ],
  ];
  $form['entity_settings']['entity_values'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Entity values'),
    '#attributes' => [
      'id' => 'webform-entity-handler--entity-values',
    ],
    '#collapsible' => FALSE,
  ];
  if (!empty($this->configuration['entity_type_id'])) {
    $form['entity_settings']['entity_values'] += $this
      ->getEntityFieldsForm($this->configuration['entity_type_id']);
  }
  $form['entity_settings']['entity_revision'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Create new revision'),
    '#default_value' => $this->configuration['entity_revision'],
    '#access' => FALSE,
    '#parents' => [
      'settings',
      'entity_revision',
    ],
  ];
  if (!empty($this->configuration['entity_type_id'])) {
    [
      $type,
    ] = explode(':', $this->configuration['entity_type_id']);
    $form['entity_settings']['entity_revision']['#access'] = $this->entityTypeManager
      ->getDefinition($type)
      ->isRevisionable();
  }
  $form['token_tree_link'] = $this->webformTokenManager
    ->buildTreeLink();

  // Additional.
  $results_disabled = $this
    ->getWebform()
    ->getSetting('results_disabled');
  $form['additional'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Additional settings'),
  ];

  // Settings: States.
  $form['additional']['states'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Execute'),
    '#options' => [
      WebformSubmissionInterface::STATE_DRAFT => $this
        ->t('…when <b>draft</b> is saved.'),
      WebformSubmissionInterface::STATE_CONVERTED => $this
        ->t('…when anonymous submission is <b>converted</b> to authenticated.'),
      WebformSubmissionInterface::STATE_COMPLETED => $this
        ->t('…when submission is <b>completed</b>.'),
      WebformSubmissionInterface::STATE_UPDATED => $this
        ->t('…when submission is <b>updated</b>.'),
      WebformSubmissionInterface::STATE_DELETED => $this
        ->t('…when submission is <b>deleted</b>.'),
    ],
    '#parents' => [
      'settings',
      'states',
    ],
    '#access' => $results_disabled ? FALSE : TRUE,
    '#default_value' => $results_disabled ? [
      WebformSubmissionInterface::STATE_COMPLETED,
    ] : $this->configuration['states'],
  ];
  if (method_exists($this, 'elementTokenValidate')) {
    $this
      ->elementTokenValidate($form);
  }
  return $this
    ->setSettingsParents($form);
}