Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
In Workbench Moderation all hook implementations were abstracted out into services. When moving to core as Content Moderation we decided that as these were not for reuse, overriding, or extending they should be just plain classes. However now looking at them again plain classes are not the most friendly approach, and if we're looking for something to
Proposed resolution
Use ClassResolver
.
For example:
/**
* Implements hook_entity_base_field_info().
*/
function content_moderation_entity_base_field_info(EntityTypeInterface $entity_type) {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityBaseFieldInfo($entity_type);
}
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#18 | use_class_resolver_for-2781207-18.patch | 10.23 KB | timmillwood |
#16 | interdiff.txt | 644 bytes | timmillwood |
Comments
Comment #2
dawehnerSo do you propose to use the
ContainerInjectionInterface
as we have it for controllers?Comment #3
timmillwood@dawehner - I think that would be a good thing, yes.
Comment #4
timmillwoodPatch updates
ContentPreprocess
,EntityOperations
, andEntityTypeInfo
to implementContainerInjectionInterface
. TheCreate
method is added inline with the interface spec. Then_content_moderation_class_resolver
is added to content_moderation.module to resolve the class and statically cache it.Comment #5
dawehnerInstead of doing it here we could introduce a ClassResolverWithState class which does the same and ensures that there is just one instance of it, by having this special class resolver in the container.
Comment #6
timmillwood@dawehner - briefly discussed this with Alex on IRC
There's a number of ways to do it, I was thinking it could just be a different method within
\Drupal\Core\DependencyInjection\ClassResolver
, or it could equally be an update of the current method.Comment #7
naveenvalechaFixing component
Comment #9
timmillwoodRemoving the static cache.
Comment #10
dawehnerThis looks basically how I expect it to look like. Thank you @timmillwood
Its a clear scope of improvements.
Comment #12
dawehner.
Comment #13
timmillwoodSimple re-roll.
Comment #14
timmillwoodIt's not fixed, it needs review!
Comment #16
timmillwoodFixing issue.
Comment #18
timmillwoodRe-rolling
Comment #19
dawehnerLooks great for me.
Comment #20
alexpottCommitted and pushed fe33c4b to 8.3.x and 0f7d56e to 8.2.x. Thanks!
I think this pattern gets us the best of both worlds. A module's hook implementations are not public services (and this is good because they are not API) and the dependencies are encapsulated in the class.
Removing unused use on commit.