Skip to content

Commit 76f33ce

Browse files
Daniel BerthereauDaniel Berthereau
authored andcommitted
Added a way to set and get form in form adapter.
1 parent 4a80f15 commit 76f33ce

File tree

5 files changed

+76
-22
lines changed

5 files changed

+76
-22
lines changed

src/Api/Representation/SearchConfigRepresentation.php

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@
3434

3535
class SearchConfigRepresentation extends AbstractEntityRepresentation
3636
{
37+
/**
38+
* @var bool
39+
*/
40+
private $isFormInit = false;
41+
3742
/**
3843
* @var \AdvancedSearch\FormAdapter\FormAdapterInterface
3944
*/
4045
protected $formAdapter;
4146

47+
/**
48+
* @var \Laminas\Form\Form|null
49+
*/
50+
protected $form;
51+
4252
public function getJsonLdType()
4353
{
4454
return 'o:SearchConfig';
@@ -123,35 +133,18 @@ public function formAdapterName(): ?string
123133

124134
public function formAdapter(): ?\AdvancedSearch\FormAdapter\FormAdapterInterface
125135
{
126-
if (!$this->formAdapter) {
127-
$formAdapterManager = $this->getServiceLocator()->get('Search\FormAdapterManager');
128-
$formAdapterName = $this->formAdapterName();
129-
if ($formAdapterManager->has($formAdapterName)) {
130-
$this->formAdapter = $formAdapterManager->get($formAdapterName);
131-
}
136+
if (!$this->isFormInit) {
137+
$this->formInit();
132138
}
133-
134139
return $this->formAdapter;
135140
}
136141

137142
public function form(): ?\Laminas\Form\Form
138143
{
139-
$formAdapter = $this->formAdapter();
140-
if (empty($formAdapter)) {
141-
return null;
142-
}
143-
144-
$formClass = $formAdapter->getFormClass();
145-
if (empty($formClass)) {
146-
return null;
144+
if (!$this->isFormInit) {
145+
$this->formInit();
147146
}
148-
149-
return $this->getServiceLocator()
150-
->get('FormElementManager')
151-
->get($formClass, [
152-
'search_config' => $this,
153-
])
154-
->setAttribute('method', 'GET');
147+
return $this->form;
155148
}
156149

157150
public function settings(): array
@@ -183,4 +176,32 @@ public function getEntity(): \AdvancedSearch\Entity\SearchConfig
183176
{
184177
return $this->resource;
185178
}
179+
180+
private function formInit(): self
181+
{
182+
$this->isFormInit = true;
183+
184+
$formAdapterManager = $this->getServiceLocator()->get('Search\FormAdapterManager');
185+
$formAdapterName = $this->formAdapterName();
186+
if (!$formAdapterManager->has($formAdapterName)) {
187+
return $this;
188+
}
189+
190+
$this->formAdapter = $formAdapterManager->get($formAdapterName);
191+
$formClass = $this->formAdapter->getFormClass();
192+
if (empty($formClass)) {
193+
return $this;
194+
}
195+
196+
$this->form = $this->getServiceLocator()
197+
->get('FormElementManager')
198+
->get($formClass, [
199+
'search_config' => $this,
200+
])
201+
->setAttribute('method', 'GET');
202+
203+
$this->formAdapter->setForm($this->form);
204+
205+
return $this;
206+
}
186207
}

src/FormAdapter/AbstractFormAdapter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,21 @@
3333

3434
abstract class AbstractFormAdapter implements FormAdapterInterface
3535
{
36+
protected $form;
37+
3638
abstract public function getLabel(): string;
3739

40+
public function setForm(?\Laminas\Form\Form $form): \AdvancedSearch\FormAdapter\FormAdapterInterface
41+
{
42+
$this->form = $form;
43+
return $this;
44+
}
45+
46+
public function getForm(): ?\Laminas\Form\Form
47+
{
48+
return $this->form;
49+
}
50+
3851
public function getFormPartialHeaders(): ?string
3952
{
4053
return null;

src/FormAdapter/ApiFormAdapter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public function getLabel(): string
3030
return 'Api'; // @translate
3131
}
3232

33+
public function setForm(?\Laminas\Form\Form $form): \AdvancedSearch\FormAdapter\FormAdapterInterface
34+
{
35+
return $this;
36+
}
37+
38+
public function getForm(): ?\Laminas\Form\Form
39+
{
40+
return null;
41+
}
42+
3343
public function getFormClass(): ?string
3444
{
3545
return null;

src/FormAdapter/FormAdapterInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ interface FormAdapterInterface
3434
{
3535
public function getLabel(): string;
3636

37+
public function setForm(?\Laminas\Form\Form $form): \AdvancedSearch\FormAdapter\FormAdapterInterface;
38+
39+
public function getForm(): ?\Laminas\Form\Form;
40+
41+
/**
42+
* The form class to use to build the search form, if any.
43+
*/
3744
public function getFormClass(): ?string;
3845

3946
/**

src/View/Helper/SearchForm.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use Laminas\Form\Form;
77
use Laminas\View\Helper\AbstractHelper;
88

9+
/**
10+
* @todo Remove this view helper and use SearchConfigRepresentation::form() only.
11+
*/
912
class SearchForm extends AbstractHelper
1013
{
1114
/**

0 commit comments

Comments
 (0)