You are here

public function ConfigFormBuilder::addDisableFieldConfigFormToEntityForm in Disable Field 8.2

Add the disable field config form to the given form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

string $form_id: The form id.

File

src/ConfigFormBuilder.php, line 60

Class

ConfigFormBuilder
Class ConfigFormAlter.

Namespace

Drupal\disable_field

Code

public function addDisableFieldConfigFormToEntityForm(array &$form, FormStateInterface $form_state, string $form_id) {
  if (!$this->currentUser
    ->hasPermission('administer disable field settings')) {
    return;
  }
  $role_options = [];
  $roles = $this->roleStorage
    ->loadMultiple();
  foreach ($roles as $role) {
    $role_options[$role
      ->id()] = $role
      ->label();
  }

  /** @var \Drupal\field\Entity\FieldConfig $field_config */
  $field_config = $form_state
    ->getFormObject()
    ->getEntity();
  $settings = $field_config
    ->getThirdPartySettings('disable_field');

  // Prepare group with fields for settings.
  $form['disable_field'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Disable Field Settings'),
    '#open' => TRUE,
    '#tree' => TRUE,
    '#weight' => 20,
    'add' => [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Disable this field on add content form?'),
    ],
    'edit' => [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Disable this field on edit content form?'),
    ],
  ];
  $form['disable_field']['add']['disable'] = [
    '#type' => 'select',
    '#options' => [
      'none' => $this
        ->t('Enable for all users'),
      'all' => $this
        ->t('Disable for all users'),
      'roles' => $this
        ->t('Disable for certain roles'),
      'roles_enable' => $this
        ->t('Enable for certain roles'),
    ],
    '#default_value' => !empty($settings['add_disable']) ? $settings['add_disable'] : 'none',
    '#required' => TRUE,
  ];
  $form['disable_field']['add']['roles'] = [
    '#type' => 'select',
    '#options' => $role_options,
    '#title' => $this
      ->t('Enable field on the add content form for next roles:'),
    '#multiple' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="disable_field[add][disable]"]' => [
          [
            'value' => 'roles',
          ],
          [
            'value' => 'roles_enable',
          ],
        ],
      ],
      'required' => [
        ':input[name="disable_field[add][disable]"]' => [
          [
            'value' => 'roles',
          ],
          [
            'value' => 'roles_enable',
          ],
        ],
      ],
    ],
    '#default_value' => !empty($settings['add_roles']) ? $settings['add_roles'] : [],
  ];
  $form['disable_field']['edit']['disable'] = [
    '#type' => 'select',
    '#options' => [
      'none' => $this
        ->t('Enable for all users'),
      'all' => $this
        ->t('Disable for all users'),
      'roles' => $this
        ->t('Disable for certain roles'),
      'roles_enable' => $this
        ->t('Enable for certain roles'),
    ],
    '#default_value' => !empty($settings['edit_disable']) ? $settings['edit_disable'] : 'none',
    '#required' => TRUE,
  ];
  $form['disable_field']['edit']['roles'] = [
    '#type' => 'select',
    '#options' => $role_options,
    '#title' => $this
      ->t('Disable field on the edit content form for next roles:'),
    '#multiple' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="disable_field[edit][disable]"]' => [
          [
            'value' => 'roles',
          ],
          [
            'value' => 'roles_enable',
          ],
        ],
      ],
      'required' => [
        ':input[name="disable_field[edit][disable]"]' => [
          [
            'value' => 'roles',
          ],
          [
            'value' => 'roles_enable',
          ],
        ],
      ],
    ],
    '#default_value' => !empty($settings['edit_roles']) ? $settings['edit_roles'] : [],
  ];
  $form['#validate'][] = [
    $this,
    'validateDisableFieldConfigForm',
  ];
  $form['#entity_builders'][] = [
    $this,
    'assignDisableFieldThirdPartySettingsToEntity',
  ];
}