annotate core/modules/media/src/EventSubscriber/MediaConfigSubscriber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@18 1 <?php
Chris@18 2
Chris@18 3 namespace Drupal\media\EventSubscriber;
Chris@18 4
Chris@18 5 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
Chris@18 6 use Drupal\Core\Config\ConfigCrudEvent;
Chris@18 7 use Drupal\Core\Config\ConfigEvents;
Chris@18 8 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@18 9 use Drupal\Core\Routing\RouteBuilderInterface;
Chris@18 10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@18 11
Chris@18 12 /**
Chris@18 13 * Listens to the config save event for media.settings.
Chris@18 14 */
Chris@18 15 class MediaConfigSubscriber implements EventSubscriberInterface {
Chris@18 16
Chris@18 17 /**
Chris@18 18 * The route builder.
Chris@18 19 *
Chris@18 20 * @var \Drupal\Core\Routing\RouteBuilderInterface
Chris@18 21 */
Chris@18 22 protected $routeBuilder;
Chris@18 23
Chris@18 24 /**
Chris@18 25 * The cache tags invalidator.
Chris@18 26 *
Chris@18 27 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
Chris@18 28 */
Chris@18 29 protected $cacheTagsInvalidator;
Chris@18 30
Chris@18 31 /**
Chris@18 32 * The entity type manager.
Chris@18 33 *
Chris@18 34 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@18 35 */
Chris@18 36 protected $entityTypeManager;
Chris@18 37
Chris@18 38 /**
Chris@18 39 * Constructs the MediaConfigSubscriber.
Chris@18 40 *
Chris@18 41 * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
Chris@18 42 * The route builder.
Chris@18 43 * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
Chris@18 44 * The cache tags invalidator.
Chris@18 45 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@18 46 * The entity type manager.
Chris@18 47 */
Chris@18 48 public function __construct(RouteBuilderInterface $router_builder, CacheTagsInvalidatorInterface $cache_tags_invalidator, EntityTypeManagerInterface $entity_type_manager) {
Chris@18 49 $this->routeBuilder = $router_builder;
Chris@18 50 $this->cacheTagsInvalidator = $cache_tags_invalidator;
Chris@18 51 $this->entityTypeManager = $entity_type_manager;
Chris@18 52 }
Chris@18 53
Chris@18 54 /**
Chris@18 55 * Updates entity type definitions and ensures routes are rebuilt when needed.
Chris@18 56 *
Chris@18 57 * @param \Drupal\Core\Config\ConfigCrudEvent $event
Chris@18 58 * The ConfigCrudEvent to process.
Chris@18 59 */
Chris@18 60 public function onSave(ConfigCrudEvent $event) {
Chris@18 61 $saved_config = $event->getConfig();
Chris@18 62 if ($saved_config->getName() === 'media.settings' && $event->isChanged('standalone_url')) {
Chris@18 63 $this->cacheTagsInvalidator->invalidateTags([
Chris@18 64 // The configuration change triggers entity type definition changes,
Chris@18 65 // which in turn triggers routes to appear or disappear.
Chris@18 66 // @see media_entity_type_alter()
Chris@18 67 'entity_types',
Chris@18 68 // The 'rendered' cache tag needs to be explicitly invalidated to ensure
Chris@18 69 // that all links to Media entities are re-rendered. Ideally, this would
Chris@18 70 // not be necessary; invalidating the 'entity_types' cache tag should be
Chris@18 71 // sufficient. But that cache tag would then need to be on nearly
Chris@18 72 // everything, resulting in excessive complexity. We prefer pragmatism.
Chris@18 73 'rendered',
Chris@18 74 ]);
Chris@18 75 // @todo Remove this when invalidating the 'entity_types' cache tag is
Chris@18 76 // respected by the entity type plugin manager. See
Chris@18 77 // https://www.drupal.org/project/drupal/issues/3001284 and
Chris@18 78 // https://www.drupal.org/project/drupal/issues/3013659.
Chris@18 79 $this->entityTypeManager->clearCachedDefinitions();
Chris@18 80 $this->routeBuilder->setRebuildNeeded();
Chris@18 81 }
Chris@18 82 }
Chris@18 83
Chris@18 84 /**
Chris@18 85 * {@inheritdoc}
Chris@18 86 */
Chris@18 87 public static function getSubscribedEvents() {
Chris@18 88 $events[ConfigEvents::SAVE][] = ['onSave'];
Chris@18 89 return $events;
Chris@18 90 }
Chris@18 91
Chris@18 92 }