-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Named query field parameters form widgets
- Loading branch information
Bertrand Dunogier
committed
Feb 18, 2020
1 parent
e28f815
commit c7efa3f
Showing
4 changed files
with
110 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/eZ/FieldType/NamedQuery/Form/EventSubscriber/FieldDefinitionParametersSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\EventSubscriber; | ||
|
||
use eZ\Publish\Core\QueryType\QueryTypeRegistry; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\Extension\Core\Type; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
|
||
class FieldDefinitionParametersSubscriber implements EventSubscriberInterface | ||
{ | ||
/** @var \eZ\Publish\Core\QueryType\QueryTypeRegistry */ | ||
private $queryTypeRegistry; | ||
|
||
public function __construct(QueryTypeRegistry $queryTypeRegistry) | ||
{ | ||
$this->queryTypeRegistry = $queryTypeRegistry; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [FormEvents::PRE_SET_DATA => 'addParametersFormFields']; | ||
} | ||
|
||
public function addParametersFormFields(FormEvent $event) | ||
{ | ||
$data = $event->getData(); | ||
if ($data === null) { | ||
return; | ||
} | ||
|
||
$queryTypeIdentifier = $event->getForm()->getConfig()->getOption('query_type'); | ||
if ($queryTypeIdentifier === null) { | ||
return; | ||
} | ||
|
||
$queryType = $this->queryTypeRegistry->getQueryType($queryTypeIdentifier); | ||
foreach ($queryType->getSupportedParameters() as $parameter) { | ||
$event->getForm()->add( | ||
$parameter, | ||
Type\TextType::class, | ||
[ | ||
'label' => $parameter, | ||
'property_path' => sprintf('[%s]', $parameter), | ||
'required' => false, | ||
] | ||
); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/eZ/FieldType/NamedQuery/Form/FieldDefinitionParametersType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class FieldDefinitionParametersType extends AbstractType | ||
{ | ||
/** @var \Symfony\Component\EventDispatcher\EventSubscriberInterface */ | ||
private $parametersSubscriber; | ||
|
||
public function __construct(EventSubscriberInterface $parametersSubscriber) | ||
{ | ||
$this->parametersSubscriber = $parametersSubscriber; | ||
} | ||
|
||
public function getParent() | ||
{ | ||
return Type\FormType::class; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefault('query_type', null); | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder->addEventSubscriber($this->parametersSubscriber); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters