3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\StringTranslation\TranslationInterface;
7 use Drupal\drupalmoduleupgrader\ConverterBase;
8 use Drupal\drupalmoduleupgrader\TargetInterface;
9 use Pharborist\Functions\ParameterNode;
10 use Psr\Log\LoggerInterface;
14 * id = "hook_ENTITY_TYPE_view",
15 * description = @Translation("Converts implementations of hook_ENTITY_TYPE_view()."),
17 * "hook_comment_view",
19 * "hook_taxonomy_term_view",
22 * dependencies = { "plugin.manager.drupalmoduleupgrader.rewriter" }
25 class HookEntityTypeView extends ConverterBase {
28 * @var \Drupal\Component\Plugin\PluginManagerInterface
32 public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translator, LoggerInterface $log, PluginManagerInterface $rewriters) {
33 parent::__construct($configuration, $plugin_id, $plugin_definition, $translator, $log);
34 $this->rewriters = $rewriters;
37 public function convert(TargetInterface $target) {
38 $indexer = $target->getIndexer('function');
40 $hooks = array_filter($this->pluginDefinition['hook'], [$indexer, 'has']);
41 foreach ($hooks as $hook) {
42 /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
43 $function = $indexer->get($hook);
44 $function->prependParameter(ParameterNode::create('build')->setTypeHint('array')->setReference(TRUE));
46 // Extract the entity type from the hook name (e.g. 'hook_node_view').
47 preg_match('/^hook_(.+)_view$/', $hook, $matches);
48 $entity_type = $matches[1];
49 $rewriter = $this->rewriters->createInstance('_rewriter:' . $entity_type);
50 $this->rewriteFunction($rewriter, $function->getParameterAtIndex(1), $target);