You are here

protected function EntityTypeForm::getPropertiesForm in Field Encryption 3.0.x

Creates properties checkboxes for each base field.

Parameters

array $base_fields: A array of base fields labels that can be encrypted. Keyed by base field name.

string $entity_type_id: The entity type ID.

Return value

array The form array containing the checkboxes.

1 call to EntityTypeForm::getPropertiesForm()
EntityTypeForm::buildForm in src/Form/EntityTypeForm.php
Form constructor.

File

src/Form/EntityTypeForm.php, line 162

Class

EntityTypeForm
Configures field encryption on the entity type level.

Namespace

Drupal\field_encrypt\Form

Code

protected function getPropertiesForm(array $base_fields, $entity_type_id) {

  // Early return if there is nought to do.
  if (!$entity_type_id || empty($base_fields)) {
    return [];
  }

  // Get information required to build a property field for each base field.
  $form = [];
  $base_field_definitions = $this->entityFieldManager
    ->getBaseFieldDefinitions($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);
  $default_values = $field_encrypt_settings ? $field_encrypt_settings
    ->getBaseFields() : [];
  $default_properties = $this
    ->config('field_encrypt.settings')
    ->get('default_properties');
  foreach ($base_fields as $base_field => $base_field_label) {
    $properties = [];
    $definitions = $base_field_definitions[$base_field]
      ->getPropertyDefinitions();
    $field_type = $base_field_definitions[$base_field]
      ->getType();
    foreach ($definitions as $property => $definition) {
      $properties[$property] = $definition
        ->getLabel();
    }
    $field_encrypt_default = isset($default_properties[$field_type]) ? $default_properties[$field_type] : [];
    $form[$base_field] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('@label properties', [
        '@label' => $base_field_label,
      ]),
      '#description' => $this
        ->t('Specify the base field properties to encrypt. If none are selected the base field will not be encrypted.'),
      '#options' => $properties,
      '#default_value' => $default_values[$base_field] ?? $field_encrypt_default,
      '#states' => [
        'visible' => [
          ':input[name="base_fields[' . $base_field . ']"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  return $form;
}