Coverage index

src/Plugin/VisitorsEvent/Entity.php

Lines: 100.00%

LineHitsSource
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
  ) {
457
    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) {
526
    return new static(
536
      $configuration,
546
      $plugin_id,
556
      $plugin_definition,
566
      $container->get('current_route_match'),
576
      $container->get('entity_type.manager'),
586
    );
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function process(array $context): ?array {
656
    $route = $context['route'];
66
676
    if (strpos($route, 'entity.') !== 0) {
681
      return NULL;
69
    }
70
715
    $return = [];
725
    $entity_route = explode('.', $route);
73
    // Get the entity type from the route.
745
    $entity_type = $entity_route[1];
755
    $return['variables']['plugin_var_1'] = $entity_type;
76
    // Implode the rest of the route.
775
    $event = implode('.', array_slice($entity_route, 2));
785
    $return['event'] = $event;
79
805
    $entity = $this->routeMatch->getParameter($entity_type);
815
    $entity_type_definition = $this->entityTypeManager->getDefinition($entity_type);
82
83
    // Check if it's a config entity.
845
    $is_config_entity = $entity_type_definition instanceof ConfigEntityType;
855
    $is_config_entity ? 'config' : 'content';
86
875
    if ($is_config_entity) {
881
      $return['variables']['plugin_var_2'] = $entity?->id();
89
    }
90
    else {
915
      $return['variables']['plugin_var_2'] = $entity?->bundle() ?? $entity_type;
925
      $entity_id = $entity?->id();
935
      if ($entity_id) {
945
        $return['variables']['plugin_int_1'] = $entity_id;
95
      }
965
      if ($entity_type_definition->isRevisionable()) {
974
        $return['variables']['plugin_int_2'] = $entity?->getRevisionId();
98
      }
99
    }
100
1015
    $forms = &drupal_static('visitors_form_alter') ?? [];
102
1035
    if ($forms && count($forms) == 1) {
1041
      $form_id = array_keys($forms)[0];
1051
      $base_form_id = array_values($forms)[0];
1061
      $return['variables']['plugin_var_3'] = $base_form_id ?? $form_id;
1071
      $return['variables']['plugin_var_4'] = $form_id;
108
    }
109
1105
    return $return;
111
  }
112
113
}