06f03b32fa52b9997fbd2bdde8ca8c42e65f7a99
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Model / FormElements / GridRow.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Model\FormElements;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException;
19
20 /**
21 * A grid row, being part of a grid container
22 *
23 * This class contains multiple FormElements ({@link FormElementInterface}).
24 *
25 * Please see {@link FormDefinition} for an in-depth explanation.
26 *
27 * Scope: frontend
28 * **This class is NOT meant to be sub classed by developers.**
29 */
30 class GridRow extends Section implements GridRowInterface
31 {
32
33 /**
34 * Register this element at the parent form, if there is a connection to the parent form.
35 *
36 * @return void
37 * @throws TypeDefinitionNotValidException
38 * @internal
39 */
40 public function registerInFormIfPossible()
41 {
42 if (!$this->getParentRenderable() instanceof GridContainerInterface) {
43 throw new TypeDefinitionNotValidException(
44 sprintf('Grid rows ("%s") only allowed within grid containers.', $this->getIdentifier()),
45 1489413805
46 );
47 }
48 parent::registerInFormIfPossible();
49 }
50
51 /**
52 * Add a new form element at the end of the grid row
53 *
54 * @param FormElementInterface $formElement The form element to add
55 * @return void
56 * @throws TypeDefinitionNotValidException if FormElement is already added to a section
57 * @api
58 */
59 public function addElement(FormElementInterface $formElement)
60 {
61 if ($formElement instanceof GridContainerInterface) {
62 throw new TypeDefinitionNotValidException(
63 sprintf('Grid containers ("%s") within grid rows ("%s") are not allowed.', $formElement->getIdentifier(), $this->getIdentifier()),
64 1489413379
65 );
66 } elseif ($formElement instanceof GridRowInterface) {
67 throw new TypeDefinitionNotValidException(
68 sprintf('Grid rows ("%s") within grid rows ("%s") are not allowed.', $formElement->getIdentifier(), $this->getIdentifier()),
69 1489413696
70 );
71 }
72
73 $this->addRenderable($formElement);
74 }
75
76 /**
77 * Create a form element with the given $identifier and attach it to this container.
78 *
79 * @param string $identifier Identifier of the new form element
80 * @param string $typeName type of the new form element
81 * @return GridRowInterface the newly created frid row
82 * @throws TypeDefinitionNotValidException
83 * @api
84 */
85 public function createElement(string $identifier, string $typeName): FormElementInterface
86 {
87 $element = parent::createElement($identifier, $typeName);
88
89 if ($element instanceof GridContainerInterface) {
90 throw new TypeDefinitionNotValidException(
91 sprintf('Grid containers ("%s") within grid rows ("%s") are not allowed.', $element->getIdentifier(), $this->getIdentifier()),
92 1489413538
93 );
94 } elseif ($element instanceof GridRowInterface) {
95 throw new TypeDefinitionNotValidException(
96 sprintf('Grid rows ("%s") within grid rows ("%s") are not allowed.', $element->getIdentifier(), $this->getIdentifier()),
97 1489413697
98 );
99 }
100
101 return $element;
102 }
103 }