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

@ -4,6 +4,5 @@ core_version_requirement: ^8 || ^9
package: Custom
dependencies:
- drupal:node
- discoverable_entity_bundle_classes:discoverable_entity_bundle_classes
- hook_event_dispatcher:hook_event_dispatcher
- paragraphs:paragraphs

View file

@ -6,19 +6,8 @@
*/
use Drupal\Core\Url;
use Drupal\discoverable_entity_bundle_classes\Storage\Node\NodeStorage;
use Drupal\node\NodeInterface;
/**
* Implements hook_entity_type_build().
*/
function opdavies_blog_entity_type_build(array &$entityTypes): void {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entityTypes */
if (isset($entityTypes['node'])) {
$entityTypes['node']->setStorageClass(NodeStorage::class);
}
}
/**
* Implements hook_node_links_alter().
*/

View file

@ -4,21 +4,14 @@ declare(strict_types=1);
namespace Drupal\opdavies_blog\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\taxonomy\Entity\Term;
use Illuminate\Support\Collection;
/**
* Defines an blog post node class.
*
* @ContentEntityBundleClass(
* label = @Translation("Blog post"),
* entity_type = "node",
* bundle = "post"
* );
*/
class Post extends Node implements ContentEntityBundleInterface {
final class Post {
public const FIELD_EXTERNAL_LINK = 'field_external_link';
public const FIELD_HAS_TWEET = 'field_has_tweet';
@ -26,12 +19,31 @@ class Post extends Node implements ContentEntityBundleInterface {
public const FIELD_SENT_TO_SOCIAL_MEDIA = 'field_sent_to_social_media';
public const FIELD_TAGS = 'field_tags';
private NodeInterface $node;
public function __construct(EntityInterface $node) {
$this->node = $node;
}
public function bundle(): string {
return 'post';
}
public function get(string $name): FieldItemListInterface {
return $this->node->get($name);
}
public function getExternalLink(): ?array {
return ($link = $this->get(self::FIELD_EXTERNAL_LINK)->get(0))
? $link->getValue()
: NULL;
}
public function getNode(): NodeInterface {
return $this->node;
}
/**
* @return Collection|Term[]
*/
@ -47,16 +59,32 @@ class Post extends Node implements ContentEntityBundleInterface {
return (bool) $this->get(self::FIELD_HAS_TWEET)->getString();
}
public function id(): int {
return (int) $this->node->id();
}
public function isExternalPost(): bool {
return (bool) $this->getExternalLink();
}
public function label(): string {
return $this->node->label();
}
public function markAsSentToSocialMedia(): self {
$this->set(self::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE);
return $this;
}
public function save(): void {
$this->node->save();
}
public function set(string $name, $value): void {
$this->node->set($name, $value);
}
public function setTags(array $tags): void {
$this->set(self::FIELD_TAGS, $tags);
}
@ -65,4 +93,13 @@ class Post extends Node implements ContentEntityBundleInterface {
return (bool) $this->get(self::FIELD_SEND_TO_SOCIAL_MEDIA)->getString();
}
public function url(string $type, array $options = []): string {
return $this->node->url($type, $options);
}
public static function createFromNode(EntityInterface $node): self {
// TODO: ensure that this is a node and a `post` type.
return new self($node);
}
}

View file

@ -28,15 +28,16 @@ final class SortTagsAlphabeticallyWhenPostIsSaved implements EventSubscriberInte
return;
}
/** @var Post $entity */
if ($entity->bundle() != 'post') {
return;
}
$sortedTags = $entity->getTags()
$post = Post::createFromNode($entity);
$sortedTags = $post->getTags()
->sortBy(fn(TermInterface $tag) => $tag->label());
$entity->setTags($sortedTags->toArray());
$post->setTags($sortedTags->toArray());
}
}

View file

