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_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" }
20 class HookNodePrepare extends ConverterBase {
23 * @var \Drupal\Component\Plugin\PluginManagerInterface
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;
35 public function convert(TargetInterface $target) {
36 /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
37 $function = $target->getIndexer('function')->get('hook_node_prepare');
39 // foo_node_prepare() --> foo_node_prepare_form().
40 $function->setName($function->getName() . '_form');
42 // The first parameter is a node, so rewrite the function accordingly.
44 ->createInstance('_entity:node')
45 ->rewrite($function->getParameterAtIndex(0));
47 // Create the $operation parameter.
48 $function->appendParameter(ParameterNode::create('operation'));
50 // Create the $form_state parameter.
51 $form_state = ParameterNode::create('form_state')->setTypeHint('\Drupal\Core\Form\FormStateInterface');
52 $function->appendParameter($form_state);
54 $target->save($function);