Problem/Motivation

DefaultPluginManager caches plugin definitions and supports cache tags out of the box. However, it does not implement any API to expose this information, so external code cannot use this metadata to properly control cache invalidation.

Proposed resolution

Make DefaultPluginManager implement CacheableDependencyInterface.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Xano created an issue. See original summary.

Xano’s picture

Status: Active » Needs review
FileSize
4.16 KB
Xano’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 2: drupal_2720813_2.patch, failed testing.

Wim Leers’s picture

Issue tags: +D8 cacheability
Xano’s picture

Status: Needs work » Needs review
FileSize
752 bytes
3.42 KB

Too bad we can't document this on the interface. Any suggestions for where else this would fit?

dawehner’s picture

So what I'm wondering, does this mean we have issues in existing core which should use that?

Xano’s picture

Yes. Anything based on plugin definitions has no cacheability metadata associated with it, except cached plugin definitions which in many cases use cache tags, but those are internal to plugin managers and not exposed to other code. This patch builds on the existing cache tag functionality to expose it.

This is important for derivative plugins, for instance. Their definitions don't just change when files are updated, but also as a result of CRUD operations on the config entities from which those definitions are derived.

I wonder if we should add Drupal\Core\Plugin\Discovery\CachedDiscoveryInterface extends Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface, Drupal\Core\Cache\CacheableDependencyInterface to clearly indicate that if you cache definitions, you'd better tell other code how to interact with those caches.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

+1 for the principle.

The code looks great.

+++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php
@@ -301,4 +302,40 @@ public function testGetDefinitionsWithoutRequiredInterface() {
+    $this->assertInternalType('array', $cache_contexts);
...
+      $this->assertInternalType('string', $cache_context);
...
+    $this->assertInternalType('array', $cache_tags);
...
+      $this->assertInternalType('string', $cache_tag);
...
+    $this->assertInternalType('int', $cache_max_age);

Cool, I didn't know this PHPUnit assertion!

catch’s picture

Status: Reviewed & tested by the community » Needs review

Is there a concrete use case for core that could have a follow-up issue?

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

Yes, there are plenty:

  1. where the 'entity_types' cache tag is currently being added manually, it should use EntityTypeManager::getCacheTags()
  2. 'entity_field_info'EntityFieldManager::getCacheTags()
  3. 'breakpoints'BreakpointManager::getCacheTags()
  4. 'contextual_links_plugins'ContextualLinkManager::getCacheTags()
  5. 'local_action'LocalActionManager::getCacheTags()
  6. 'local_task'LocalTaskManager::getCacheTags()

et cetera. And in fact, any plugin manager that doesn't yet specify a cache tag is kinda buggy in that respect, because AFAICT there's no reason for it not to have cache tags. Which means that anything depending on those plugins currently has incomplete cacheability metadata.

alexpott’s picture

Version: 8.1.x-dev » 8.2.x-dev
Status: Reviewed & tested by the community » Fixed

As this is an API addition I think we shouldn't add this to 8.1.x. If I'm wrong and this bug should be fixed in 8.1.x then feel free to re-open.

Committed 78bb26d and pushed to 8.2.x. Thanks!

  • alexpott committed 78bb26d on 8.2.x
    Issue #2720813 by Xano, Wim Leers: DefaultPluginManager should expose...
Xano’s picture

Thanks!

I wouldn't say this is an API issue, because it only extends a class to implement an API. I'm not against leaving this out of 8.1.x, although including it will help with performance improvements. The Verf and Plugin modules are able to use this new cacheability metadata already.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.