You are here

public function MapRenderEvent::addBehavior in farmOS 2.x

Add behavior to the map.

Parameters

string $behavior_name: The behavior name.

array $settings: Optional behavior settings that will be added to drupalSettings.farm_map.behaviors.behavior_name.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

modules/core/map/src/Event/MapRenderEvent.php, line 76

Class

MapRenderEvent
An event that is dispatched before rendering a map on the page.

Namespace

Drupal\farm_map\Event

Code

public function addBehavior(string $behavior_name, array $settings = []) {

  // Load the behavior.

  /** @var \Drupal\farm_map\Entity\MapBehaviorInterface $behavior */
  $behavior = \Drupal::entityTypeManager()
    ->getStorage('map_behavior')
    ->load($behavior_name);

  // Attach the library.
  $this->element['#attached']['library'][] = $behavior
    ->getLibrary();

  // Add behavior settings if supplied.
  if (!empty($settings)) {
    $behaviorSettings['behaviors'][$behavior_name] = $settings;
    $this
      ->addSettings($behaviorSettings);
  }
}