You are here

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

Creates a table of encryption profiles in use.

Parameters

string $entity_type_id: The entity type ID.

Return value

array A render array of encryption profiles in use.

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

File

src/Form/EntityTypeForm.php, line 302

Class

EntityTypeForm
Configures field encryption on the entity type level.

Namespace

Drupal\field_encrypt\Form

Code

protected function getEncryptionProfilesTable($entity_type_id) {
  $form = [];
  $rows = [];
  $results = [];
  $base_fields = $entity_type_id ? $this->entityFieldManager
    ->getBaseFieldDefinitions($entity_type_id) : [];
  $profile_storage = $this->entityTypeManager
    ->getStorage('encryption_profile');
  $count_alias = 'count';
  $default_encryption_profile = $this
    ->config('field_encrypt.settings')
    ->get('encryption_profile');
  if ($entity_type_id && isset($base_fields[ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME])) {
    $query = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->getAggregateQuery()
      ->aggregate(ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME . '.encryption_profile', 'COUNT', NULL, $count_alias)
      ->groupBy(ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME . '.encryption_profile');
    if ($this->entityTypeManager
      ->getDefinition($entity_type_id)
      ->isRevisionable()) {
      $query
        ->allRevisions();
    }
    $results = $query
      ->execute();
  }
  foreach ($results as $result) {
    $row = [];
    $encryption_profile = $profile_storage
      ->load($result[ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME . '__encryption_profile']);
    $row[] = $encryption_profile
      ->label();
    $row[] = $result[$count_alias];
    $operations = [];
    if ($encryption_profile
      ->id() !== $default_encryption_profile) {
      $operations = [
        '#type' => 'operations',
        '#links' => [
          'update_encryption_profile' => [
            'title' => $this
              ->t('Update encryption profile'),
            'url' => Url::fromRoute('field_encrypt.update_encryption_profile_confirm', [
              'entity_type' => $entity_type_id,
              'encryption_profile' => $encryption_profile
                ->id(),
            ]),
          ],
        ],
      ];
    }
    $row[]['data'] = $operations;
    $rows[] = $row;
  }
  $form['encryption_profiles'] = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'encryption-profiles',
      ],
    ],
    '#header' => [
      $this
        ->t('Encryption profile'),
      $this
        ->t('Usages'),
      $this
        ->t('Operations'),
    ],
    '#title' => 'Encryption profiles',
    '#rows' => $rows,
    '#access' => !empty($rows),
    '#description' => $this
      ->t('If the field encryption profile is changed entities can be updated to use the new encryption profile.'),
  ];
  return $form;
}