Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 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 | 7 | 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 | 6 | return new static( |
| 53 | 6 | $configuration, |
| 54 | 6 | $plugin_id, |
| 55 | 6 | $plugin_definition, |
| 56 | 6 | $container->get('current_route_match'), |
| 57 | 6 | $container->get('entity_type.manager'), |
| 58 | 6 | ); |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * {@inheritdoc} | |
| 63 | */ | |
| 64 | public function process(array $context): ?array { | |
| 65 | 6 | $route = $context['route']; |
| 66 | ||
| 67 | 6 | if (strpos($route, 'entity.') !== 0) { |
| 68 | 1 | return NULL; |
| 69 | } | |
| 70 | ||
| 71 | 5 | $return = []; |
| 72 | 5 | $entity_route = explode('.', $route); |
| 73 | // Get the entity type from the route. | |
| 74 | 5 | $entity_type = $entity_route[1]; |
| 75 | 5 | $return['variables']['plugin_var_1'] = $entity_type; |
| 76 | // Implode the rest of the route. | |
| 77 | 5 | $event = implode('.', array_slice($entity_route, 2)); |
| 78 | 5 | $return['event'] = $event; |
| 79 | ||
| 80 | 5 | $entity = $this->routeMatch->getParameter($entity_type); |
| 81 | 5 | $entity_type_definition = $this->entityTypeManager->getDefinition($entity_type); |
| 82 | ||
| 83 | // Check if it's a config entity. | |
| 84 | 5 | $is_config_entity = $entity_type_definition instanceof ConfigEntityType; |
| 85 | 5 | $is_config_entity ? 'config' : 'content'; |
| 86 | ||
| 87 | 5 | if ($is_config_entity) { |
| 88 | 1 | $return['variables']['plugin_var_2'] = $entity?->id(); |
| 89 | } | |
| 90 | else { | |
| 91 | 5 | $return['variables']['plugin_var_2'] = $entity?->bundle() ?? $entity_type; |
| 92 | 5 | $entity_id = $entity?->id(); |
| 93 | 5 | if ($entity_id) { |
| 94 | 5 | $return['variables']['plugin_int_1'] = $entity_id; |
| 95 | } | |
| 96 | 5 | if ($entity_type_definition->isRevisionable()) { |
| 97 | 4 | $return['variables']['plugin_int_2'] = $entity?->getRevisionId(); |
| 98 | } | |
| 99 | } | |
| 100 | ||
| 101 | 5 | $forms = &drupal_static('visitors_form_alter') ?? []; |
| 102 | ||
| 103 | 5 | if ($forms && count($forms) == 1) { |
| 104 | 1 | $form_id = array_keys($forms)[0]; |
| 105 | 1 | $base_form_id = array_values($forms)[0]; |
| 106 | 1 | $return['variables']['plugin_var_3'] = $base_form_id ?? $form_id; |
| 107 | 1 | $return['variables']['plugin_var_4'] = $form_id; |
| 108 | } | |
| 109 | ||
| 110 | 5 | return $return; |
| 111 | } | |
| 112 | ||
| 113 | } |