You are here

public function HookEntityTypeView::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/HookEntityTypeView.php, line 37

Class

HookEntityTypeView
Plugin annotation @Converter( id = "hook_ENTITY_TYPE_view", description = @Translation("Converts implementations of hook_ENTITY_TYPE_view()."), hook = { "hook_comment_view", "hook_node_view", "hook_taxonomy_term_view", …

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Converter

Code

public function convert(TargetInterface $target) {
  $indexer = $target
    ->getIndexer('function');
  $hooks = array_filter($this->pluginDefinition['hook'], [
    $indexer,
    'has',
  ]);
  foreach ($hooks as $hook) {

    /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
    $function = $indexer
      ->get($hook);
    $function
      ->prependParameter(ParameterNode::create('build')
      ->setTypeHint('array')
      ->setReference(TRUE));

    // Extract the entity type from the hook name (e.g. 'hook_node_view').
    preg_match('/^hook_(.+)_view$/', $hook, $matches);
    $entity_type = $matches[1];
    $rewriter = $this->rewriters
      ->createInstance('_rewriter:' . $entity_type);
    $this
      ->rewriteFunction($rewriter, $function
      ->getParameterAtIndex(1), $target);
  }
}