-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZEE-3176: Added event dispatching for content field form options
- Loading branch information
1 parent
bf7cf69
commit e1de434
Showing
11 changed files
with
284 additions
and
3 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
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
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
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
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,76 @@ | ||
<?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. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformContentForms\Event; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct; | ||
use EzSystems\EzPlatformContentForms\Data\Content\FieldData; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
final class ContentCreateFieldOptionsEvent extends Event | ||
{ | ||
/** @var \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct */ | ||
private $contentCreateStruct; | ||
|
||
/** @var \Symfony\Component\Form\FormInterface */ | ||
private $parentForm; | ||
|
||
/** @var \EzSystems\EzPlatformContentForms\Data\Content\FieldData */ | ||
private $fieldData; | ||
|
||
/** @var array */ | ||
private $options; | ||
|
||
public function __construct( | ||
ContentCreateStruct $contentCreateStruct, | ||
FormInterface $parentForm, | ||
FieldData $fieldData, | ||
array $options | ||
) { | ||
$this->contentCreateStruct = $contentCreateStruct; | ||
$this->parentForm = $parentForm; | ||
$this->fieldData = $fieldData; | ||
$this->options = $options; | ||
} | ||
|
||
public function getContentCreateStruct(): ContentCreateStruct | ||
{ | ||
return $this->contentCreateStruct; | ||
} | ||
|
||
public function getParentForm(): FormInterface | ||
{ | ||
return $this->parentForm; | ||
} | ||
|
||
public function getFieldData(): FieldData | ||
{ | ||
return $this->fieldData; | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function setOptions(array $options): void | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
public function setOption(string $option, $value): void | ||
{ | ||
$this->options[$option] = $value; | ||
} | ||
|
||
public function getOption(string $option) | ||
{ | ||
return $this->options[$option] ?? null; | ||
} | ||
} |
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
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,87 @@ | ||
<?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. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformContentForms\Event; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Content; | ||
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct; | ||
use EzSystems\EzPlatformContentForms\Data\Content\FieldData; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
final class ContentUpdateFieldOptionsEvent extends Event | ||
{ | ||
/** @var \eZ\Publish\API\Repository\Values\Content\Content */ | ||
private $content; | ||
|
||
/** @var \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct */ | ||
private $contentUpdateStruct; | ||
|
||
/** @var \Symfony\Component\Form\FormInterface */ | ||
private $parentForm; | ||
|
||
/** @var \EzSystems\EzPlatformContentForms\Data\Content\FieldData */ | ||
private $fieldData; | ||
|
||
/** @var array */ | ||
private $options; | ||
|
||
public function __construct( | ||
Content $content, | ||
ContentUpdateStruct $contentUpdateStruct, | ||
FormInterface $parentForm, | ||
FieldData $fieldData, | ||
array $options | ||
) { | ||
$this->content = $content; | ||
$this->contentUpdateStruct = $contentUpdateStruct; | ||
$this->parentForm = $parentForm; | ||
$this->fieldData = $fieldData; | ||
$this->options = $options; | ||
} | ||
|
||
public function getContent(): Content | ||
{ | ||
return $this->content; | ||
} | ||
|
||
public function getContentUpdateStruct(): ContentUpdateStruct | ||
{ | ||
return $this->contentUpdateStruct; | ||
} | ||
|
||
public function getParentForm(): FormInterface | ||
{ | ||
return $this->parentForm; | ||
} | ||
|
||
public function getFieldData(): FieldData | ||
{ | ||
return $this->fieldData; | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function setOptions(array $options): void | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
public function setOption(string $option, $value): void | ||
{ | ||
$this->options[$option] = $value; | ||
} | ||
|
||
public function getOption(string $option) | ||
{ | ||
return $this->options[$option] ?? null; | ||
} | ||
} |
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
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
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
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,95 @@ | ||
<?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. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformContentForms\Form\Type\Content; | ||
|
||
use EzSystems\EzPlatformContentForms\Event\ContentCreateFieldOptionsEvent; | ||
use EzSystems\EzPlatformContentForms\Event\ContentFormEvents; | ||
use EzSystems\EzPlatformContentForms\Event\ContentUpdateFieldOptionsEvent; | ||
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
|
||
class FieldCollectionType extends CollectionType | ||
{ | ||
/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */ | ||
private $eventDispatcher; | ||
|
||
public function __construct( | ||
EventDispatcherInterface $eventDispatcher | ||
) { | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
public function buildForm( | ||
FormBuilderInterface $builder, | ||
array $options | ||
) { | ||
parent::buildForm($builder, $options); | ||
|
||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) { | ||
$form = $event->getForm(); | ||
$data = $event->getData(); | ||
|
||
foreach ($form as $name => $child) { | ||
$form->remove($name); | ||
} | ||
|
||
// Then add all rows again in the correct order | ||
foreach ($data as $name => $value) { | ||
$entryOptions = array_replace([ | ||
'property_path' => '[' . $name . ']', | ||
], $options['entry_options']); | ||
$entryData = $data[$name]; | ||
|
||
if ($this->isContentUpdate($entryOptions)) { | ||
/** @var \EzSystems\EzPlatformContentForms\Event\ContentUpdateFieldOptionsEvent $contentUpdateFieldOptionsEvent */ | ||
$contentUpdateFieldOptionsEvent = $this->eventDispatcher->dispatch( | ||
new ContentUpdateFieldOptionsEvent( | ||
$entryOptions['content'], | ||
$entryOptions['contentUpdateStruct'], | ||
$form, | ||
$entryData, | ||
$entryOptions | ||
), | ||
ContentFormEvents::CONTENT_EDIT_FIELD_OPTIONS | ||
); | ||
|
||
$entryOptions = $contentUpdateFieldOptionsEvent->getOptions(); | ||
} elseif ($this->isContentCreate($entryOptions)) { | ||
/** @var \EzSystems\EzPlatformContentForms\Event\ContentCreateFieldOptionsEvent $contentUpdateFieldOptionsEvent */ | ||
$contentCreateFieldOptionsEvent = $this->eventDispatcher->dispatch( | ||
new ContentCreateFieldOptionsEvent( | ||
$entryOptions['contentCreateStruct'], | ||
$form, | ||
$entryData, | ||
$entryOptions | ||
), | ||
ContentFormEvents::CONTENT_CREATE_FIELD_OPTIONS | ||
); | ||
|
||
$entryOptions = $contentCreateFieldOptionsEvent->getOptions(); | ||
} | ||
|
||
$form->add($name, $options['entry_type'], $entryOptions); | ||
} | ||
}); | ||
} | ||
|
||
private function isContentCreate(array $entryOptions): bool | ||
{ | ||
return !empty($entryOptions['contentCreateStruct']); | ||
} | ||
|
||
private function isContentUpdate(array $entryOptions): bool | ||
{ | ||
return !empty($entryOptions['content']) && !empty($entryOptions['contentUpdateStruct']); | ||
} | ||
} |