EncryptionProfileListBuilder.php in Encrypt 8.3
File
src/Controller/EncryptionProfileListBuilder.php
View source
<?php
namespace Drupal\encrypt\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EncryptionProfileListBuilder extends ConfigEntityListBuilder {
protected $config;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ConfigFactoryInterface $config_factory) {
parent::__construct($entity_type, $storage);
$this->config = $config_factory
->get('encrypt.settings');
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('config.factory'));
}
public function buildHeader() {
$header['label'] = $this
->t('Label');
$header['encryption_method'] = $this
->t('Encryption method');
$header['key'] = $this
->t('Key');
if ($this->config
->get('check_profile_status')) {
$header['status'] = $this
->t('Status');
}
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity
->label();
if ($encryption_method = $entity
->getEncryptionMethod()) {
$row['encryption_method'] = $encryption_method
->getLabel();
}
else {
$row['encryption_method'] = $this
->t('Error loading encryption method');
}
if ($key = $entity
->getEncryptionKey()) {
$row['key'] = $key
->label();
}
else {
$row['key'] = $this
->t('Error loading key');
}
if ($this->config
->get('check_profile_status')) {
$errors = $entity
->validate();
$warnings = [];
if ($encryption_method
->isDeprecated()) {
$warnings[] = $this
->t('The encryption plugin used in this encryption profile is deprecated.');
}
if (!empty($errors)) {
$row['status']['data'] = [
'#theme' => 'item_list',
'#items' => $errors,
'#attributes' => [
"class" => [
"color-error",
],
],
];
}
elseif (!empty($warnings)) {
$row['status']['data'] = [
'#theme' => 'item_list',
'#items' => $warnings,
'#attributes' => [
"class" => [
"color-warning",
],
],
];
}
else {
$row['status'] = $this
->t('OK');
}
}
return $row + parent::buildRow($entity);
}
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity
->hasLinkTemplate('test-form')) {
$operations['test'] = [
'title' => $this
->t('Test'),
'weight' => 30,
'url' => $entity
->toUrl('test-form'),
];
}
return $operations;
}
public function render() {
$build = parent::render();
$build['table']['#empty'] = $this
->t('No encryption profiles are available. <a href=":link">Add a profile</a>.', [
':link' => Url::fromRoute('entity.encryption_profile.add_form')
->toString(),
]);
return $build;
}
}