You are here

public function EntityFlagActionForm::buildForm in farmOS 2.x

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 ConfirmFormBase::buildForm

File

modules/core/flag/src/Form/EntityFlagActionForm.php, line 152

Class

EntityFlagActionForm
Provides an entity flag action form.

Namespace

Drupal\farm_flag\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
  $this->entityType = $this->entityTypeManager
    ->getDefinition($entity_type_id);
  $this->entities = $this->tempStore
    ->get($this->user
    ->id() . ':' . $entity_type_id);
  if (empty($entity_type_id) || empty($this->entities)) {
    return new RedirectResponse($this
      ->getCancelUrl()
      ->setAbsolute()
      ->toString());
  }

  // Get allowed values for the selected entities.
  // We find the intersection of all the allowed values to ensure that
  // disallowed flags cannot be assigned.
  $allowed_values = [];
  $base_field_definitions = $this->entityFieldManager
    ->getBaseFieldDefinitions($entity_type_id);
  if (!empty($base_field_definitions['flag'])) {
    foreach ($this->entities as $entity) {
      $entity_allowed_values = farm_flag_field_allowed_values($base_field_definitions['flag'], $entity);
      if (empty($allowed_values)) {
        $allowed_values = $entity_allowed_values;
      }
      else {
        $allowed_values = array_intersect_assoc($allowed_values, $entity_allowed_values);
      }
    }
  }
  $form['flags'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Flag'),
    '#description' => $this
      ->t('Select the flags that should be attached to the record(s).'),
    '#options' => $allowed_values,
    '#multiple' => TRUE,
  ];
  $form['operation'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Append or replace'),
    '#description' => $this
      ->t('Select "Append" if you want to add flags to the records, but keep the existing flags. Select "Replace" if you want to replace existing flags with the ones specified above.'),
    '#options' => [
      'append' => $this
        ->t('Append'),
      'replace' => $this
        ->t('Replace'),
    ],
    '#default_value' => 'append',
    '#required' => TRUE,
  ];
  return parent::buildForm($form, $form_state);
}