You are here

class FormState in Drupal 7 to 8/9 Module Upgrader 8

Plugin annotation


@Rewriter(
 id = "form_state",
 type_hint = "\Drupal\Core\Form\FormStateInterface",
 properties = {
   "always_process" = {
     "get" = "getAlwaysProcess",
     "set" = "setAlwaysProcess"
   },
   "build_info" = {
     "get" = "getBuildInfo",
     "set" = "setBuildInfo"
   },
   "buttons" = {
     "get" = "getButtons",
     "set" = "setButtons"
   },
   "cache" = {
     "get" = "isCached",
     "set" = "setCached"
   },
   "complete_form" = {
     "get" = "getCompleteForm"
   },
   "executed" = {
     "get" = "isExecuted",
     "set" = "setExecuted"
   },
   "groups" = {
     "get" = "getGroups",
     "set" = "setGroups"
   },
   "has_file_element" = {
     "get" = "hasFileElement",
     "set" = "setHasFileElement"
   },
   "input" = {
     "get" = "getUserInput",
     "set" = "setUserInput"
   },
   "limit_validation_errors" = {
     "get" = "getLimitValidationErrors",
     "set" = "setLimitValidationErrors"
   },
   "must_validate" = {
     "get" = "isValidationEnforced",
     "set" = "setValidationEnforced"
   },
   "process_input" = {
     "get" = "isProcessingInput",
     "set" = "setProcessInput"
   },
   "programmed" = {
     "get" = "isProgrammed",
     "set" = "setProgrammed"
   },
   "programmed_bypass_access_check" = {
     "get" = "isBypassingProgrammedAccessChecks",
     "set" = "setProgrammedBypassAccessCheck"
   },
   "rebuild" = {
     "get" = "isRebuilding",
     "set" = "setRebuild"
   },
   "response" = {
     "get" = "getResponse",
     "set" = "setResponse"
   },
   "storage" = {
     "get" = "getStorage",
     "set" = "setStorage"
   },
   "submit_handlers" = {
     "get" = "getSubmitHandlers",
     "set" = "setSubmitHandlers"
   },
   "submitted" = {
     "get" = "isSubmitted",
     "set" = "getSubmitted"
   },
   "temporary" = {
     "get" = "getTemporary",
     "set" = "setTemporary"
   },
   "triggering_element" = {
     "get" = "getTriggeringElement",
     "set" = "setTriggeringElement"
   },
   "validate_handlers" = {
     "get" = "getValidateHandlers",
     "set" = "setValidateHandlers"
   },
   "validation_complete" = {
     "get" = "isValidationComplete",
     "set" = "setValidationComplete"
   }
 }
)

Hierarchy

Expanded class hierarchy of FormState

File

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

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Rewriter
View source
class FormState extends Generic {

  /**
   * {@inheritdoc}
   */
  public function rewrite(ParameterNode $parameter) {
    parent::rewrite($parameter);
    $function = $parameter
      ->getFunction();
    $form_state = Token::variable('$' . $parameter
      ->getName());
    $set_errors = $function
      ->find(Filter::isFunctionCall('form_set_error', 'form_error'));

    /** @var \Pharborist\Functions\FunctionCallNode $set_error */
    foreach ($set_errors as $set_error) {
      $arguments = $set_error
        ->getArguments();
      $method = $set_error
        ->getName()
        ->getText() == 'form_set_error' ? 'setErrorByName' : 'setError';
      $rewrite = ObjectMethodCallNode::create(clone $form_state, $method);
      foreach ($arguments as $argument) {
        if (is_object($argument)) {
          $rewrite
            ->appendArgument(clone $argument);
        }
      }
      $set_error
        ->replaceWith($rewrite);
    }

    // form_clear_error() --> $form_state->clearErrors().
    $clear_errors = $function
      ->find(Filter::isFunctionCall('form_clear_error'));
    foreach ($clear_errors as $clear_error) {
      $clear_error
        ->replaceWith(ObjectMethodCallNode::create(clone $form_state, 'clearErrors'));
    }

    // form_get_errors() --> $form_state->getErrors()
    $get_errors = $function
      ->find(Filter::isFunctionCall('form_get_errors'));
    foreach ($get_errors as $get_error) {
      $get_error
        ->replaceWith(ObjectMethodCallNode::create(clone $form_state, 'getErrors'));
    }

    // form_get_error() --> $form_state->getError()
    $get_errors = $function
      ->find(Filter::isFunctionCall('form_get_error'));

    /** @var \Pharborist\Functions\FunctionCallNode $get_error */
    foreach ($get_errors as $get_error) {
      $rewrite = ObjectMethodCallNode::create(clone $form_state, 'getError')
        ->appendArgument($get_error
        ->getArguments()
        ->get(0));
      $get_error
        ->replaceWith($rewrite);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function rewriteAsGetter(ExpressionNode $expr, $property) {

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

    // $foo = $form_state['values'] --> $foo = $form_state->getValues()
    // $foo = $form_state['values']['baz'] --> $form_state->getValue(['baz'])
    if ($property == 'values') {
      if (sizeof($keys) == 1) {
        return ObjectMethodCallNode::create($object, 'getValues');
      }
      else {
        array_shift($keys);
        return ObjectMethodCallNode::create($object, 'getValue')
          ->appendArgument(ArrayNode::create($keys));
      }
    }
    elseif (isset($this->pluginDefinition['properties'][$property]['get'])) {
      return parent::rewriteAsGetter($expr, $property);
    }
    else {
      return ObjectMethodCallNode::create($object, 'get')
        ->appendArgument(ArrayNode::create($keys));
    }
  }

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormState::rewrite public function Parametrically rewrites the function containing the given parameter. Overrides Generic::rewrite
FormState::rewriteAsGetter public function Rewrites the given expression as a property getter. Returns NULL if the expression cannot be rewritten. Overrides Generic::rewriteAsGetter
FormState::rewriteAsSetter public function Rewrites an assignment expression as a property setter. Returns NULL if the expression cannot be rewritten. Overrides Generic::rewriteAsSetter
Generic::$isAssigned protected property
Generic::getExpressions protected function Finds every rewritable expression in the function body.
Generic::getProperty protected function Returns the property used by a rewritable expression, or NULL if the property cannot be determined.
Generic::isReassigned protected function Returns if the parameter is fully reassigned anywhere in the function.
Generic::isTranslation public static function Checks if a field lookup key is translated. This will be TRUE unless one of the following conditions applies:
Generic::rewriteFieldLookup public static function Rewrites a Drupal 7 field lookup like so:
Generic::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$log protected property
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.