Rename custom module directories
- Rename `opdavies_blog` to `blog`. - Rename `opdavies_blog_test` to `blog_test`. - Rename `opdavies_talks` to `talks`. - Rename `opdavies_talks_test` to `talks_test`. The files within the directories haven't changed, so there is no breaking change caused by renaming the directories. Please enter the commit message for your changes. Lines starting
This commit is contained in:
parent
d7d5a6c8a3
commit
cbe60209e6
59 changed files with 0 additions and 0 deletions
|
@ -1,69 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_talks\EventSubscriber;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
|
||||
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
|
||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* Update a talk node before it's saved.
|
||||
*/
|
||||
final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
|
||||
|
||||
public static function getSubscribedEvents() {
|
||||
return [
|
||||
HookEventDispatcherInterface::ENTITY_PRE_SAVE => 'onEntityPreSave',
|
||||
];
|
||||
}
|
||||
|
||||
public function onEntityPreSave(BaseEntityEvent $event): void {
|
||||
if ($event->getEntity()->getEntityTypeId() != 'node') {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($event->getEntity()->bundle() != 'talk') {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var \Drupal\opdavies_blog\Entity\Node\Talk $talk */
|
||||
$talk = $event->getEntity();
|
||||
$this->reorderEvents($talk);
|
||||
$this->updateCreatedDate($talk);
|
||||
}
|
||||
|
||||
private function reorderEvents(Talk $talk): void {
|
||||
$events = $talk->getEvents();
|
||||
|
||||
$eventsByDate = $events
|
||||
->sortBy(fn(ParagraphInterface $event) => $event->get('field_date')
|
||||
->getString())
|
||||
->values();
|
||||
|
||||
// If the original event IDs don't match the sorted event IDs, update the
|
||||
// event field to use the sorted ones.
|
||||
if ($events->map->id() != $eventsByDate->map->id()) {
|
||||
$talk->set('field_events', $eventsByDate->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
private function updateCreatedDate(Talk $talk): void {
|
||||
if (!$eventDate = $talk->findLatestEventDate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$talkDate = Carbon::parse($eventDate)->getTimestamp();
|
||||
|
||||
if ($talkDate == $talk->get('created')->getString()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$talk->set('created', $talkDate);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue