class SubformState

Same name in other branches
  1. 9 core/lib/Drupal/Core/Form/SubformState.php \Drupal\Core\Form\SubformState
  2. 8.9.x core/lib/Drupal/Core/Form/SubformState.php \Drupal\Core\Form\SubformState
  3. 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

18 files declare their use of SubformState
BlockForm.php in core/modules/block/src/BlockForm.php
CKEditor5.php in core/modules/ckeditor5/src/Plugin/Editor/CKEditor5.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.

... 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.
     * @param \Drupal\Core\Form\FormInterface|null $subformFormObject
     *   The subform form object when it's not the same as the parent form.
     */
    protected function __construct(array &$subform, array &$parent_form, FormStateInterface $parent_form_state, ?FormInterface $subformFormObject = NULL) {
        $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.
     * @param \Drupal\Core\Form\FormInterface|null $subform_form_object
     *   The subform form object when it's not the same as the parent form.
     *
     * @return static
     */
    public static function createForSubform(array &$subform, array &$parent_form, FormStateInterface $parent_form_state, ?FormInterface $subform_form_object = NULL) {
        return new static($subform, $parent_form, $parent_form_state, $subform_form_object);
    }
    
    /**
     * 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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFormObject() {
        if ($this->subformFormObject) {
            return $this->subformFormObject;
        }
        return parent::getFormObject();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FormStateDecoratorBase::$decoratedFormState protected property The decorated form state.
FormStateDecoratorBase::addBuildInfo public function Overrides FormStateInterface::addBuildInfo
FormStateDecoratorBase::addCleanValueKey public function Overrides FormStateInterface::addCleanValueKey
FormStateDecoratorBase::addRebuildInfo public function Overrides FormStateInterface::addRebuildInfo
FormStateDecoratorBase::cleanValues public function Overrides FormStateInterface::cleanValues
FormStateDecoratorBase::clearErrors public function Overrides FormStateInterface::clearErrors
FormStateDecoratorBase::disableCache public function Overrides FormStateInterface::disableCache
FormStateDecoratorBase::disableRedirect public function Overrides FormStateInterface::disableRedirect
FormStateDecoratorBase::get public function Overrides FormStateInterface::get
FormStateDecoratorBase::getAlwaysProcess public function Overrides FormStateInterface::getAlwaysProcess
FormStateDecoratorBase::getBuildInfo public function Overrides FormStateInterface::getBuildInfo
FormStateDecoratorBase::getButtons public function Overrides FormStateInterface::getButtons
FormStateDecoratorBase::getCacheableArray public function Overrides FormStateInterface::getCacheableArray
FormStateDecoratorBase::getCleanValueKeys public function Overrides FormStateInterface::getCleanValueKeys
FormStateDecoratorBase::getCompleteForm public function Overrides FormStateInterface::getCompleteForm
FormStateDecoratorBase::getError public function Overrides FormStateInterface::getError
FormStateDecoratorBase::getErrors public function Overrides FormStateInterface::getErrors
FormStateDecoratorBase::getGroups public function Overrides FormStateInterface::getGroups
FormStateDecoratorBase::getIgnoreDestination public function Overrides FormStateInterface::getIgnoreDestination
FormStateDecoratorBase::getRebuildInfo public function Overrides FormStateInterface::getRebuildInfo
FormStateDecoratorBase::getRedirect public function Overrides FormStateInterface::getRedirect
FormStateDecoratorBase::getResponse public function Overrides FormStateInterface::getResponse
FormStateDecoratorBase::getStorage public function Overrides FormStateInterface::getStorage
FormStateDecoratorBase::getSubmitHandlers public function Overrides FormStateInterface::getSubmitHandlers
FormStateDecoratorBase::getTemporary public function Overrides FormStateInterface::getTemporary
FormStateDecoratorBase::getTemporaryValue public function Overrides FormStateInterface::getTemporaryValue
FormStateDecoratorBase::getTriggeringElement public function Overrides FormStateInterface::getTriggeringElement
FormStateDecoratorBase::getUserInput public function Overrides FormStateInterface::getUserInput
FormStateDecoratorBase::getValidateHandlers public function Overrides FormStateInterface::getValidateHandlers
FormStateDecoratorBase::has public function Overrides FormStateInterface::has
FormStateDecoratorBase::hasAnyErrors public static function Overrides FormStateInterface::hasAnyErrors
FormStateDecoratorBase::hasFileElement public function Overrides FormStateInterface::hasFileElement
FormStateDecoratorBase::hasInvalidToken public function Overrides FormStateInterface::hasInvalidToken
FormStateDecoratorBase::hasTemporaryValue public function Overrides FormStateInterface::hasTemporaryValue
FormStateDecoratorBase::isBypassingProgrammedAccessChecks public function Overrides FormStateInterface::isBypassingProgrammedAccessChecks
FormStateDecoratorBase::isCached public function Overrides FormStateInterface::isCached
FormStateDecoratorBase::isExecuted public function Overrides FormStateInterface::isExecuted
FormStateDecoratorBase::isMethodType public function Overrides FormStateInterface::isMethodType
FormStateDecoratorBase::isProcessingInput public function Overrides FormStateInterface::isProcessingInput
FormStateDecoratorBase::isProgrammed public function Overrides FormStateInterface::isProgrammed
FormStateDecoratorBase::isRebuilding public function Overrides FormStateInterface::isRebuilding
FormStateDecoratorBase::isRedirectDisabled public function Overrides FormStateInterface::isRedirectDisabled
FormStateDecoratorBase::isSubmitted public function Overrides FormStateInterface::isSubmitted
FormStateDecoratorBase::isValidationComplete public function Overrides FormStateInterface::isValidationComplete
FormStateDecoratorBase::isValidationEnforced public function Overrides FormStateInterface::isValidationEnforced
FormStateDecoratorBase::loadInclude public function Overrides FormStateInterface::loadInclude
FormStateDecoratorBase::prepareCallback public function Overrides FormStateInterface::prepareCallback
FormStateDecoratorBase::set public function Overrides FormStateInterface::set
FormStateDecoratorBase::setAlwaysProcess public function Overrides FormStateInterface::setAlwaysProcess
FormStateDecoratorBase::setBuildInfo public function Overrides FormStateInterface::setBuildInfo
FormStateDecoratorBase::setButtons public function Overrides FormStateInterface::setButtons
FormStateDecoratorBase::setCached public function Overrides FormStateInterface::setCached
FormStateDecoratorBase::setCleanValueKeys public function Overrides FormStateInterface::setCleanValueKeys
FormStateDecoratorBase::setCompleteForm public function Overrides FormStateInterface::setCompleteForm
FormStateDecoratorBase::setError public function Overrides FormStateInterface::setError
FormStateDecoratorBase::setExecuted public function Overrides FormStateInterface::setExecuted
FormStateDecoratorBase::setFormObject public function Overrides FormStateInterface::setFormObject
FormStateDecoratorBase::setFormState public function Overrides FormStateInterface::setFormState
FormStateDecoratorBase::setGroups public function Overrides FormStateInterface::setGroups
FormStateDecoratorBase::setHasFileElement public function Overrides FormStateInterface::setHasFileElement
FormStateDecoratorBase::setIgnoreDestination public function Overrides FormStateInterface::setIgnoreDestination
FormStateDecoratorBase::setInvalidToken public function Overrides FormStateInterface::setInvalidToken
FormStateDecoratorBase::setMethod public function Overrides FormStateInterface::setMethod
FormStateDecoratorBase::setProcessInput public function Overrides FormStateInterface::setProcessInput
FormStateDecoratorBase::setProgrammed public function Overrides FormStateInterface::setProgrammed
FormStateDecoratorBase::setProgrammedBypassAccessCheck public function Overrides FormStateInterface::setProgrammedBypassAccessCheck
FormStateDecoratorBase::setRebuild public function Overrides FormStateInterface::setRebuild
FormStateDecoratorBase::setRebuildInfo public function Overrides FormStateInterface::setRebuildInfo
FormStateDecoratorBase::setRedirect public function Overrides FormStateInterface::setRedirect
FormStateDecoratorBase::setRedirectUrl public function Overrides FormStateInterface::setRedirectUrl
FormStateDecoratorBase::setRequestMethod public function Overrides FormStateInterface::setRequestMethod
FormStateDecoratorBase::setResponse public function Overrides FormStateInterface::setResponse
FormStateDecoratorBase::setStorage public function Overrides FormStateInterface::setStorage
FormStateDecoratorBase::setSubmitHandlers public function Overrides FormStateInterface::setSubmitHandlers
FormStateDecoratorBase::setSubmitted public function Overrides FormStateInterface::setSubmitted
FormStateDecoratorBase::setTemporary public function Overrides FormStateInterface::setTemporary
FormStateDecoratorBase::setTemporaryValue public function Overrides FormStateInterface::setTemporaryValue
FormStateDecoratorBase::setTriggeringElement public function Overrides FormStateInterface::setTriggeringElement
FormStateDecoratorBase::setUserInput public function Overrides FormStateInterface::setUserInput
FormStateDecoratorBase::setValidateHandlers public function Overrides FormStateInterface::setValidateHandlers
FormStateDecoratorBase::setValidationComplete public function Overrides FormStateInterface::setValidationComplete
FormStateDecoratorBase::setValidationEnforced public function 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::getFormObject public function Returns the form object that is responsible for building this form. Overrides FormStateDecoratorBase::getFormObject
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.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.