You are here

public function FormState::rewriteAsSetter in Drupal 7 to 8/9 Module Upgrader 8

Rewrites an assignment expression as a property setter. Returns NULL if the expression cannot be rewritten.

Parameters

\Pharborist\ExpressionNode $expr: The expression to rewrite.

string $property: The property being used in the expression.

\Pharborist\Operators\AssignNode $assignment: The entire assignment expression being rewritten.

Return value

\Pharborist\ExpressionNode|null

Overrides Generic::rewriteAsSetter

File

src/Plugin/DMU/Rewriter/FormState.php, line 193

Class

FormState
Plugin annotation @Rewriter( id = "form_state", type_hint = "\Drupal\Core\Form\FormStateInterface", properties = { "always_process" = { "get" = "getAlwaysProcess", "set" = "setAlwaysProcess" }, "build_info" = { "get" =…

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Rewriter

Code

public function rewriteAsSetter(ExpressionNode $expr, $property, AssignNode $assignment) {

  /** @var \Pharborist\ArrayLookupNode $expr */
  $object = clone $expr
    ->getRootArray();
  $keys = $expr
    ->getKeys();
  $value = clone $assignment
    ->getRightOperand();

  // $form_state['values']['baz'] = 'foo' --> $form_state->setValue(['baz'], 'foo')
  if ($property == 'values') {
    array_shift($keys);
    return ObjectMethodCallNode::create($object, 'setValue')
      ->appendArgument(ArrayNode::create($keys))
      ->appendArgument($value);
  }
  elseif (isset($this->pluginDefinition['properties'][$property]['set'])) {
    return parent::rewriteAsSetter($expr, $property, $assignment);
  }
  else {
    return ObjectMethodCallNode::create($object, 'set')
      ->appendArgument(ArrayNode::create($keys))
      ->appendArgument($value);
  }
}