Pull merge.
[yaffs-website] / web / core / modules / workspaces / src / EntityTypeInfo.php
1 <?php
2
3 namespace Drupal\workspaces;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8
9 /**
10  * Manipulates entity type information.
11  *
12  * This class contains primarily bridged hooks for compile-time or
13  * cache-clear-time hooks. Runtime hooks should be placed in EntityOperations.
14  *
15  * @internal
16  */
17 class EntityTypeInfo implements ContainerInjectionInterface {
18
19   /**
20    * The entity type manager service.
21    *
22    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23    */
24   protected $entityTypeManager;
25
26   /**
27    * The workspace manager service.
28    *
29    * @var \Drupal\workspaces\WorkspaceManagerInterface
30    */
31   protected $workspaceManager;
32
33   /**
34    * Constructs a new EntityTypeInfo instance.
35    *
36    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
37    *   The entity type manager service.
38    * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
39    *   The workspace manager service.
40    */
41   public function __construct(EntityTypeManagerInterface $entity_type_manager, WorkspaceManagerInterface $workspace_manager) {
42     $this->entityTypeManager = $entity_type_manager;
43     $this->workspaceManager = $workspace_manager;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public static function create(ContainerInterface $container) {
50     return new static(
51       $container->get('entity_type.manager'),
52       $container->get('workspaces.manager')
53     );
54   }
55
56   /**
57    * Adds the "EntityWorkspaceConflict" constraint to eligible entity types.
58    *
59    * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
60    *   An associative array of all entity type definitions, keyed by the entity
61    *   type name. Passed by reference.
62    *
63    * @see hook_entity_type_build()
64    */
65   public function entityTypeBuild(array &$entity_types) {
66     foreach ($entity_types as $entity_type) {
67       if ($this->workspaceManager->isEntityTypeSupported($entity_type)) {
68         $entity_type->addConstraint('EntityWorkspaceConflict');
69       }
70     }
71   }
72
73   /**
74    * Alters field plugin definitions.
75    *
76    * @param array[] $definitions
77    *   An array of field plugin definitions.
78    *
79    * @see hook_field_info_alter()
80    */
81   public function fieldInfoAlter(&$definitions) {
82     if (isset($definitions['entity_reference'])) {
83       $definitions['entity_reference']['constraints']['EntityReferenceSupportedNewEntities'] = [];
84     }
85   }
86
87 }