class SalesforceMappingFieldsForm in Salesforce Suite 8.3
Salesforce Mapping Fields Form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\salesforce_mapping\Form\SalesforceMappingFormBase
- class \Drupal\salesforce_mapping\Form\SalesforceMappingFieldsForm
- class \Drupal\salesforce_mapping\Form\SalesforceMappingFormBase
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of SalesforceMappingFieldsForm
File
- modules/
salesforce_mapping/ src/ Form/ SalesforceMappingFieldsForm.php, line 14
Namespace
Drupal\salesforce_mapping\FormView source
class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
if (!$this
->ensureConnection('objectDescribe', $this->entity
->getSalesforceObjectType())) {
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' => t('Upsert Key'),
'#type' => 'details',
'#open' => TRUE,
'#description' => 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'] .= ' ' . 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' => t('Upsert Key'),
'#options' => $key_options,
'#default_value' => $this->entity
->getKeyField(),
'#empty_option' => t('(none)'),
'#empty_value' => '',
];
$form['key_wrapper']['always_upsert'] = [
'#type' => 'checkbox',
'#title' => t('Always Upsert'),
'#default_value' => $this->entity
->get('always_upsert'),
'#description' => 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' => t('Mapped Fields'),
'#type' => 'details',
'#id' => 'edit-field-mappings-wrapper',
'#open' => TRUE,
];
$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.
'#id' => 'edit-field-mappings',
'#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) ? t('Add another field mapping') : t('Add a field mapping to get started');
$form['buttons'] = [
'#type' => 'container',
];
$form['buttons']['field_type'] = [
'#title' => 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',
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => [
$this,
'fieldAddCallback',
],
'wrapper' => 'edit-field-mappings-wrapper',
],
'#states' => [
'disabled' => [
':input#edit-mapping-add-field-type' => [
'value' => '',
],
],
],
];
$row_template = [
'#type' => 'container',
'#attributes' => [
'class' => [
'field_mapping_field',
'row',
],
],
];
// Add a row for each saved mapping.
$zebra = 0;
foreach ($this->entity
->getFieldMappings() as $field_plugin) {
$row = $row_template;
$row['#attributes']['class']['zebra'] = $zebra % 2 ? 'odd' : 'even';
$rows[] = $row + $this
->getRow($field_plugin, $form, $form_state);
$zebra++;
}
// Apply any changes from form_state to existing fields.
$input = $form_state
->getUserInput();
if (!empty($input['field_mappings'])) {
for ($i = count($this->entity
->getFieldMappings()); $i < count($input['field_mappings']); $i++) {
$row = $row_template;
$row['#attributes']['class']['zebra'] = $zebra % 2 ? 'odd' : 'even';
$field_plugin = $this->entity
->getFieldMapping($input['field_mappings'][$i]);
$rows[] = $row + $this
->getRow($field_plugin, $form, $form_state);
$zebra++;
}
}
// @TODO input does not contain the clicked button, have to go to values for
// that. This may change?
// Add button was clicked. See if we have a field_type value -- it's
// required. If not, take no action. #states is already used to prevent
// users from adding without selecting field_type. If they've worked
// around that, they're going to have problems.
if (!empty($form_state
->getValues()) && $form_state
->getValue('add') == $form_state
->getValue('op') && !empty($input['field_type']) && $form_state
->getTriggeringElement()['#name'] != 'context_drupal_field_value') {
$row = $row_template;
$row['#attributes']['class']['zebra'] = $zebra % 2 ? 'odd' : 'even';
$rows[] = $row + $this
->getRow(NULL, $form, $form_state);
$zebra++;
}
// Retrieve and add the form actions array.
$actions = $this
->actionsElement($form, $form_state);
if (!empty($actions)) {
$form['actions'] = $actions;
}
return $form;
}
/**
* Return an options array of field labels for any fields marked externalId.
*/
private function getUpsertKeyOptions() {
$options = [];
try {
$describe = $this
->getSalesforceObject();
} catch (\Exception $e) {
return [];
}
foreach ($describe->fields as $field) {
if ($field['externalId'] || $field['idLookup']) {
$options[$field['name']] = $field['label'];
}
}
return $options;
}
/**
* Helper function to return an empty row for the field mapping form.
*/
private function getRow(FieldPluginInterface $field_plugin = NULL, $form, FormStateInterface $form_state) {
$input = $form_state
->getUserInput();
if ($field_plugin == NULL) {
$field_type = $input['field_type'];
$field_plugin_definition = $this
->getFieldPlugin($field_type);
$field_plugin = $this->mappingFieldPluginManager
->createInstance($field_plugin_definition['id']);
}
$row['config'] = $field_plugin
->buildConfigurationForm($form, $form_state);
// @TODO implement "lock/unlock" logic here:
// @TODO convert these to AJAX operations
$operations = [
'delete' => $this
->t('Delete'),
];
$defaults = [];
$row['ops'] = [
'#title' => t('Operations'),
'#type' => 'checkboxes',
'#options' => $operations,
'#default_value' => $defaults,
];
$row['drupal_field_type'] = [
'#type' => 'hidden',
'#value' => $field_plugin
->getPluginId(),
];
return $row;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Transform data from the operations column into the expected schema.
// Copy the submitted values so we don't run into problems with array
// indexing while removing delete field mappings.
$values = $form_state
->getValues();
if (empty($values['field_mappings'])) {
// No mappings have been added, no validation to be done.
return;
}
$key = $values['key'];
$key_mapped = FALSE;
foreach ($values['field_mappings'] as $i => $value) {
// If a field was deleted, delete it!
if (!empty($value['ops']['delete'])) {
$form_state
->unsetValue([
"field_mappings",
"{$i}",
]);
continue;
}
// Pass validation to field plugins before performing mapping validation.
$field_plugin = $this->entity
->getFieldMapping($value);
$sub_form_state = SubformState::createForSubform($form['field_mappings_wrapper']['field_mappings'][$i], $form, $form_state);
$field_plugin
->validateConfigurationForm($form['field_mappings_wrapper']['field_mappings'][$i], $sub_form_state);
// Send to drupal field plugin for additional validation.
if ($field_plugin
->config('salesforce_field') == $key) {
$key_mapped = TRUE;
}
}
if (!empty($key) && !$key_mapped) {
// Do not allow saving mapping when key field is not mapped.
$form_state
->setErrorByName('key', t('You must add the selected field to the field mapping in order set an Upsert Key.'));
}
}
/**
* Submit handler.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Need to transform the schema slightly to remove the "config" dereference.
// Also trigger submit handlers on plugins.
$form_state
->unsetValue([
'field_type',
'ops',
]);
$values =& $form_state
->getValues();
foreach ($values['field_mappings'] as $i => &$value) {
// Pass submit values to plugin submit handler.
$field_plugin = $this->entity
->getFieldMapping($value);
$sub_form_state = SubformState::createForSubform($form['field_mappings_wrapper']['field_mappings'][$i], $form, $form_state);
$field_plugin
->submitConfigurationForm($form['field_mappings_wrapper']['field_mappings'][$i], $sub_form_state);
$value = $value + $value['config'];
unset($value['config'], $value['ops']);
}
parent::submitForm($form, $form_state);
}
/**
* Ajax callback for adding a new field.
*/
public function fieldAddCallback($form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// Requires updating itself and the field map.
$response
->addCommand(new ReplaceCommand('#edit-field-mappings-wrapper', render($form['field_mappings_wrapper'])));
return $response;
}
/**
* Get an array of drupal types.
*/
protected function getDrupalTypeOptions($mapping) {
$field_plugins = $this->mappingFieldPluginManager
->getDefinitions();
$options = [];
foreach ($field_plugins as $definition) {
if (call_user_func([
$definition['class'],
'isAllowed',
], $mapping)) {
$options[$definition['id']] = $definition['label'];
}
}
return $options;
}
/**
* Get a field plugin of the given type.
*/
protected function getFieldPlugin($field_type) {
$field_plugins = $this->mappingFieldPluginManager
->getDefinitions();
return $field_plugins[$field_type];
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function | Gets the actual form array to be built. | 30 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SalesforceMappingFieldsForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
SalesforceMappingFieldsForm:: |
public | function | Ajax callback for adding a new field. | |
SalesforceMappingFieldsForm:: |
protected | function | Get an array of drupal types. | |
SalesforceMappingFieldsForm:: |
protected | function | Get a field plugin of the given type. | |
SalesforceMappingFieldsForm:: |
private | function | Helper function to return an empty row for the field mapping form. | |
SalesforceMappingFieldsForm:: |
private | function | Return an options array of field labels for any fields marked externalId. | |
SalesforceMappingFieldsForm:: |
public | function |
Submit handler. Overrides EntityForm:: |
|
SalesforceMappingFieldsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
SalesforceMappingFormBase:: |
protected | property | Salesforce client. | |
SalesforceMappingFormBase:: |
protected | property |
The mapping entity for this form. Overrides EntityForm:: |
|
SalesforceMappingFormBase:: |
protected | property | Mappable types service. | |
SalesforceMappingFormBase:: |
protected | property | Field plugin manager. | |
SalesforceMappingFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
SalesforceMappingFormBase:: |
protected | function | Test the Salesforce connection by issuing the given api call. | |
SalesforceMappingFormBase:: |
protected | function | Retreive Salesforce's information about an object type. | |
SalesforceMappingFormBase:: |
protected | function | Helper to retreive a list of object type options. | |
SalesforceMappingFormBase:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
1 |
SalesforceMappingFormBase:: |
public | function | SalesforceMappingFormBase constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |