Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / HookNodePrepare.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_node_prepare",
15  *  description = @Translation("Converts hook_node_prepare() into hook_ENTITY_TYPE_prepare_form()."),
16  *  hook = "hook_node_prepare",
17  *  dependencies = { "plugin.manager.drupalmoduleupgrader.rewriter" }
18  * )
19  */
20 class HookNodePrepare extends ConverterBase {
21
22   /**
23    * @var \Drupal\Component\Plugin\PluginManagerInterface
24    */
25   protected $rewriters;
26
27   public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translator, LoggerInterface $log, PluginManagerInterface $rewriters) {
28     parent::__construct($configuration, $plugin_id, $plugin_definition, $translator, $log);
29     $this->rewriters = $rewriters;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function convert(TargetInterface $target) {
36     /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
37     $function = $target->getIndexer('function')->get('hook_node_prepare');
38
39     // foo_node_prepare() --> foo_node_prepare_form().
40     $function->setName($function->getName() . '_form');
41
42     // The first parameter is a node, so rewrite the function accordingly.
43     $this->rewriters
44       ->createInstance('_entity:node')
45       ->rewrite($function->getParameterAtIndex(0));
46
47     // Create the $operation parameter.
48     $function->appendParameter(ParameterNode::create('operation'));
49
50     // Create the $form_state parameter.
51     $form_state = ParameterNode::create('form_state')->setTypeHint('\Drupal\Core\Form\FormStateInterface');
52     $function->appendParameter($form_state);
53
54     $target->save($function);
55   }
56
57 }