-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement event listener to set creation date based on content field
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
bundle/EventListener/Content/CreationDateEventListener.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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\Bundle\SiteBundle\EventListener\Content; | ||
|
||
use DateTimeInterface; | ||
use Ibexa\Contracts\Core\Repository\ContentService; | ||
use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent; | ||
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface; | ||
use Ibexa\Core\FieldType\DateAndTime\Value as DateAndTimeValue; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
use function array_key_exists; | ||
|
||
final class CreationDateEventListener implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private ConfigResolverInterface $configResolver, | ||
private ContentService $contentService, | ||
) { | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
PublishVersionEvent::class => ['onPublishVersion', 255], | ||
]; | ||
} | ||
|
||
/** | ||
* Sets the creation date of the content based on a field value. | ||
*/ | ||
public function onPublishVersion(PublishVersionEvent $event): void | ||
{ | ||
/** @var bool $listenerEnabled */ | ||
$listenerEnabled = $this->configResolver->getParameter('set_creation_date.enabled', 'ngsite'); | ||
if (!$listenerEnabled) { | ||
return; | ||
} | ||
|
||
/** @var array<string, string> $fieldConfig */ | ||
$fieldConfig = $this->configResolver->getParameter('set_creation_date.fields', 'ngsite'); | ||
|
||
$content = $event->getContent(); | ||
$contentTypeIdentifier = $content->getContentType()->identifier; | ||
if (!array_key_exists($contentTypeIdentifier, $fieldConfig)) { | ||
return; | ||
} | ||
|
||
if (!isset($content->fields[$fieldConfig[$contentTypeIdentifier]])) { | ||
return; | ||
} | ||
|
||
$creationDate = $content->getFieldValue($fieldConfig[$contentTypeIdentifier]); | ||
if (!$creationDate instanceof DateAndTimeValue || !$creationDate->value instanceof DateTimeInterface) { | ||
return; | ||
} | ||
|
||
$updateStruct = $this->contentService->newContentMetadataUpdateStruct(); | ||
$updateStruct->publishedDate = $creationDate->value; | ||
|
||
$this->contentService->updateContentMetadata($content->contentInfo, $updateStruct); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -26,6 +26,11 @@ parameters: | |
ngsite.default.path_helper.excluded_content_types: ['ng_container', 'folder'] | ||
ngsite.default.container_content_types: ['ng_container', 'folder'] | ||
|
||
ngsite.default.set_creation_date.enabled: false | ||
ngsite.default.set_creation_date.fields: | ||
ng_news: publish_date | ||
ng_blog_post: publish_date | ||
|
||
ngsite.default.mail.sender_email: '[email protected]' | ||
ngsite.default.mail.sender_name: 'Netgen Site' | ||
ngsite.default.mail.admin_email: ~ | ||
|