Skip to content

Commit

Permalink
EZEE-3176: Added event dispatching for content field form options
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Jul 7, 2020
1 parent bf7cf69 commit e1de434
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
}
},
"scripts": {
"fix-cs": "@php ./vendor/bin/php-cs-fixer fix -v --show-progress=estimating"
"fix-cs": "@php ./bin/php-cs-fixer fix -v --show-progress=estimating"
},
"config": {
"bin-dir": "bin"
Expand Down
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/form_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ services:
EzSystems\EzPlatformContentForms\Form\Type\FieldType\BinaryBaseFieldType: ~

EzSystems\EzPlatformContentForms\Form\Type\LocationType: ~

EzSystems\EzPlatformContentForms\Form\Type\Content\FieldCollectionType: ~
1 change: 1 addition & 0 deletions src/lib/Content/View/Filter/ContentCreateViewFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private function resolveContentCreateForm(
return $this->formFactory->create(ContentEditType::class, $contentCreateData, [
'languageCode' => $languageCode,
'mainLanguageCode' => $languageCode,
'contentCreateStruct' => $contentCreateData,
'drafts_enabled' => true,
]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/Content/View/Filter/ContentEditViewFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ private function resolveContentEditForm(
[
'languageCode' => $languageCode,
'mainLanguageCode' => $content->contentInfo->mainLanguageCode,
'content' => $content,
'contentUpdateStruct' => $contentUpdate,
'drafts_enabled' => true,
]
);
Expand Down
76 changes: 76 additions & 0 deletions src/lib/Event/ContentCreateFieldOptionsEvent.php
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;
}
}
10 changes: 10 additions & 0 deletions src/lib/Event/ContentFormEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ final class ContentFormEvents
* Triggered when canceling a user edition.
*/
const USER_CANCEL = 'user.edit.cancel';

/**
* Triggered when resolving Field Type options for content edit form.
*/
const CONTENT_EDIT_FIELD_OPTIONS = 'content.edit.field.options';

/**
* Triggered when resolving Field Type options for content create form.
*/
const CONTENT_CREATE_FIELD_OPTIONS = 'content.create.field.options';
}
87 changes: 87 additions & 0 deletions src/lib/Event/ContentUpdateFieldOptionsEvent.php
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;
}
}
6 changes: 4 additions & 2 deletions src/lib/Form/Type/Content/BaseContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace EzSystems\EzPlatformContentForms\Form\Type\Content;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
Expand All @@ -34,12 +33,15 @@ public function getBlockPrefix()
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('fieldsData', CollectionType::class, [
->add('fieldsData', FieldCollectionType::class, [
'entry_type' => ContentFieldType::class,
'label' => /** @Desc("Fields") */ 'ezplatform.content_forms.content.fields',
'entry_options' => [
'languageCode' => $options['languageCode'],
'mainLanguageCode' => $options['mainLanguageCode'],
'content' => $options['content'] ?? null,
'contentCreateStruct' => $options['contentCreateStruct'] ?? null,
'contentUpdateStruct' => $options['contentUpdateStruct'] ?? null,
],
])
->add('redirectUrlAfterPublish', HiddenType::class, [
Expand Down
3 changes: 3 additions & 0 deletions src/lib/Form/Type/Content/ContentEditType.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults([
'content' => null,
'contentCreateStruct' => null,
'contentUpdateStruct' => null,
'drafts_enabled' => false,
'data_class' => ContentStruct::class,
'translation_domain' => 'ezplatform_content_forms_content',
Expand Down
3 changes: 3 additions & 0 deletions src/lib/Form/Type/Content/ContentFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults([
'content' => null,
'contentCreateStruct' => null,
'contentUpdateStruct' => null,
'data_class' => FieldData::class,
'translation_domain' => 'ezplatform_content_forms_content',
])
Expand Down
95 changes: 95 additions & 0 deletions src/lib/Form/Type/Content/FieldCollectionType.php
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']);
}
}

0 comments on commit e1de434

Please # to comment.