View source
<?php
namespace Drupal\field_encrypt\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldOverviewController extends ControllerBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function overview() {
$encrypted_fields = $this
->getEncryptedFields();
$build['table'] = array(
'#type' => 'table',
'#header' => [
'field_name' => $this
->t('Field'),
'entity_type' => $this
->t('Entity type'),
'properties' => $this
->t('Properties'),
'encryption_profile' => $this
->t('Encryption profile'),
'count' => $this
->t('# encrypted field values'),
'operations' => $this
->t('Operations'),
],
'#title' => 'Overview of encrypted fields',
'#rows' => array(),
'#empty' => $this
->t('There are no encrypted fields.'),
);
foreach ($encrypted_fields as $encrypted_field) {
$properties = $encrypted_field
->getThirdPartySetting('field_encrypt', 'properties', []);
$entity_type = $encrypted_field
->getTargetEntityTypeId();
$field_name = $encrypted_field
->getName();
$row = [
'field_name' => $field_name,
'entity_type' => $entity_type,
'properties' => [
'data' => [
'#theme' => 'item_list',
'#items' => array_filter($properties),
],
],
'encryption_profile' => $encrypted_field
->getThirdPartySetting('field_encrypt', 'encryption_profile', ''),
'count' => $this
->getEncryptedFieldValueCount($entity_type, $field_name),
'operations' => [
'data' => [
'#type' => 'operations',
'#links' => [
'decrypt' => [
'title' => $this
->t('Decrypt'),
'url' => Url::fromRoute('field_encrypt.field_decrypt_confirm', [
'entity_type' => $entity_type,
'field_name' => $field_name,
]),
],
],
],
],
];
$build['table']['#rows'][$encrypted_field
->id()] = $row;
}
return $build;
}
protected function getEncryptedFields() {
$encrypted_fields = [];
$storage = $this->entityTypeManager
->getStorage('field_storage_config');
$fields = $storage
->loadMultiple();
foreach ($fields as $field) {
if ($field
->getThirdPartySetting('field_encrypt', 'encrypt', FALSE) == TRUE) {
$encrypted_fields[] = $field;
}
}
return $encrypted_fields;
}
protected function getEncryptedFieldValueCount($entity_type, $field_name) {
$query = $this->entityTypeManager
->getStorage('encrypted_field_value')
->getQuery()
->condition('entity_type', $entity_type)
->condition('field_name', $field_name);
$values = $query
->execute();
return count($values);
}
}