You are here

class MapRenderEventSubscriber in farmOS 2.x

Same name in this branch
  1. 2.x modules/asset/land/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_land\EventSubscriber\MapRenderEventSubscriber
  2. 2.x modules/asset/structure/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_structure\EventSubscriber\MapRenderEventSubscriber
  3. 2.x modules/core/map/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_map\EventSubscriber\MapRenderEventSubscriber
  4. 2.x modules/core/quantity/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\quantity\EventSubscriber\MapRenderEventSubscriber
  5. 2.x modules/core/ui/map/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_ui_map\EventSubscriber\MapRenderEventSubscriber
  6. 2.x modules/core/map/modules/mapbox/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_map_mapbox\EventSubscriber\MapRenderEventSubscriber

An event subscriber for the MapRenderEvent.

This adds the mapbox behavior and api_key setting to all maps.

Hierarchy

  • class \Drupal\farm_map_mapbox\EventSubscriber\MapRenderEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MapRenderEventSubscriber

1 string reference to 'MapRenderEventSubscriber'
farm_map_mapbox.services.yml in modules/core/map/modules/mapbox/farm_map_mapbox.services.yml
modules/core/map/modules/mapbox/farm_map_mapbox.services.yml
1 service uses MapRenderEventSubscriber
mapbox_map_render_event_subscriber in modules/core/map/modules/mapbox/farm_map_mapbox.services.yml
Drupal\farm_map_mapbox\EventSubscriber\MapRenderEventSubscriber

File

modules/core/map/modules/mapbox/src/EventSubscriber/MapRenderEventSubscriber.php, line 14

Namespace

Drupal\farm_map_mapbox\EventSubscriber
View source
class MapRenderEventSubscriber implements EventSubscriberInterface {

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  private $configFactory;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      MapRenderEvent::EVENT_NAME => 'onMapRender',
    ];
  }

  /**
   * React to the MapRenderEvent.
   *
   * @param \Drupal\farm_map\Event\MapRenderEvent $event
   *   The MapRenderEvent.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function onMapRender(MapRenderEvent $event) {

    // Get the mapbox api_key.
    $api_key = $this->configFactory
      ->get('farm_map_mapbox.settings')
      ->get('api_key');

    // Set a cache tag on the mapbox settings in case this ever changes.
    // This is added to all maps since the mapbox behavior can be added to all
    // maps.
    $event
      ->addCacheTags([
      'config:farm_map_mapbox.settings',
    ]);

    // If the api key exists, add the mapbox behavior.
    if (!empty($api_key)) {
      $event
        ->addBehavior('mapbox', [
        'api_key' => $api_key,
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MapRenderEventSubscriber::$configFactory private property The config factory service.
MapRenderEventSubscriber::getSubscribedEvents public static function
MapRenderEventSubscriber::onMapRender public function React to the MapRenderEvent.
MapRenderEventSubscriber::__construct public function Constructor.