You are here

public function DrupalConstant::buildConfigurationForm in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 5.0.x modules/salesforce_mapping/src/Plugin/SalesforceMappingField/DrupalConstant.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\DrupalConstant::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/DrupalConstant.php, line 25

Class

DrupalConstant
Adapter for entity Constant and fields.

Namespace

Drupal\salesforce_mapping\Plugin\SalesforceMappingField

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $pluginForm = parent::buildConfigurationForm($form, $form_state);

  // @TODO inspecting the form and form_state feels wrong, but haven't found a good way to get the entity from config before the config is saved.
  $options = $this
    ->getConfigurationOptions($form['#entity']);

  // Display the plugin config form here:
  if (empty($options)) {
    $pluginForm['drupal_field_value'] = [
      '#markup' => $this
        ->t('No available properties.'),
    ];
  }
  else {
    $pluginForm['drupal_field_value'] += [
      '#type' => 'select',
      '#options' => $options,
      '#empty_option' => $this
        ->t('- Select -'),
      '#default_value' => $this
        ->config('drupal_field_value'),
      '#description' => $this
        ->t('Select a Drupal field or property to map to a constant.'),
    ];
  }

  // A field to hold the constant value.
  $pluginForm['drupal_constant'] = [
    '#type' => 'textfield',
    '#default_value' => $this
      ->config('drupal_constant'),
    '#description' => $this
      ->t('Enter a constant value to map to a Drupal field.'),
  ];

  // There is no salesforce field for this mapping.
  unset($pluginForm['salesforce_field']);

  // We should only be able to pull a constant value to a Drupal field.
  $pluginForm['direction']['#options'] = [
    MappingConstants::SALESFORCE_MAPPING_DIRECTION_SF_DRUPAL => $pluginForm['direction']['#options'][MappingConstants::SALESFORCE_MAPPING_DIRECTION_SF_DRUPAL],
  ];
  $pluginForm['direction']['#default_value'] = MappingConstants::SALESFORCE_MAPPING_DIRECTION_SF_DRUPAL;
  return $pluginForm;
}