You are here

public function SophronEventSubscriber::initializeMap in Sophron 8

Reacts to a 'sophron.map.initialize' event.

Alters the map's mappings at run-time with additional commands stored in the module settings.

Parameters

\Drupal\sophron\Event\MapEvent $event: Sophron's map event.

File

src/EventSubscriber/SophronEventSubscriber.php, line 60

Class

SophronEventSubscriber
Sophron's module Event Subscriber.

Namespace

Drupal\sophron\EventSubscriber

Code

public function initializeMap(MapEvent $event) {
  $map_commands = $this->sophronSettings
    ->get('map_commands');
  $map = MapHandler::map($event
    ->getMapClass());
  foreach ($map_commands as $command) {
    $method = isset($command[0]) ? $command[0] : '';
    $args = isset($command[1]) ? $command[1] : [];
    try {
      if (!is_callable([
        $map,
        $method,
      ])) {
        throw new \InvalidArgumentException("Non-existing mapping method '{$method}'");
      }
      call_user_func_array([
        $map,
        $method,
      ], $args);
    } catch (MappingException $e) {
      $event
        ->addError((string) $method, (array) $args, 'Mapping', $e
        ->getMessage());
    } catch (MalformedTypeException $e) {
      $event
        ->addError((string) $method, (array) $args, 'Invalid MIME type syntax', $e
        ->getMessage());
    } catch (\Exception $e) {
      $event
        ->addError((string) $method, (array) $args, 'Other', $e
        ->getMessage());
    } catch (\Error $e) {
      $event
        ->addError((string) $method, (array) $args, 'Error', $e
        ->getMessage());
    }
  }
  $map
    ->sort();
}