You are here

protected function SubformState::getParents in Markdown 8.2

Gets the subform's parents relative to its parent form.

Parameters

string $property: The property name (#parents or #array_parents).

Return value

mixed The parents.

Throws

\InvalidArgumentException Thrown when the requested property does not exist.

\UnexpectedValueException Thrown when the subform is not contained by the given parent form.

4 calls to SubformState::getParents()
SubformState::getAllParents in src/Form/SubformState.php
Retrieves all parents for the form state up to this point.
SubformState::getLimitValidationErrors in src/BcSupport/SubformState.php
Retrieves the limited validation error sections.
SubformState::getValues in src/BcSupport/SubformState.php
Implements \Drupal\Core\Form\FormStateInterface::getValues()
SubformState::setLimitValidationErrors in src/BcSupport/SubformState.php
Sets the limited validation error sections.

File

src/BcSupport/SubformState.php, line 80

Class

SubformState
Stores information about the state of a subform.

Namespace

Drupal\markdown\BcSupport

Code

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;
}