You are here

public function DefaultSelection::buildConfigurationForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h....

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 complete form.

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

3 calls to DefaultSelection::buildConfigurationForm()
NodeSelection::buildConfigurationForm in core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php
Form constructor.
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.
3 methods override DefaultSelection::buildConfigurationForm()
NodeSelection::buildConfigurationForm in core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php
Form constructor.
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.

File

core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php, line 109
Contains \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection.

Class

DefaultSelection
Default plugin implementation of the Entity Reference Selection plugin.

Namespace

Drupal\Core\Entity\Plugin\EntityReferenceSelection

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $entity_type_id = $this->configuration['target_type'];
  $selection_handler_settings = $this->configuration['handler_settings'];
  $entity_type = $this->entityManager
    ->getDefinition($entity_type_id);
  $bundles = $this->entityManager
    ->getBundleInfo($entity_type_id);

  // Merge-in default values.
  $selection_handler_settings += array(
    // For the 'target_bundles' setting, a NULL value is equivalent to "allow
    // entities from any bundle to be referenced" and an empty array value is
    // equivalent to "no entities from any bundle can be referenced".
    'target_bundles' => NULL,
    'sort' => array(
      'field' => '_none',
    ),
    'auto_create' => FALSE,
  );
  if ($entity_type
    ->hasKey('bundle')) {
    $bundle_options = array();
    foreach ($bundles as $bundle_name => $bundle_info) {
      $bundle_options[$bundle_name] = $bundle_info['label'];
    }
    $form['target_bundles'] = array(
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Bundles'),
      '#options' => $bundle_options,
      '#default_value' => (array) $selection_handler_settings['target_bundles'],
      '#required' => TRUE,
      '#size' => 6,
      '#multiple' => TRUE,
      '#element_validate' => [
        [
          get_class($this),
          'elementValidateFilter',
        ],
      ],
    );
  }
  else {
    $form['target_bundles'] = array(
      '#type' => 'value',
      '#value' => array(),
    );
  }
  if ($entity_type
    ->isSubclassOf('\\Drupal\\Core\\Entity\\FieldableEntityInterface')) {
    $fields = array();
    foreach (array_keys($bundles) as $bundle) {
      $bundle_fields = array_filter($this->entityManager
        ->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)', array(
              '@label' => $field_definition
                ->getLabel(),
              '@column' => $column_name,
            ));
          }
        }
        else {
          $fields[$field_name] = $this
            ->t('@label', array(
            '@label' => $field_definition
              ->getLabel(),
          ));
        }
      }
    }
    $form['sort']['field'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Sort by'),
      '#options' => array(
        '_none' => $this
          ->t('- None -'),
      ) + $fields,
      '#ajax' => TRUE,
      '#limit_validation_errors' => array(),
      '#default_value' => $selection_handler_settings['sort']['field'],
    );
    $form['sort']['settings'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'entity_reference-settings',
        ),
      ),
      '#process' => [
        [
          EntityReferenceItem::class,
          'formProcessMergeParent',
        ],
      ],
    );
    if ($selection_handler_settings['sort']['field'] != '_none') {

      // Merge-in default values.
      $selection_handler_settings['sort'] += array(
        'direction' => 'ASC',
      );
      $form['sort']['settings']['direction'] = array(
        '#type' => 'select',
        '#title' => $this
          ->t('Sort direction'),
        '#required' => TRUE,
        '#options' => array(
          'ASC' => $this
            ->t('Ascending'),
          'DESC' => $this
            ->t('Descending'),
        ),
        '#default_value' => $selection_handler_settings['sort']['direction'],
      );
    }
  }
  return $form;
}