Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / HookEntityTypeView.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
4
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;
11
12 /**
13  * @Converter(
14  *  id = "hook_ENTITY_TYPE_view",
15  *  description = @Translation("Converts implementations of hook_ENTITY_TYPE_view()."),
16  *  hook = {
17  *    "hook_comment_view",
18  *    "hook_node_view",
19  *    "hook_taxonomy_term_view",
20  *    "hook_user_view"
21  *  },
22  *  dependencies = { "plugin.manager.drupalmoduleupgrader.rewriter" }
23  * )
24  */
25 class HookEntityTypeView extends ConverterBase {
26
27   /**
28    * @var \Drupal\Component\Plugin\PluginManagerInterface
29    */
30   protected $rewriters;
31
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;
35   }
36
37   public function convert(TargetInterface $target) {
38     $indexer = $target->getIndexer('function');
39
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));
45
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);
51     }
52   }
53
54 }