annotate core/modules/media/src/EventSubscriber/MediaConfigSubscriber.php @ 5:12f9dff5fda9 tip

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