You are here

class SubformState in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Form/SubformState.php \Drupal\Core\Form\SubformState
  2. 10 core/lib/Drupal/Core/Form/SubformState.php \Drupal\Core\Form\SubformState

Stores information about the state of a subform.

Hierarchy

Expanded class hierarchy of SubformState

14 files declare their use of SubformState
BlockForm.php in core/modules/block/src/BlockForm.php
ConfigureBlockFormBase.php in core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php
ConfigureSectionForm.php in core/modules/layout_builder/src/Form/ConfigureSectionForm.php
editor.module in core/modules/editor/editor.module
Adds bindings for client-side "text editors" to text formats.
EditorBaseTest.php in core/modules/editor/tests/src/Unit/EditorBaseTest.php

... See full list

File

core/lib/Drupal/Core/Form/SubformState.php, line 10

Namespace

Drupal\Core\Form
View source
class SubformState extends FormStateDecoratorBase implements SubformStateInterface {
  use FormStateValuesTrait;

  /**
   * The parent form.
   *
   * @var mixed[]
   */
  protected $parentForm;

  /**
   * The subform.
   *
   * @var mixed[]
   */
  protected $subform;

  /**
   * Constructs a new instance.
   *
   * @param mixed[] $subform
   *   The subform for which to create a form state.
   * @param mixed[] $parent_form
   *   The subform's parent form.
   * @param \Drupal\Core\Form\FormStateInterface $parent_form_state
   *   The parent form state.
   */
  protected function __construct(array &$subform, array &$parent_form, FormStateInterface $parent_form_state) {
    $this->decoratedFormState = $parent_form_state;
    $this->parentForm = $parent_form;
    $this->subform = $subform;
  }

  /**
   * Creates a new instance for a subform.
   *
   * @param mixed[] $subform
   *   The subform for which to create a form state.
   * @param mixed[] $parent_form
   *   The subform's parent form.
   * @param \Drupal\Core\Form\FormStateInterface $parent_form_state
   *   The parent form state.
   *
   * @return static
   */
  public static function createForSubform(array &$subform, array &$parent_form, FormStateInterface $parent_form_state) {
    return new static($subform, $parent_form, $parent_form_state);
  }

  /**
   * Gets the subform's parents relative to its parent form.
   *
   * @param string $property
   *   The property name (#parents or #array_parents).
   *
   * @return mixed
   *
   * @throws \InvalidArgumentException
   *   Thrown when the requested property does not exist.
   * @throws \UnexpectedValueException
   *   Thrown when the subform is not contained by the given parent form.
   */
  protected function getParents($property) {
    foreach ([
      $this->subform,
      $this->parentForm,
    ] as $form) {
      if (!isset($form[$property]) || !is_array($form[$property])) {
        throw new \RuntimeException(sprintf('The subform and parent form must contain the %s property, which must be an array. Try calling this method from a #process callback instead.', $property));
      }
    }
    $relative_subform_parents = $this->subform[$property];

    // Remove all of the subform's parents that are also the parent form's
    // parents, so we are left with the parents relative to the parent form.
    foreach ($this->parentForm[$property] as $parent_form_parent) {
      if ($parent_form_parent !== $relative_subform_parents[0]) {

        // The parent form's parents are the subform's parents as well. If we
        // find no match, that means the given subform is not contained by the
        // given parent form.
        throw new \UnexpectedValueException('The subform is not contained by the given parent form.');
      }
      array_shift($relative_subform_parents);
    }
    return $relative_subform_parents;
  }

  /**
   * {@inheritdoc}
   */
  public function &getValues() {
    $exists = NULL;
    $values =& NestedArray::getValue(parent::getValues(), $this
      ->getParents('#parents'), $exists);
    if (!$exists) {
      $values = [];
    }
    elseif (!is_array($values)) {
      throw new \UnexpectedValueException('The form state values do not belong to the subform.');
    }
    return $values;
  }

  /**
   * {@inheritdoc}
   */
  public function getCompleteFormState() {
    return $this->decoratedFormState instanceof SubformStateInterface ? $this->decoratedFormState
      ->getCompleteFormState() : $this->decoratedFormState;
  }

  /**
   * {@inheritdoc}
   */
  public function setLimitValidationErrors($limit_validation_errors) {
    if (is_array($limit_validation_errors)) {
      $limit_validation_errors = array_merge($this
        ->getParents('#parents'), $limit_validation_errors);
    }
    return parent::setLimitValidationErrors($limit_validation_errors);
  }

