Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
36 / 36 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Entity | |
100.00% |
36 / 36 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| process | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
8 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Drupal\visitors\Plugin\VisitorsEvent; |
| 4 | |
| 5 | use Drupal\Core\Config\Entity\ConfigEntityType; |
| 6 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 7 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
| 8 | use Drupal\Core\Routing\RouteMatchInterface; |
| 9 | use Drupal\visitors\Attribute\VisitorsEvent; |
| 10 | use Drupal\visitors\Plugin\VisitorsEventBase; |
| 11 | use Drupal\visitors\VisitorsEventPluginInterface; |
| 12 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 13 | |
| 14 | /** |
| 15 | * Plugin implementation for entity event. |
| 16 | */ |
| 17 | #[VisitorsEvent( |
| 18 | id: "entity", |
| 19 | label: "Entity", |
| 20 | weight: -10, |
| 21 | )] |
| 22 | class Entity extends VisitorsEventBase implements VisitorsEventPluginInterface, ContainerFactoryPluginInterface { |
| 23 | |
| 24 | /** |
| 25 | * Constructs a new Entity object. |
| 26 | * |
| 27 | * @param array $configuration |
| 28 | * A configuration array containing information about the plugin instance. |
| 29 | * @param string $plugin_id |
| 30 | * The plugin_id for the plugin instance. |
| 31 | * @param mixed $plugin_definition |
| 32 | * The plugin implementation definition. |
| 33 | * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch |
| 34 | * The route match service. |
| 35 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
| 36 | * The entity type manager. |
| 37 | */ |
| 38 | public function __construct( |
| 39 | array $configuration, |
| 40 | $plugin_id, |
| 41 | $plugin_definition, |
| 42 | protected readonly RouteMatchInterface $routeMatch, |
| 43 | protected readonly EntityTypeManagerInterface $entityTypeManager, |
| 44 | ) { |
| 45 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * {@inheritdoc} |
| 50 | */ |
| 51 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 52 | return new static( |
| 53 | $configuration, |
| 54 | $plugin_id, |
| 55 | $plugin_definition, |
| 56 | $container->get('current_route_match'), |
| 57 | $container->get('entity_type.manager'), |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * {@inheritdoc} |
| 63 | */ |
| 64 | public function process(array $context): ?array { |
| 65 | $route = $context['route']; |
| 66 | |
| 67 | if (strpos($route, 'entity.') !== 0) { |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | $return = []; |
| 72 | $entity_route = explode('.', $route); |
| 73 | // Get the entity type from the route. |
| 74 | $entity_type = $entity_route[1]; |
| 75 | $return['variables']['plugin_var_1'] = $entity_type; |
| 76 | // Implode the rest of the route. |
| 77 | $event = implode('.', array_slice($entity_route, 2)); |
| 78 | $return['event'] = $event; |
| 79 | |
| 80 | $entity = $this->routeMatch->getParameter($entity_type); |
| 81 | $entity_type_definition = $this->entityTypeManager->getDefinition($entity_type); |
| 82 | |
| 83 | // Check if it's a config entity. |
| 84 | $is_config_entity = $entity_type_definition instanceof ConfigEntityType; |
| 85 | $is_config_entity ? 'config' : 'content'; |
| 86 | |
| 87 | if ($is_config_entity) { |
| 88 | $return['variables']['plugin_var_2'] = $entity?->id(); |
| 89 | } |
| 90 | else { |
| 91 | $return['variables']['plugin_var_2'] = $entity?->bundle() ?? $entity_type; |
| 92 | $entity_id = $entity?->id(); |
| 93 | if ($entity_id) { |
| 94 | $return['variables']['plugin_int_1'] = $entity_id; |
| 95 | } |
| 96 | if ($entity_type_definition->isRevisionable()) { |
| 97 | $return['variables']['plugin_int_2'] = $entity?->getRevisionId(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | $forms = &drupal_static('visitors_form_alter') ?? []; |
| 102 | |
| 103 | if ($forms && count($forms) == 1) { |
| 104 | $form_id = array_keys($forms)[0]; |
| 105 | $base_form_id = array_values($forms)[0]; |
| 106 | $return['variables']['plugin_var_3'] = $base_form_id ?? $form_id; |
| 107 | $return['variables']['plugin_var_4'] = $form_id; |
| 108 | } |
| 109 | |
| 110 | return $return; |
| 111 | } |
| 112 | |
| 113 | } |