Add and use a custom Node class

Add and use a custom `Node` class within the custom module, and move the
method there for finding the last event date for a talk node.
This commit is contained in:
Oliver Davies 2020-05-09 22:31:34 +01:00
parent 7e2bd98d93
commit 1a2372f611
4 changed files with 53 additions and 21 deletions

View file

@ -8,8 +8,8 @@ parameters:
- *TestBase.php
checkMissingIterableValueType: false
ignoreErrors:
- '#Call to an undefined method Drupal\\Core\\Entity\\EntityInterface::get()#'
- '#Call to an undefined method Drupal\\Core\\Entity\\EntityInterface::set()#'
- '#Call to an undefined method Drupal\\Core\\Field\\FieldItemListInterface::referencedEntities()#'
includes:
- vendor/mglaman/phpstan-drupal/extension.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon

View file

@ -0,0 +1,19 @@
<?php
/**
* @file
* Custom module.
*/
declare(strict_types=1);
use Drupal\custom\Entity\Node;
/**
* Implements hook_entity_type_build().
*/
function custom_entity_type_build(array &$entityTypes): void {
if (isset($entityTypes['node'])) {
$entityTypes['node']->setClass(Node::class);
}
}

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Drupal\custom\Entity;
use Drupal\node\Entity\Node as CoreNode;
use Drupal\paragraphs\ParagraphInterface;
use Illuminate\Support\Collection;
final class Node extends CoreNode {
/**
* Find the date for the latest event.
*
* @return string|null
*/
public function findLatestEventDate(): ?string {
if (!$this->hasField('field_events')) {
return NULL;
}
return Collection::make($this->get('field_events')->referencedEntities())
->map(fn(ParagraphInterface $event) => $event->get('field_date')
->getString())
->max();
}
}

View file

@ -6,10 +6,9 @@ namespace Drupal\custom\EventSubscriber;
use Carbon\Carbon;
use Drupal\Core\Entity\EntityInterface;
use Drupal\custom\Entity\Node;
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\paragraphs\ParagraphInterface;
use Illuminate\Support\Collection;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
@ -25,11 +24,7 @@ final class UpdateTalkCreatedDateOnSave implements EventSubscriberInterface {
}
public function entityInsertOrUpdate(BaseEntityEvent $event): void {
if ($event->getEntity()->getEntityTypeId() != 'node') {
return;
}
if ($event->getEntity()->bundle() != 'talk') {
if (!$event->getEntity() instanceof Node) {
return;
}
@ -37,7 +32,8 @@ final class UpdateTalkCreatedDateOnSave implements EventSubscriberInterface {
}
private function updateCreatedDate(EntityInterface $talk): void {
if (!$eventDate = $this->findLatestEventDate($talk)) {
/** @var \Drupal\custom\Entity\Node $talk */
if (!$eventDate = $talk->findLatestEventDate()) {
return;
}
@ -50,16 +46,4 @@ final class UpdateTalkCreatedDateOnSave implements EventSubscriberInterface {
$talk->set('created', $talkDate);
}
/**
* Find the date for the latest event.
*
* @return string|null
*/
private function findLatestEventDate(EntityInterface $talk) {
return Collection::make($talk->get('field_events')->referencedEntities())
->map(fn(ParagraphInterface $event) => $event->get('field_date')
->getString())
->max();
}
}