  /**
   * {@inheritdoc}
   */
  public function getLimitValidationErrors() {
    $limit_validation_errors = parent::getLimitValidationErrors();
    if (is_array($limit_validation_errors)) {
      return array_slice($limit_validation_errors, count($this
        ->getParents('#parents')));
    }
    return $limit_validation_errors;
  }

  /**
   * {@inheritdoc}
   */
  public function setErrorByName($name, $message = '') {
    $parents = $this->subform['#array_parents'];
    $parents[] = $name;
    $name = implode('][', $parents);
    parent::setErrorByName($name, $message);
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormStateDecoratorBase::$decoratedFormState protected property The decorated form state.
FormStateDecoratorBase::addBuildInfo public function Adds a value to the build info. Overrides FormStateInterface::addBuildInfo
FormStateDecoratorBase::addCleanValueKey public function Adds a key to the array of form values that will be cleaned. Overrides FormStateInterface::addCleanValueKey
FormStateDecoratorBase::addRebuildInfo public function Adds a value to the rebuild info. Overrides FormStateInterface::addRebuildInfo
FormStateDecoratorBase::cleanValues public function Removes internal Form API elements and buttons from submitted form values. Overrides FormStateInterface::cleanValues
FormStateDecoratorBase::clearErrors public function Clears all errors against all form elements made by self::setErrorByName(). Overrides FormStateInterface::clearErrors
FormStateDecoratorBase::disableCache public function Prevents the form from being cached. Overrides FormStateInterface::disableCache
FormStateDecoratorBase::disableRedirect public function Prevents the form from redirecting. Overrides FormStateInterface::disableRedirect
FormStateDecoratorBase::get public function Gets any arbitrary property. Overrides FormStateInterface::get
FormStateDecoratorBase::getAlwaysProcess public function Determines if this form should always be processed. Overrides FormStateInterface::getAlwaysProcess
FormStateDecoratorBase::getBuildInfo public function Returns the build info for the form. Overrides FormStateInterface::getBuildInfo
FormStateDecoratorBase::getButtons public function Returns the submit and button elements for the form. Overrides FormStateInterface::getButtons
FormStateDecoratorBase::getCacheableArray public function Returns an array representation of the cacheable portion of the form state. Overrides FormStateInterface::getCacheableArray
FormStateDecoratorBase::getCleanValueKeys public function Gets the keys of the form values that will be cleaned. Overrides FormStateInterface::getCleanValueKeys
FormStateDecoratorBase::getCompleteForm public function Returns a reference to the complete form array. Overrides FormStateInterface::getCompleteForm
FormStateDecoratorBase::getError public function Returns the error message filed against the given form element. Overrides FormStateInterface::getError
FormStateDecoratorBase::getErrors public function Returns an associative array of all errors. Overrides FormStateInterface::getErrors
FormStateDecoratorBase::getFormObject public function Returns the form object that is responsible for building this form. Overrides FormStateInterface::getFormObject
FormStateDecoratorBase::getGroups public function Returns references to details elements to render them within vertical tabs. Overrides FormStateInterface::getGroups
FormStateDecoratorBase::getRebuildInfo public function Gets the rebuild info. Overrides FormStateInterface::getRebuildInfo
FormStateDecoratorBase::getRedirect public function Gets the value to use for redirecting after the form has been executed. Overrides FormStateInterface::getRedirect
FormStateDecoratorBase::getResponse public function Gets a response for this form. Overrides FormStateInterface::getResponse
FormStateDecoratorBase::getStorage public function Returns the entire set of arbitrary data. Overrides FormStateInterface::getStorage
FormStateDecoratorBase::getSubmitHandlers public function Gets the submit handlers. Overrides FormStateInterface::getSubmitHandlers
FormStateDecoratorBase::getTemporary public function Gets temporary data. Overrides FormStateInterface::getTemporary
FormStateDecoratorBase::getTemporaryValue public function Gets an arbitrary value from temporary storage. Overrides FormStateInterface::getTemporaryValue
FormStateDecoratorBase::getTriggeringElement public function Gets the form element that triggered submission. Overrides FormStateInterface::getTriggeringElement
FormStateDecoratorBase::getUserInput public function Returns the form values as they were submitted by the user. Overrides FormStateInterface::getUserInput
FormStateDecoratorBase::getValidateHandlers public function Gets the validate handlers. Overrides FormStateInterface::getValidateHandlers
FormStateDecoratorBase::has public function Determines if an arbitrary property is present. Overrides FormStateInterface::has
FormStateDecoratorBase::hasAnyErrors public static function Determines if any forms have any errors. Overrides FormStateInterface::hasAnyErrors
FormStateDecoratorBase::hasFileElement public function Returns whether this form has a file element. Overrides FormStateInterface::hasFileElement
FormStateDecoratorBase::hasInvalidToken public function Determines if the form has an invalid token. Overrides FormStateInterface::hasInvalidToken
FormStateDecoratorBase::hasTemporaryValue public function Determines if a temporary value is present. Overrides FormStateInterface::hasTemporaryValue
FormStateDecoratorBase::isBypassingProgrammedAccessChecks public function Determines if this form submission should bypass #access. Overrides FormStateInterface::isBypassingProgrammedAccessChecks
FormStateDecoratorBase::isCached public function Determines if the form should be cached. Overrides FormStateInterface::isCached
FormStateDecoratorBase::isExecuted public function Determines if the form was submitted and has been processed and executed. Overrides FormStateInterface::isExecuted
FormStateDecoratorBase::isMethodType public function Returns the HTTP form method. Overrides FormStateInterface::isMethodType
FormStateDecoratorBase::isProcessingInput public function Determines if the form input will be processed. Overrides FormStateInterface::isProcessingInput
FormStateDecoratorBase::isProgrammed public function Returns if this form was submitted programmatically. Overrides FormStateInterface::isProgrammed
FormStateDecoratorBase::isRebuilding public function Determines if the form should be rebuilt after processing. Overrides FormStateInterface::isRebuilding
FormStateDecoratorBase::isRedirectDisabled public function Determines if redirecting has been prevented. Overrides FormStateInterface::isRedirectDisabled
FormStateDecoratorBase::isSubmitted public function Determines if the form has been submitted. Overrides FormStateInterface::isSubmitted
FormStateDecoratorBase::isValidationComplete public function Determines if validation has been completed. Overrides FormStateInterface::isValidationComplete
FormStateDecoratorBase::isValidationEnforced public function Checks if validation is enforced. Overrides FormStateInterface::isValidationEnforced
FormStateDecoratorBase::loadInclude public function Ensures an include file is loaded whenever the form is processed. Overrides FormStateInterface::loadInclude
FormStateDecoratorBase::prepareCallback public function Converts support notations for a form callback to a valid callable. Overrides FormStateInterface::prepareCallback
FormStateDecoratorBase::set public function Sets a value to an arbitrary property. Overrides FormStateInterface::set
FormStateDecoratorBase::setAlwaysProcess public function Sets this form to always be processed. Overrides FormStateInterface::setAlwaysProcess
FormStateDecoratorBase::setBuildInfo public function Sets the build info for the form. Overrides FormStateInterface::setBuildInfo
FormStateDecoratorBase::setButtons public function Stores the submit and button elements for the form. Overrides FormStateInterface::setButtons
FormStateDecoratorBase::setCached public function Sets this form to be cached. Overrides FormStateInterface::setCached
FormStateDecoratorBase::setCleanValueKeys public function Sets the keys of the form values that will be cleaned. Overrides FormStateInterface::setCleanValueKeys
FormStateDecoratorBase::setCompleteForm public function Stores the complete form array. Overrides FormStateInterface::setCompleteForm
FormStateDecoratorBase::setError public function Flags an element as having an error. Overrides FormStateInterface::setError
FormStateDecoratorBase::setExecuted public function Sets that the form was submitted and has been processed and executed. Overrides FormStateInterface::setExecuted
FormStateDecoratorBase::setFormObject public function Sets the form object that is responsible for building this form. Overrides FormStateInterface::setFormObject
FormStateDecoratorBase::setFormState public function Sets the value of the form state. Overrides FormStateInterface::setFormState
FormStateDecoratorBase::setGroups public function Sets references to details elements to render them within vertical tabs. Overrides FormStateInterface::setGroups
FormStateDecoratorBase::setHasFileElement public function Sets that this form has a file element. Overrides FormStateInterface::setHasFileElement
FormStateDecoratorBase::setInvalidToken public function Flags the form state as having or not an invalid token. Overrides FormStateInterface::setInvalidToken
FormStateDecoratorBase::setMethod public function Sets the HTTP method to use for the form's submission. Overrides FormStateInterface::setMethod
FormStateDecoratorBase::setProcessInput public function Sets that the form should process input. Overrides FormStateInterface::setProcessInput
FormStateDecoratorBase::setProgrammed public function Sets that this form was submitted programmatically. Overrides FormStateInterface::setProgrammed
FormStateDecoratorBase::setProgrammedBypassAccessCheck public function Sets if this form submission should bypass #access. Overrides FormStateInterface::setProgrammedBypassAccessCheck
FormStateDecoratorBase::setRebuild public function Sets the form to be rebuilt after processing. Overrides FormStateInterface::setRebuild
FormStateDecoratorBase::setRebuildInfo public function Sets the rebuild info. Overrides FormStateInterface::setRebuildInfo
FormStateDecoratorBase::setRedirect public function Sets the redirect for the form. Overrides FormStateInterface::setRedirect
FormStateDecoratorBase::setRedirectUrl public function Sets the redirect URL for the form. Overrides FormStateInterface::setRedirectUrl
FormStateDecoratorBase::setRequestMethod public function Sets the HTTP method used by the request that is building the form. Overrides FormStateInterface::setRequestMethod
FormStateDecoratorBase::setResponse public function Sets a response for this form. Overrides FormStateInterface::setResponse
FormStateDecoratorBase::setStorage public function Sets the entire set of arbitrary data. Overrides FormStateInterface::setStorage
FormStateDecoratorBase::setSubmitHandlers public function Sets the submit handlers. Overrides FormStateInterface::setSubmitHandlers
FormStateDecoratorBase::setSubmitted public function Sets that the form has been submitted. Overrides FormStateInterface::setSubmitted
FormStateDecoratorBase::setTemporary public function Sets temporary data. Overrides FormStateInterface::setTemporary
FormStateDecoratorBase::setTemporaryValue public function Sets an arbitrary value in temporary storage. Overrides FormStateInterface::setTemporaryValue
FormStateDecoratorBase::setTriggeringElement public function Sets the form element that triggered submission. Overrides FormStateInterface::setTriggeringElement
FormStateDecoratorBase::setUserInput public function Sets the form values as though they were submitted by a user. Overrides FormStateInterface::setUserInput
FormStateDecoratorBase::setValidateHandlers public function Sets the validate handlers. Overrides FormStateInterface::setValidateHandlers
FormStateDecoratorBase::setValidationComplete public function Sets that validation has been completed. Overrides FormStateInterface::setValidationComplete
FormStateDecoratorBase::setValidationEnforced public function Enforces that validation is run. Overrides FormStateInterface::setValidationEnforced
FormStateValuesTrait::getValue public function Implements \Drupal\Core\Form\FormStateInterface::getValue()
FormStateValuesTrait::hasValue public function Implements \Drupal\Core\Form\FormStateInterface::hasValue()
FormStateValuesTrait::isValueEmpty public function Implements \Drupal\Core\Form\FormStateInterface::isValueEmpty()
FormStateValuesTrait::setValue public function Implements \Drupal\Core\Form\FormStateInterface::setValue()
FormStateValuesTrait::setValueForElement public function Implements \Drupal\Core\Form\FormStateInterface::setValueForElement()
FormStateValuesTrait::setValues public function Implements \Drupal\Core\Form\FormStateInterface::setValues()
FormStateValuesTrait::unsetValue public function Implements \Drupal\Core\Form\FormStateInterface::unsetValue()
SubformState::$parentForm protected property The parent form.
SubformState::$subform protected property The subform.
SubformState::createForSubform public static function Creates a new instance for a subform.
SubformState::getCompleteFormState public function Gets the complete form state. Overrides SubformStateInterface::getCompleteFormState
SubformState::getLimitValidationErrors public function Retrieves the limited validation error sections. Overrides FormStateDecoratorBase::getLimitValidationErrors
SubformState::getParents protected function Gets the subform's parents relative to its parent form.
SubformState::getValues public function Implements \Drupal\Core\Form\FormStateInterface::getValues() Overrides FormStateValuesTrait::getValues
SubformState::setErrorByName public function Files an error against a form element. Overrides FormStateDecoratorBase::setErrorByName
SubformState::setLimitValidationErrors public function Sets the limited validation error sections. Overrides FormStateDecoratorBase::setLimitValidationErrors
SubformState::__construct protected function Constructs a new instance.