You are here

public function HookInit::convert in Drupal 7 to 8/9 Module Upgrader 8

Performs required conversions.

Parameters

TargetInterface $target: The target module to convert.

Overrides ConverterInterface::convert

File

src/Plugin/DMU/Converter/HookInit.php, line 20

Class

HookInit
Plugin annotation @Converter( id = "hook_init", description = @Translation("Converts Drupal 7's hook_init() to an EventSubscriber."), hook = "hook_init" )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Converter

Code

public function convert(TargetInterface $target) {
  $this
    ->writeService($target, 'init_subscriber', [
    'class' => 'Drupal\\' . $target
      ->id() . '\\EventSubscriber\\InitSubscriber',
    'tags' => [
      [
        'name' => 'event_subscriber',
      ],
    ],
  ]);
  $render = [
    '#theme' => 'dmu_event_subscriber',
    '#module' => $target
      ->id(),
    '#class' => 'InitSubscriber',
    '#event' => 'KernelEvents::REQUEST',
  ];
  $subscriber = $this
    ->parse($render);
  $target
    ->getIndexer('function')
    ->get('hook_init')
    ->cloneAsMethodOf($subscriber)
    ->setName('onEvent');
  $this
    ->writeClass($target, $subscriber);
}