You are here

public function PropertiesExtended::buildConfigurationForm in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/salesforce_mapping/src/Plugin/SalesforceMappingField/PropertiesExtended.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\PropertiesExtended::buildConfigurationForm()
  2. 8.3 modules/salesforce_mapping/src/Plugin/SalesforceMappingField/PropertiesExtended.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\PropertiesExtended::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 SalesforceMappingFieldPluginBase::buildConfigurationForm

File

modules/salesforce_mapping/src/Plugin/SalesforceMappingField/PropertiesExtended.php, line 48

Class

PropertiesExtended
Adapter for entity properties and fields.

Namespace

Drupal\salesforce_mapping\Plugin\SalesforceMappingField

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $pluginForm = parent::buildConfigurationForm($form, $form_state);
  $mapping = $form['#entity'];

  // Display the plugin config form here:
  $context_name = 'drupal_field_value';

  // If the form has been submitted already, take the mode from the submitted
  // values, otherwise default to existing configuration. And if that does not
  // exist default to the "input" mode.
  $mode = $form_state
    ->get('context_' . $context_name);
  if (!$mode) {
    $mode = 'selector';
    $form_state
      ->set('context_' . $context_name, $mode);
  }
  $title = $mode == 'selector' ? $this
    ->t('Data selector') : $this
    ->t('Value');
  $pluginForm[$context_name]['setting'] = [
    '#type' => 'textfield',
    '#title' => $title,
    '#attributes' => [
      'class' => [
        'drupal-field-value',
      ],
    ],
    '#default_value' => $this
      ->config('drupal_field_value'),
  ];
  $element =& $pluginForm[$context_name]['setting'];
  if ($mode == 'selector') {
    $element['#description'] = $this
      ->t("The data selector helps you drill down into the data available.");
    $element['#autocomplete_route_name'] = 'salesforce_mapping.autocomplete_controller_autocomplete';
    $element['#autocomplete_route_parameters'] = [
      'entity_type_id' => $mapping
        ->get('drupal_entity_type'),
      'bundle' => $mapping
        ->get('drupal_bundle'),
    ];
  }
  $value = $mode == 'selector' ? $this
    ->t('Switch to the direct input mode') : $this
    ->t('Switch to data selection');
  $pluginForm[$context_name]['switch_button'] = [
    '#type' => 'submit',
    '#name' => 'context_' . $context_name,
    '#attributes' => [
      'class' => [
        'drupal-field-switch-button',
      ],
    ],
    '#parameter' => $context_name,
    '#value' => $value,
    '#submit' => [
      static::class . '::switchContextMode',
    ],
    // Do not validate!
    '#limit_validation_errors' => [],
  ];
  return $pluginForm;
}