You are here

protected function FormConverter::addMethod in Drupal 7 to 8/9 Module Upgrader 8

Return value

\Pharborist\Objects\ClassMethodNode

1 call to FormConverter::addMethod()
FormConverter::build in src/Utility/FormConverter.php

File

src/Utility/FormConverter.php, line 143

Class

FormConverter
Converts a form from a set of callback functions to a class implementing \Drupal\Core\Form\FormInterface.

Namespace

Drupal\drupalmoduleupgrader\Utility

Code

protected function addMethod(FunctionDeclarationNode $function, ClassNode $class, $alias = NULL) {
  $method = ClassMethodNode::fromFunction($function);
  if ($alias) {
    $method
      ->setName($alias);
  }
  $class
    ->appendMethod($method);

  // Add the parameters required for FormInterface conformance.
  $parameters = $method
    ->getParameters()
    ->toArray();
  if (empty($parameters)) {
    $parameters[0] = ParameterNode::create('$form');
    $method
      ->appendParameter($parameters[0]);
  }
  if (sizeof($parameters) == 1) {
    $parameters[1] = ParameterNode::create('$form_state');
    $method
      ->appendParameter($parameters[1]);
  }

  // The $form parameter must have the array type hint.
  $parameters[0]
    ->setTypeHint('array');

  // The form state is never passed by reference.
  $parameters[1]
    ->setReference(FALSE);

  // Additional parameters MUST have a default value of NULL in order to conform
  // to FormInterface.
  for ($i = 2; $i < sizeof($parameters); $i++) {
    $parameters[$i]
      ->setValue(new TokenNode(T_STRING, 'NULL'));
  }
  $this->formStateRewriter
    ->rewrite($parameters[1]);
  return $method;
}