If your module depends on the service of another module , we do not have a very good way to get injection currently. You can't just add it as an argument. You would need to write something ContainerAware and that's ouch. Here's a patch.
Here's how it works:
arguments: ['@plugin.manager.entity']
tags:
- {name: optional_dependency, block_manager: '@plugin.manager.block' }
And then
function __construct(EntityManager $entity_manager, array $optional_dependencies = array()) {
$this->entityManager = $entity_manager;
if (isset($optional_dependencies['block_manager'])) {
$this->blockManager = $optional_dependencies['block_manager'];
}
}
function postSave() {
if ($this->blockManager) {
$this->blockManager->clearCachedDefinitions();
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 1978244_4.patch | 5.82 KB | chx |
| #3 | 1978244_3.patch | 5.73 KB | chx |
| #2 | 1978244_1.patch | 2.66 KB | chx |
| OptionalDependencyPass.patch | 2.9 KB | chx |
Comments
Comment #2
chx commentedComment #3
chx commentedAdded a test.
Comment #4
chx commentedAnd a comment on the new test method. Note that this patch nukes a needless duplicate in CoreBundle.
Comment #5
tim.plunkettI'm super +1 to this patch. But I'm nervous about this going in while Crell is away. Can we clean it up a bit and hope he's in a good mood when he returns?
----
Missing trailing .
I think this whole approach is awesome and will be very useful. But we should explain why this might be useful and how you might use it, maybe even add a @code block?
Missing {@inheritdoc}
It's reasonably clear what's going on, but this could use some inline documentation.
What did that blank line ever do to you? :)
Need to add a @param for the new one
This is supposed to have a one liner above the @return (with a blank in the middle)
Comment #6
msonnabaum commentedThis is a very unintuitive feature. An optional dependency?
The example given should be an event.
Comment #7
tim.plunkettTake for example menu_menu_delete():
When a menu is deleted, the block plugin cache must be cleared to remove all menu-blocks.
If this were an event, who would define the event? Who would subscribe to it?
Block module shouldn't need to know that menus can be deleted.
Menu module (or the menu entity storage controller, that invokes this hook) shouldn't worry if the block plugin it provides is in use.
Comment #8
msonnabaum commentedI started #1980212: Use cachetags to invalidate block definitions as an alternative solution to this issue.
Comment #9
Anonymous (not verified) commentedi like the suggestion in #8.
even more if we could do:
with $type == 'menu' for the menu delete case. i don't know enough about plugins/blocks to know if that is possible.
Comment #10
chx commentedThere are countless contrib modules calling each others APIs which are not always events, they can be simply, well, APIs.
Comment #11
Crell commentedI'm with Mark. This seems seriously weird. If you want to optionally integrate with another component, wouldn't either an event or just a direct observer object (that you or the other module can register) be cleaner?
Comment #12
lsmith77 commentedThere is
<argument type="service" id="foo" on-invalid="null" />.. IIRC for yaml the syntax is "?@foo" .. which just passes null if the service doesnt existComment #13
katbailey commented#12 is what I'm doing for the url generator's optional dependency on the request over in #1888424-158: Make Drupal's URL generation logic available to HttpKernel, and minimize code repetition/divergence - see the first hunk there.
Comment #16
Crell commentedBetween events and optional service dependencies (which we're already using anyway) I don't think this is necessary. Closing.