build(deps): remove discoverable_entity_bundle_classes

As this module is no longer supported, remove it from the codebase and
update all references to it within the custom code - instead manually
wrapping nodes with the `Post` or `Talk` class, or returning it from a
Repository.

Fixes: #465
This commit is contained in:
Oliver Davies 2021-12-17 15:16:52 +00:00
parent 41e13fe078
commit cae2091436
24 changed files with 214 additions and 150 deletions

View file

@ -3,5 +3,3 @@ description: Custom code for talks pages.
type: module
core_version_requirement: ^8 || ^9
package: Custom
dependencies:
- discoverable_entity_bundle_classes:discoverable_entity_bundle_classes

View file

@ -9,7 +9,6 @@ declare(strict_types=1);
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\discoverable_entity_bundle_classes\Storage\Node\NodeStorage;
use Drupal\opdavies_talks\Service\TalkCounter;
use Drupal\opdavies_talks\Service\TalkDateUpdater;
@ -36,16 +35,6 @@ function opdavies_talks_views_data_alter(array &$data): void {
];
}
/**
* Implements hook_entity_type_build().
*/
function opdavies_talks_entity_type_build(array &$entityTypes): void {
/** @var EntityTypeInterface[] $entityTypes */
if (isset($entityTypes['node'])) {
$entityTypes['node']->setStorageClass(NodeStorage::class);
}
}
/**
* Implements hook_token_info().
*/

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\opdavies_talks\Collection;
use Drupal\node\NodeInterface;
use Drupal\opdavies_talks\Entity\Node\Talk;
use Drupal\paragraphs\ParagraphInterface;
use Illuminate\Support\Collection;
@ -16,7 +17,8 @@ final class TalkCollection extends Collection {
* @return Collection|ParagraphInterface[]
*/
public function getEvents(): Collection {
return $this->flatMap(fn(Talk $talk): Collection => $talk->getEvents());
return $this
->flatMap(fn(Talk $talk): Collection => $talk->getEvents());
}
}

View file

@ -1,26 +1,27 @@
<?php
declare(strict_types=1);
namespace Drupal\opdavies_talks\Entity\Node;
use Drupal\discoverable_entity_bundle_classes\ContentEntityBundleInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\paragraphs\ParagraphInterface;
use Illuminate\Support\Collection;
/**
* Defines an talk node class.
*
* @ContentEntityBundleClass(
* label = @Translation("Talk"),
* entity_type = "node",
* bundle = "talk"
* );
*/
class Talk extends Node implements ContentEntityBundleInterface {
final class Talk {
public const FIELD_EVENTS = 'field_events';
public const FIELD_EVENT_DATE = 'field_event_date';
private NodeInterface $node;
public function __construct(EntityInterface $node) {
$this->node = $node;
}
public function addEvent(ParagraphInterface $event): void {
$this->set(
self::FIELD_EVENTS,
@ -28,6 +29,26 @@ class Talk extends Node implements ContentEntityBundleInterface {
);
}
/**
* Find the date for the latest event.
*
* @return string|null
*/
public function findLatestEventDate(): ?string {
return $this->getEvents()
->map(fn(ParagraphInterface $event) => $event->get('field_date')
->getString())
->max();
}
public function get(string $name): FieldItemListInterface {
return $this->node->get($name);
}
public function getCreatedTime(): int {
return (int) $this->node->getCreatedTime();
}
public function getEvents(): Collection {
return Collection::make($this->get(self::FIELD_EVENTS)
->referencedEntities());
@ -41,16 +62,24 @@ class Talk extends Node implements ContentEntityBundleInterface {
return (int) $this->get(self::FIELD_EVENT_DATE)->getString();
}
/**
* Find the date for the latest event.
*
* @return string|null
*/
public function findLatestEventDate(): ?string {
return $this->getEvents()
->map(fn(ParagraphInterface $event) => $event->get('field_date')
->getString())
->max();
public function id(): int {
return (int) $this->node->id();
}
public function label(): string {
return $this->node->label();
}
public function save(): void {
$this->node->save();
}
public function set(string $name, $value): void {
$this->node->set($name, $value);
}
public function setCreatedTime(int $timestamp): void {
$this->node->setCreatedTime($timestamp);
}
public function setEvents(array $events): void {
@ -61,4 +90,9 @@ class Talk extends Node implements ContentEntityBundleInterface {
$this->set(self::FIELD_EVENT_DATE, $date);
}
public static function createFromNode(EntityInterface $node): self {
// TODO: ensure that this is a node and a `talk` type.
return new self($node);
}
}

View file

@ -32,8 +32,9 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
return;
}
/** @var Talk $talk */
$talk = $event->getEntity();
$node = $event->getEntity();
$talk = Talk::createFromNode($node);
$this->reorderEvents($talk);
$this->updateCreatedDate($talk);
}
@ -42,9 +43,7 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
$events = $talk->getEvents();
$eventsByDate = $this->sortEventsByDate($events);
// If the original event IDs don't match the sorted event IDs, update the
// event field to use the sorted ones.
// @phpstan-ignore-next-line
// 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->setEvents($eventsByDate->toArray());
}

View file

@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\opdavies_talks\Collection\TalkCollection;
use Drupal\opdavies_talks\Entity\Node\Talk;
final class TalkRepository {
@ -20,7 +21,8 @@ final class TalkRepository {
public function findAll(): TalkCollection {
$talks = $this->nodeStorage->loadByProperties($this->defaultProperties());
return new TalkCollection($talks);
return (new TalkCollection($talks))
->map(fn(NodeInterface $node): Talk => Talk::createFromNode($node));
}
public function findAllPublished(): TalkCollection {
@ -31,7 +33,8 @@ final class TalkRepository {
],
));
return new TalkCollection($talks);
return (new TalkCollection($talks))
->map(fn(NodeInterface $node): Talk => Talk::createFromNode($node));
}
private function defaultProperties(): array {

View file

@ -47,7 +47,9 @@ final class TalkEventDateTest extends TalksTestBase {
$expected = Carbon::today()->addDays(4)->getTimestamp();
$talk = Node::load($talk->id());
$node = Node::load($talk->id());
$talk = Talk::createFromNode($node);
$this->assertNextEventDateIs($talk, $expected);
}
@ -76,7 +78,9 @@ final class TalkEventDateTest extends TalksTestBase {
$expected = Carbon::today()->subDays(2)->getTimestamp();
$talk = Node::load($talk->id());
$node = Node::load($talk->id());
$talk = Talk::createFromNode($node);
$this->assertNextEventDateIs($talk, $expected);
}
@ -90,7 +94,9 @@ final class TalkEventDateTest extends TalksTestBase {
$dateUpdater = $this->container->get(TalkDateUpdater::class);
$dateUpdater->__invoke();
$talk = Node::load($talk->id());
$node = Node::load($talk->id());
$talk = Talk::createFromNode($node);
$this->assertNoNextEventDate($talk);
}

View file

@ -20,7 +20,6 @@ abstract class TalksTestBase extends EntityKernelTestBase {
'datetime',
// Contrib.
'discoverable_entity_bundle_classes',
'entity_reference_revisions',
'paragraphs',
'hook_event_dispatcher',
@ -45,14 +44,14 @@ abstract class TalksTestBase extends EntityKernelTestBase {
}
protected function createTalk(array $overrides = []): Talk {
$talk = Node::create(array_merge([
$node = Node::create(array_merge([
'title' => 'Test Driven Drupal',
'type' => 'talk',
], $overrides));
$talk->save();
$node->save();
return $talk;
return Talk::createFromNode($node);
}
protected function setUp() {