PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/web/core/lib/Drupal/Core/Form/SubformState.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 154 lines | 65 code | 21 blank | 68 comment | 6 complexity | 0ad64fc0fd05b721a2e87734c6652cd2 MD5 | raw file
  1. <?php
  2. namespace Drupal\Core\Form;
  3. use Drupal\Component\Utility\NestedArray;
  4. /**
  5. * Stores information about the state of a subform.
  6. */
  7. class SubformState extends FormStateDecoratorBase implements SubformStateInterface {
  8. use FormStateValuesTrait;
  9. /**
  10. * The parent form.
  11. *
  12. * @var mixed[]
  13. */
  14. protected $parentForm;
  15. /**
  16. * The subform.
  17. *
  18. * @var mixed[]
  19. */
  20. protected $subform;
  21. /**
  22. * Constructs a new instance.
  23. *
  24. * @param mixed[] $subform
  25. * The subform for which to create a form state.
  26. * @param mixed[] $parent_form
  27. * The subform's parent form.
  28. * @param \Drupal\Core\Form\FormStateInterface $parent_form_state
  29. * The parent form state.
  30. */
  31. protected function __construct(array &$subform, array &$parent_form, FormStateInterface $parent_form_state) {
  32. $this->decoratedFormState = $parent_form_state;
  33. $this->parentForm = $parent_form;
  34. $this->subform = $subform;
  35. }
  36. /**
  37. * Creates a new instance for a subform.
  38. *
  39. * @param mixed[] $subform
  40. * The subform for which to create a form state.
  41. * @param mixed[] $parent_form
  42. * The subform's parent form.
  43. * @param \Drupal\Core\Form\FormStateInterface $parent_form_state
  44. * The parent form state.
  45. *
  46. * @return static
  47. */
  48. public static function createForSubform(array &$subform, array &$parent_form, FormStateInterface $parent_form_state) {
  49. return new static($subform, $parent_form, $parent_form_state);
  50. }
  51. /**
  52. * Gets the subform's parents relative to its parent form.
  53. *
  54. * @param string $property
  55. * The property name (#parents or #array_parents).
  56. *
  57. * @return mixed
  58. *
  59. * @throws \InvalidArgumentException
  60. * Thrown when the requested property does not exist.
  61. * @throws \UnexpectedValueException
  62. * Thrown when the subform is not contained by the given parent form.
  63. */
  64. protected function getParents($property) {
  65. foreach ([$this->subform, $this->parentForm] as $form) {
  66. if (!isset($form[$property]) || !is_array($form[$property])) {
  67. 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));
  68. }
  69. }
  70. $relative_subform_parents = $this->subform[$property];
  71. // Remove all of the subform's parents that are also the parent form's
  72. // parents, so we are left with the parents relative to the parent form.
  73. foreach ($this->parentForm[$property] as $parent_form_parent) {
  74. if ($parent_form_parent !== $relative_subform_parents[0]) {
  75. // The parent form's parents are the subform's parents as well. If we
  76. // find no match, that means the given subform is not contained by the
  77. // given parent form.
  78. throw new \UnexpectedValueException('The subform is not contained by the given parent form.');
  79. }
  80. array_shift($relative_subform_parents);
  81. }
  82. return $relative_subform_parents;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function &getValues() {
  88. $exists = NULL;
  89. $values = &NestedArray::getValue(parent::getValues(), $this->getParents('#parents'), $exists);
  90. if (!$exists) {
  91. $values = [];
  92. }
  93. elseif (!is_array($values)) {
  94. throw new \UnexpectedValueException('The form state values do not belong to the subform.');
  95. }
  96. return $values;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function getCompleteFormState() {
  102. return $this->decoratedFormState instanceof SubformStateInterface ? $this->decoratedFormState->getCompleteFormState() : $this->decoratedFormState;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function setLimitValidationErrors($limit_validation_errors) {
  108. if (is_array($limit_validation_errors)) {
  109. $limit_validation_errors = array_merge($this->getParents('#parents'), $limit_validation_errors);
  110. }
  111. return parent::setLimitValidationErrors($limit_validation_errors);
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getLimitValidationErrors() {
  117. $limit_validation_errors = parent::getLimitValidationErrors();
  118. if (is_array($limit_validation_errors)) {
  119. return array_slice($limit_validation_errors, count($this->getParents('#parents')));
  120. }
  121. return $limit_validation_errors;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function setErrorByName($name, $message = '') {
  127. $parents = $this->subform['#array_parents'];
  128. $parents[] = $name;
  129. $name = implode('][', $parents);
  130. parent::setErrorByName($name, $message);
  131. return $this;
  132. }
  133. }