Browse Source

Refactored and removed EntityTypeInfo class.

pull/2/head
Nigel Banks 3 years ago
parent
commit
c5404c9fdc
  1. 32
      dgi_fixity.module
  2. 125
      src/EntityTypeInfo.php

32
dgi_fixity.module

@ -139,18 +139,38 @@ function dgi_fixity_cron() {
* Implements hook_entity_type_alter(). * Implements hook_entity_type_alter().
*/ */
function dgi_fixity_entity_type_alter(array &$entity_types) { function dgi_fixity_entity_type_alter(array &$entity_types) {
return \Drupal::service('class_resolver') /** @var \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity */
->getInstanceFromDefinition(EntityTypeInfo::class) $fixity = \Drupal::service('dgi_fixity.fixity_check');
->entityTypeAlter($entity_types); $supported_entity_types = $fixity->fromEntityTypes();
foreach ($supported_entity_types as $entity_type_id) {
$entity_type = &$entity_types[$entity_type_id];
$entity_type->setLinkTemplate('fixity-audit', "/fixity/$entity_type_id/{{$entity_type_id}}");
$entity_type->setLinkTemplate('fixity-check', "/fixity/$entity_type_id/{{$entity_type_id}}/check");
$entity_type->setFormClass('fixity-check', 'Drupal\dgi_fixity\Form\CheckForm');
}
} }
/** /**
* Implements hook_entity_operation(). * Implements hook_entity_operation().
*/ */
function dgi_fixity_entity_operation(EntityInterface $entity) { function dgi_fixity_entity_operation(EntityInterface $entity) {
return \Drupal::service('class_resolver') $current_user = \Drupal::service('current_user');
->getInstanceFromDefinition(EntityTypeInfo::class) $operations = [];
->entityOperation($entity); if ($entity->hasLinkTemplate('fixity-audit') && $current_user->hasPermission('view fixity checks')) {
$operations['fixity-audit'] = [
'title' => \t('Audit'),
'weight' => 10,
'url' => $entity->toUrl('fixity-audit'),
];
if ($entity->hasLinkTemplate('fixity-check') && $current_user->hasPermission('administer fixity checks')) {
$operations['fixity-check'] = [
'title' => \t('Check'),
'weight' => 13,
'url' => $entity->toUrl('fixity-check', ['query' => \Drupal::service('redirect.destination')->getAsArray()]),
];
}
}
return $operations;
} }
/** /**

125
src/EntityTypeInfo.php

@ -1,125 +0,0 @@
<?php
namespace Drupal\dgi_fixity;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Manipulates entity type information.
*
* This class contains primarily bridged hooks for compile-time or
* cache-clear-time hooks. Runtime hooks should be placed in EntityOperations.
*/
class EntityTypeInfo implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The redirect destination helper.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirect;
/**
* The fixity check service.
*
* @var \Drupal\dgi_fixity\FixityCheckServiceInterface
*/
protected $fixity;
/**
* EntityTypeInfo constructor.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The translation manager.
* @param \Drupal\Core\Session\AccountInterface $current_user
* Current user.
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect
* The redirect destination helper.
* @param \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity
* The fixity service.
*/
public function __construct(TranslationInterface $string_translation, AccountInterface $current_user, RedirectDestinationInterface $redirect, FixityCheckServiceInterface $fixity) {
$this->stringTranslation = $string_translation;
$this->currentUser = $current_user;
$this->redirect = $redirect;
$this->fixity = $fixity;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('string_translation'),
$container->get('current_user'),
$container->get('redirect.destination'),
$container->get('dgi_fixity.fixity_check'),
);
}
/**
* Gets fixity check links to appropriate entity types.
*
* This is an alter hook bridge.
*
* @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
* The master entity type list to alter.
*
* @see hook_entity_type_alter()
*/
public function entityTypeAlter(array &$entity_types) {
$supported_entity_types = $this->fixity->fromEntityTypes();
foreach ($supported_entity_types as $entity_type_id) {
$entity_type = &$entity_types[$entity_type_id];
$entity_type->setLinkTemplate('fixity-audit', "/fixity/$entity_type_id/{{$entity_type_id}}");
$entity_type->setLinkTemplate('fixity-check', "/fixity/$entity_type_id/{{$entity_type_id}}/check");
$entity_type->setFormClass('fixity-check', 'Drupal\dgi_fixity\Form\CheckForm');
}
}
/**
* Gets fixity operations on entities that support it.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity on which to define an operation.
*
* @return array
* An array of operation definitions.
*
* @see hook_entity_operation()
*/
public function entityOperation(EntityInterface $entity) {
$operations = [];
if ($entity->hasLinkTemplate('fixity-audit') && $this->currentUser->hasPermission('view fixity checks')) {
$operations['fixity-audit'] = [
'title' => $this->t('Audit'),
'weight' => 10,
'url' => $entity->toUrl('fixity-audit'),
];
if ($entity->hasLinkTemplate('fixity-check') && $this->currentUser->hasPermission('administer fixity checks')) {
$operations['fixity-check'] = [
'title' => $this->t('Check'),
'weight' => 13,
'url' => $entity->toUrl('fixity-check', ['query' => $this->redirect->getAsArray()]),
];
}
}
return $operations;
}
}
Loading…
Cancel
Save