You are here

class MediaConfigSubscriber in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/src/EventSubscriber/MediaConfigSubscriber.php \Drupal\media\EventSubscriber\MediaConfigSubscriber

Listens to the config save event for media.settings.

Hierarchy

  • class \Drupal\media\EventSubscriber\MediaConfigSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MediaConfigSubscriber

1 string reference to 'MediaConfigSubscriber'
media.services.yml in core/modules/media/media.services.yml
core/modules/media/media.services.yml
1 service uses MediaConfigSubscriber
media.config_subscriber in core/modules/media/media.services.yml
Drupal\media\EventSubscriber\MediaConfigSubscriber

File

core/modules/media/src/EventSubscriber/MediaConfigSubscriber.php, line 15

Namespace

Drupal\media\EventSubscriber
View source
class MediaConfigSubscriber implements EventSubscriberInterface {

  /**
   * The route builder.
   *
   * @var \Drupal\Core\Routing\RouteBuilderInterface
   */
  protected $routeBuilder;

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs the MediaConfigSubscriber.
   *
   * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
   *   The route builder.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
   *   The cache tags invalidator.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(RouteBuilderInterface $router_builder, CacheTagsInvalidatorInterface $cache_tags_invalidator, EntityTypeManagerInterface $entity_type_manager) {
    $this->routeBuilder = $router_builder;
    $this->cacheTagsInvalidator = $cache_tags_invalidator;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Updates entity type definitions and ensures routes are rebuilt when needed.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The ConfigCrudEvent to process.
   */
  public function onSave(ConfigCrudEvent $event) {
    $saved_config = $event
      ->getConfig();
    if ($saved_config
      ->getName() === 'media.settings' && $event
      ->isChanged('standalone_url')) {
      $this->cacheTagsInvalidator
        ->invalidateTags([
        // The configuration change triggers entity type definition changes,
        // which in turn triggers routes to appear or disappear.
        // @see media_entity_type_alter()
        'entity_types',
        // The 'rendered' cache tag needs to be explicitly invalidated to ensure
        // that all links to Media entities are re-rendered. Ideally, this would
        // not be necessary; invalidating the 'entity_types' cache tag should be
        // sufficient. But that cache tag would then need to be on nearly
        // everything, resulting in excessive complexity. We prefer pragmatism.
        'rendered',
      ]);

      // @todo Remove this when invalidating the 'entity_types' cache tag is
      // respected by the entity type plugin manager. See
      // https://www.drupal.org/project/drupal/issues/3001284 and
      // https://www.drupal.org/project/drupal/issues/3013659.
      $this->entityTypeManager
        ->clearCachedDefinitions();
      $this->routeBuilder
        ->setRebuildNeeded();
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ConfigEvents::SAVE][] = [
      'onSave',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaConfigSubscriber::$cacheTagsInvalidator protected property The cache tags invalidator.
MediaConfigSubscriber::$entityTypeManager protected property The entity type manager.
MediaConfigSubscriber::$routeBuilder protected property The route builder.
MediaConfigSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MediaConfigSubscriber::onSave public function Updates entity type definitions and ensures routes are rebuilt when needed.
MediaConfigSubscriber::__construct public function Constructs the MediaConfigSubscriber.