You are here

public function EntityTypeForm::buildForm in Field Encryption 3.0.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 FormInterface::buildForm

File

src/Form/EntityTypeForm.php, line 68

Class

EntityTypeForm
Configures field encryption on the entity type level.

Namespace

Drupal\field_encrypt\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
  if ($this
    ->config('field_encrypt.settings')
    ->get('encryption_profile') === '') {
    $this
      ->messenger()
      ->addError('Select an encryption profile before configuring entity types.');
    return $this
      ->redirect('field_encrypt.settings');
  }
  if (empty($entity_type_id)) {
    $entity_type_id = NULL;
  }
  $triggering_element = $form_state
    ->getTriggeringElement();
  if ($triggering_element) {
    $entity_type_id = $form_state
      ->getUserInput()['entity_type'];
  }
  $options = [];
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type) {

    // Only content entity types support encryption.
    if ($entity_type instanceof ContentEntityTypeInterface) {
      $options[$entity_type
        ->id()] = $entity_type
        ->getLabel();
    }
  }

  // Sort the entity types to make it easier to find the entity type to
  // configure.
  natsort($options);
  $form['entity_type'] = [
    '#title' => $this
      ->t('Select a entity type to configure'),
    '#type' => 'select',
    '#options' => $options,
    '#required' => TRUE,
    '#default_value' => $entity_type_id,
    '#ajax' => [
      'callback' => '::updateBaseFields',
      'wrapper' => 'js-edit-base-fields',
    ],
    // Disable this field when not using javascript and the entity type is
    // set.
    '#disabled' => !empty($entity_type_id) && !$triggering_element,
  ];
  $default_value = [];
  $possible_fields = [];
  if ($entity_type_id) {

    /** @var \Drupal\field_encrypt\Entity\FieldEncryptEntityType $field_encrypt_settings */
    $field_encrypt_settings = $this->entityTypeManager
      ->getStorage('field_encrypt_entity_type')
      ->load($entity_type_id);
    if ($field_encrypt_settings) {
      $default_value = array_keys($field_encrypt_settings
        ->getBaseFields());
      $default_value = array_combine($default_value, $default_value);
    }
    $possible_fields = $this
      ->getBaseFields($entity_type_id);
  }
  $form['entity_type_container'] = [
    '#type' => 'container',
    '#prefix' => '<div id="js-edit-base-fields">',
    '#suffix' => '</div>',
  ];
  $form['entity_type_container']['encryption_profiles'] = $this
    ->getEncryptionProfilesTable($entity_type_id);
  $form['entity_type_container']['base_fields'] = [
    '#type' => 'checkboxes',
    '#title' => $entity_type_id ? $this
      ->t('Base fields to encrypt') : '',
    '#default_value' => $default_value,
    '#value' => $default_value,
    '#options' => $possible_fields,
    '#description' => $entity_type_id ? $this
      ->t('Note that encrypted fields can not be filtered on in SQL queries or joined against.') : '',
  ];
  $form['entity_type_container']['base_field_properties'] = $this
    ->getPropertiesForm($possible_fields, $entity_type_id);
  $form['entity_type_container']['base_field_properties']['#tree'] = TRUE;
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save configuration'),
    '#button_type' => 'primary',
  ];

  // By default, render the form using system-config-form.html.twig.
  $form['#theme'] = 'system_config_form';
  return $form;
}