public function DefaultSelection::buildConfigurationForm in Drupal 8
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::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 SelectionPluginBase::buildConfigurationForm
3 calls to DefaultSelection::buildConfigurationForm()
- TermSelection::buildConfigurationForm in core/
modules/ taxonomy/ src/ Plugin/ EntityReferenceSelection/ TermSelection.php - Form constructor.
- UserSelection::buildConfigurationForm in core/
modules/ user/ src/ Plugin/ EntityReferenceSelection/ UserSelection.php - Form constructor.
- WorkspaceSelection::buildConfigurationForm in core/
modules/ workspaces/ src/ Plugin/ EntityReferenceSelection/ WorkspaceSelection.php - Form constructor.
3 methods override DefaultSelection::buildConfigurationForm()
- TermSelection::buildConfigurationForm in core/
modules/ taxonomy/ src/ Plugin/ EntityReferenceSelection/ TermSelection.php - Form constructor.
- UserSelection::buildConfigurationForm in core/
modules/ user/ src/ Plugin/ EntityReferenceSelection/ UserSelection.php - Form constructor.
- WorkspaceSelection::buildConfigurationForm in core/
modules/ workspaces/ src/ Plugin/ EntityReferenceSelection/ WorkspaceSelection.php - Form constructor.
File
- core/
lib/ Drupal/ Core/ Entity/ Plugin/ EntityReferenceSelection/ DefaultSelection.php, line 179
Class
- DefaultSelection
- Default plugin implementation of the Entity Reference Selection plugin.
Namespace
Drupal\Core\Entity\Plugin\EntityReferenceSelectionCode
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$configuration = $this
->getConfiguration();
$entity_type_id = $configuration['target_type'];
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$bundles = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
if ($entity_type
->hasKey('bundle')) {
$bundle_options = [];
foreach ($bundles as $bundle_name => $bundle_info) {
$bundle_options[$bundle_name] = $bundle_info['label'];
}
natsort($bundle_options);
$form['target_bundles'] = [
'#type' => 'checkboxes',
'#title' => $entity_type
->getBundleLabel(),
'#options' => $bundle_options,
'#default_value' => (array) $configuration['target_bundles'],
'#required' => TRUE,
'#size' => 6,
'#multiple' => TRUE,
'#element_validate' => [
[
get_class($this),
'elementValidateFilter',
],
],
'#ajax' => TRUE,
'#limit_validation_errors' => [],
];
$form['target_bundles_update'] = [
'#type' => 'submit',
'#value' => $this
->t('Update form'),
'#limit_validation_errors' => [],
'#attributes' => [
'class' => [
'js-hide',
],
],
'#submit' => [
[
EntityReferenceItem::class,
'settingsAjaxSubmit',
],
],
];
}
else {
$form['target_bundles'] = [
'#type' => 'value',
'#value' => [],
];
}
if ($entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
$fields = [];
foreach (array_keys($bundles) as $bundle) {
$bundle_fields = array_filter($this->entityFieldManager
->getFieldDefinitions($entity_type_id, $bundle), function ($field_definition) {
return !$field_definition
->isComputed();
});
foreach ($bundle_fields as $field_name => $field_definition) {
/* @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
$columns = $field_definition
->getFieldStorageDefinition()
->getColumns();
// If there is more than one column, display them all, otherwise just
// display the field label.
// @todo: Use property labels instead of the column name.
if (count($columns) > 1) {
foreach ($columns as $column_name => $column_info) {
$fields[$field_name . '.' . $column_name] = $this
->t('@label (@column)', [
'@label' => $field_definition
->getLabel(),
'@column' => $column_name,
]);
}
}
else {
$fields[$field_name] = $this
->t('@label', [
'@label' => $field_definition
->getLabel(),
]);
}
}
}
$form['sort']['field'] = [
'#type' => 'select',
'#title' => $this
->t('Sort by'),
'#options' => $fields,
'#ajax' => TRUE,
'#empty_value' => '_none',
'#sort_options' => TRUE,
'#limit_validation_errors' => [],
'#default_value' => $configuration['sort']['field'],
];
if ($entity_type
->hasKey('bundle')) {
$form['sort']['field']['#states'] = [
'visible' => [
':input[name^="settings[handler_settings][target_bundles]["]' => [
'checked' => TRUE,
],
],
];
}
$form['sort']['settings'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'entity_reference-settings',
],
],
'#process' => [
[
EntityReferenceItem::class,
'formProcessMergeParent',
],
],
];
if ($configuration['sort']['field'] != '_none') {
$form['sort']['settings']['direction'] = [
'#type' => 'select',
'#title' => $this
->t('Sort direction'),
'#required' => TRUE,
'#options' => [
'ASC' => $this
->t('Ascending'),
'DESC' => $this
->t('Descending'),
],
'#default_value' => $configuration['sort']['direction'],
];
}
}
$form['auto_create'] = [
'#type' => 'checkbox',
'#title' => $this
->t("Create referenced entities if they don't already exist"),
'#default_value' => $configuration['auto_create'],
'#weight' => -2,
];
if ($entity_type
->hasKey('bundle')) {
$bundles = array_intersect_key($bundle_options, array_filter((array) $configuration['target_bundles']));
$form['auto_create_bundle'] = [
'#type' => 'select',
'#title' => $this
->t('Store new items in'),
'#options' => $bundles,
'#default_value' => $configuration['auto_create_bundle'],
'#access' => count($bundles) > 1,
'#states' => [
'visible' => [
':input[name="settings[handler_settings][auto_create]"]' => [
'checked' => TRUE,
],
],
],
'#weight' => -1,
];
}
return $form;
}