DependencySerializationTrait
is used to avoid that a service contained in a class property is fully serialized when that class instance is serialized. Instead, the service identifier is serialized within the class instance.
DependencySerializationTrait
isn't used to add dependencies to a class that uses Dependency Injection. If you are editing an existing access control handler class (which isn't the correct thing to do when the class is implemented by Drupal core or a contributed module hosted on drupal.org) to add more dependencies, you should edit its createInstance()
method, if it exists, or add it. For example, the WorkflowAccessControlHandler
class uses the following code, to inject the dependencies it needs.
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('plugin.manager.workflows.type'));
}
/**
* Constructs the workflow access control handler instance.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Component\Plugin\PluginManagerInterface $workflow_type_manager
* The workflow type plugin manager.
*/
public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $workflow_type_manager) {
parent::__construct($entity_type);
$this->workflowTypeManager = $workflow_type_manager;
}
The correct way to alter an access control handler used by an entity is implementing hook_entity_type_build()
and change the access control handler with EntityTypeInterface::setAccessClass()
. For example, for the Node entity, I could use the following code.
function book_entity_type_build(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
$entity_types['node']->setAccessClass('Drupal\\mymodule\\Access\\ExtendedNodeAccessControlHandler');
}