@ -44,7 +44,6 @@ class RelatedPostsBlock extends BlockBase implements ContainerFactoryPluginInter
$pluginId,
$pluginDefinition
): self {
// @phpstan-ignore-next-line
return new self(
$configuration,
$pluginId,
@ -66,7 +65,7 @@ class RelatedPostsBlock extends BlockBase implements ContainerFactoryPluginInter
$build['content'] = [
'#items' => $relatedPosts
->sortByDesc(fn(Post $post) => $post->getCreatedTime())
->sortByDesc(fn(Post $post) => $post->getNode()->getCreatedTime())
->map(fn(Post $post) => $this->generateLinkToPost($post))
->slice(0, 3)
->toArray(),
@ -93,7 +92,7 @@ class RelatedPostsBlock extends BlockBase implements ContainerFactoryPluginInter
private function generateLinkToPost(Post $post): Link {
return Link::createFromRoute(
$post->getTitle(),
$post->label(),
'entity.node.canonical',
['node' => $post->id()]
);

View file

@ -68,21 +68,19 @@ final class PostPusherQueueWorker extends QueueWorkerBase implements ContainerFa
return;
}
if (!$post->isLatestRevision()) {
$post = $this->nodeStorage->load($post->id());
if (!$post->getNode()->isLatestRevision()) {
$node = $this->nodeStorage->load($post->id());
$post = Post::createFromNode($node);
// @phpstan-ignore-next-line
if (!$this->shouldBePushed($post)) {
return;
}
}
foreach ($this->postPushers as $pusher) {
// @phpstan-ignore-next-line
$pusher->push($post);
}
// @phpstan-ignore-next-line
$post->set(Post::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE);
$post->save();
}
@ -92,7 +90,7 @@ final class PostPusherQueueWorker extends QueueWorkerBase implements ContainerFa
return FALSE;
}
if (!$post->isPublished()) {
if (!$post->getNode()->isPublished()) {
return FALSE;
}

View file

@ -41,10 +41,9 @@ final class PostFactory {
Post::FIELD_TAGS => $this->tags->toArray(),
];
/** @var Post $post */
$post = Node::create($values + $overrides);
return $post;
return Post::createFromNode($post);
}
public function setTitle(string $title): self {

View file

@ -16,9 +16,6 @@ final class PostTest extends EntityKernelTestBase {
'link',
'taxonomy',
// Contrib.
'discoverable_entity_bundle_classes',
// Custom.
'opdavies_blog',
'opdavies_blog_test',

View file

@ -18,7 +18,6 @@ abstract class PostTestBase extends EntityKernelTestBase {
'link',
// Contrib.
'discoverable_entity_bundle_classes',
'hook_event_dispatcher',
'core_event_dispatcher',

View file

@ -6,8 +6,9 @@ namespace Drupal\Tests\opdavies_blog\Kernel;
use Drupal\Core\Queue\QueueInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\opdavies_blog\Entity\Node\Post;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\node\NodeInterface;
use Drupal\opdavies_blog\Entity\Node\Post;
final class PushToSocialMediaTest extends EntityKernelTestBase {
@ -20,7 +21,6 @@ final class PushToSocialMediaTest extends EntityKernelTestBase {
'link',
// Contrib.
'discoverable_entity_bundle_classes',
'hook_event_dispatcher',
'core_event_dispatcher',
@ -48,8 +48,8 @@ final class PushToSocialMediaTest extends EntityKernelTestBase {
$post = $item->data['post'];
$this->assertNotNull($post);
$this->assertInstanceOf(Post::class, $post);
$this->assertSame('1', $post->id());
$this->assertInstanceOf(NodeInterface::class, $post);
$this->assertSame('post', $post->bundle());
$this->assertSame('Ignoring PHPCS sniffs within PHPUnit tests', $post->getTitle());
}

View file

@ -5,6 +5,7 @@
namespace Drupal\Tests\opdavies_blog\Kernel;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\opdavies_blog\Entity\Node\Post;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\TermInterface;
@ -26,8 +27,8 @@ final class ReorderBlogTagsTest extends PostTestBase {
Post::FIELD_TAGS => [3, 1, 2],
]);
/** @var Post $post */
$post = Node::load($post->id());
$node = Node::load($post->id());
$post = Post::createFromNode($node);
$this->assertSame(
['Drupal', 'PHP', 'Symfony'],