You are here

public function SalesforceMappingFieldsForm::buildForm in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 5.0.x modules/salesforce_mapping_ui/src/Form/SalesforceMappingFieldsForm.php \Drupal\salesforce_mapping_ui\Form\SalesforceMappingFieldsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

modules/salesforce_mapping_ui/src/Form/SalesforceMappingFieldsForm.php, line 19

Class

SalesforceMappingFieldsForm
Salesforce Mapping Fields Form.

Namespace

Drupal\salesforce_mapping_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if (!$this
    ->ensureConnection('objectDescribe', [
    $this->entity
      ->getSalesforceObjectType(),
    TRUE,
  ])) {
    return $form;
  }
  $form = parent::buildForm($form, $form_state);

  // Previously "Field Mapping" table on the map edit form.
  // @TODO add a header with Fieldmap Property information.
  // Add #entity property to expose it to our field plugin forms.
  $form['#entity'] = $this->entity;
  $form['#attached']['library'][] = 'salesforce/admin';

  // This needs to be loaded now as it can't be loaded via AJAX for the AC
  // enabled fields.
  $form['#attached']['library'][] = 'core/drupal.autocomplete';

  // For each field on the map, add a row to our table.
  $form['overview'] = [
    '#markup' => 'Field mapping overview goes here.',
  ];
  $form['key_wrapper'] = [
    '#title' => $this
      ->t('Upsert Key'),
    '#type' => 'details',
    '#open' => TRUE,
    '#description' => $this
      ->t('An Upsert Key can be assigned to map a Drupal property to a Salesforce External Identifier. If specified an UPSERT will be used to limit data duplication.'),
  ];
  $key_options = $this
    ->getUpsertKeyOptions();
  if (empty($key_options)) {
    $form['key_wrapper']['#description'] .= ' ' . $this
      ->t('To add an upsert key for @sobject_name, assign a field as an External Identifier in Salesforce.', [
      '@sobject_name' => $this->entity
        ->get('salesforce_object_type'),
    ]);
    $form['key_wrapper']['key'] = [
      '#type' => 'value',
      '#value' => '',
    ];
  }
  else {
    $form['key_wrapper']['key'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Upsert Key'),
      '#options' => $key_options,
      '#default_value' => $this->entity
        ->getKeyField(),
      '#empty_option' => $this
        ->t('(none)'),
      '#empty_value' => '',
    ];
    $form['key_wrapper']['always_upsert'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Always Upsert'),
      '#default_value' => $this->entity
        ->get('always_upsert'),
      '#description' => $this
        ->t('If checked, always use "upsert" to push data to Salesforce. Otherwise, prefer a Salesforce ID if available. For example, given a user mapping with "email" set for upsert key, leave this checkbox off; otherwise, a new Salesforce record will be created whenever a user changes their email.'),
    ];
  }
  $form['field_mappings_wrapper'] = [
    '#title' => $this
      ->t('Mapped Fields'),
    '#type' => 'fieldset',
  ];
  $field_mappings_wrapper =& $form['field_mappings_wrapper'];

  // Check to see if we have enough information to allow mapping fields.  If
  // not, tell the user what is needed in order to have the field map show up.
  $field_mappings_wrapper['field_mappings'] = [
    '#tree' => TRUE,
    '#type' => 'container',
    // @TODO there's probably a better way to tie ajax callbacks to this element than by hard-coding an HTML DOM ID here.
    '#prefix' => '<div id="edit-field-mappings">',
    '#suffix' => '</div>',
    '#attributes' => [
      'class' => [
        'container-striped',
      ],
    ],
  ];
  $rows =& $field_mappings_wrapper['field_mappings'];
  $form['field_mappings_wrapper']['ajax_warning'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'edit-ajax-warning',
    ],
  ];
  $add_field_text = !empty($field_mappings) ? $this
    ->t('Add another field mapping') : $this
    ->t('Add a field mapping to get started');
  $form['buttons'] = [
    '#type' => 'container',
    '#tree' => true,
  ];
  $form['buttons']['field_type'] = [
    '#title' => $this
      ->t('Field Type'),
    '#type' => 'select',
    '#options' => $this
      ->getDrupalTypeOptions($this->entity),
    '#attributes' => [
      'id' => 'edit-mapping-add-field-type',
    ],
    '#empty_option' => $this
      ->t('- Select -'),
  ];
  $form['buttons']['add'] = [
    '#value' => $add_field_text,
    '#type' => 'submit',
    '#limit_validation_errors' => [
      [
        'buttons',
      ],
    ],
    '#submit' => [
      '::addField',
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'fieldAddCallback',
      ],
      'wrapper' => 'edit-field-mappings',
    ],
    '#states' => [
      'disabled' => [
        ':input#edit-mapping-add-field-type' => [
          'value' => '',
        ],
      ],
    ],
  ];

  // Add a row for each saved mapping.
  foreach ($this->entity
    ->getFieldMappings() as $field_plugin) {
    $rows[] = $this
      ->getRow($form, $form_state, $field_plugin);
  }

  // Add a new row in case it was just added.
  $values =& $form_state
    ->getValues();
  $new_field = NestedArray::getValue($values, [
    'buttons',
    'new_field',
  ]);
  if (!empty($new_field)) {
    $rows[] = $this
      ->getRow($form, $form_state);
    NestedArray::unsetValue($values, [
      'buttons',
      'new_field',
    ]);
  }

  // Retrieve and add the form actions array.
  $actions = $this
    ->actionsElement($form, $form_state);
  if (!empty($actions)) {
    $form['actions'] = $actions;
  }
  return $form;
}