You are here

protected function BulkEditFormTrait::getSelectorForm in Views Bulk Edit 8.2

Builds the selector form.

Given an entity form, create a selector form to provide options to update values.

Parameters

string $entity_type_id: Entity type ID.

string $bundle: The bundle machine name.

array $form: The form we're building the selection options for.

Return value

array The new selector form.

1 call to BulkEditFormTrait::getSelectorForm()
BulkEditFormTrait::getBundleForm in src/Form/BulkEditFormTrait.php
Gets the form for this entity display.

File

src/Form/BulkEditFormTrait.php, line 236

Class

BulkEditFormTrait
Common methods for Views Bulk Edit forms.

Namespace

Drupal\views_bulk_edit\Form

Code

protected function getSelectorForm($entity_type_id, $bundle, array &$form) {
  $selector['_field_selector'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Select fields to change'),
    '#weight' => -50,
    '#tree' => TRUE,
    '#attributes' => [
      'class' => [
        'vbe-selector-fieldset',
      ],
    ],
  ];
  $definitions = $this->entityFieldManager
    ->getFieldDefinitions($entity_type_id, $bundle);
  foreach (Element::children($form) as $key) {
    if (isset($form[$key]['#access']) && !$form[$key]['#access']) {
      continue;
    }
    if ($key == '_field_selector' || !($element =& $this
      ->findFormElement($form[$key]))) {
      continue;
    }
    if (!$definitions[$key]
      ->isDisplayConfigurable('form')) {
      $element['#access'] = FALSE;
      continue;
    }

    // Modify the referenced element a bit so it doesn't
    // cause errors and returns correct data structure.
    $element['#required'] = FALSE;
    $element['#tree'] = TRUE;

    // Add the toggle field to the form.
    $selector['_field_selector'][$key] = [
      '#type' => 'checkbox',
      '#title' => $element['#title'],
      '#weight' => isset($form[$key]['#weight']) ? $form[$key]['#weight'] : 0,
      '#tree' => TRUE,
    ];

    // Force the original value to be hidden unless the checkbox is enabled.
    $form[$key]['#states'] = [
      'visible' => [
        sprintf('[name="%s[%s][_field_selector][%s]"]', $entity_type_id, $bundle, $key) => [
          'checked' => TRUE,
        ],
      ],
    ];

    // Add options.
    $options = [];
    $options['replace'] = $this
      ->t('Replace the current value');
    if (in_array($definitions[$key]
      ->getType(), [
      'string',
      'string_long',
      'text',
      'text_long',
    ])) {
      $options['append'] = $this
        ->t('Append to the current value');
    }
    if ($definitions[$key]
      ->getFieldStorageDefinition()
      ->getCardinality() !== 1) {
      $options['new'] = $this
        ->t('Add a new value to the multivalue field');
    }
    $option_keys = array_keys($options);
    $form["{$key}_change_method"] = [
      '#title' => $this
        ->t('Change method'),
      '#type' => count($options) > 1 ? 'radios' : 'hidden',
      '#options' => $options,
      '#default_value' => reset($option_keys),
      '#states' => count($options) > 1 ? $form[$key]['#states'] : [],
      '#weight' => isset($form[$key]['#weight']) ? $form[$key]['#weight'] + 0.01 : 0,
    ];
  }
  if (empty(Element::children($selector['_field_selector']))) {
    $selector['_field_selector']['#title'] = $this
      ->t('There are no fields available to modify');
  }
  return $selector;
}