SubformState::getParents

protected SubformState::getParents($property)

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

Parameters

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

Return value

mixed

Throws

\InvalidArgumentException Thrown when the requested property does not exist.

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

File

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

Class

SubformState
Stores information about the state of a subform.

Namespace

Drupal\Core\Form

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;
}
doc_Drupal
2016-10-29 09:45:25
Comments
Leave a Comment

Please login to continue.