Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
build(deps): remove discoverable_entity_bundle_classes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
opdavies committed Dec 17, 2021
1 parent 41e13fe commit 130bb92
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 133 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"drupal/config_split": "^1.4",
"drupal/core-composer-scaffold": "^8.8",
"drupal/core-recommended": "^8.8",
"drupal/discoverable_entity_bundle_classes": "^1.0",
"drupal/gin": "^3.0",
"drupal/gin_toolbar": "^1.0",
"drupal/honeypot": "^2.0",
Expand Down
49 changes: 1 addition & 48 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion web/modules/custom/blog/opdavies_blog.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 0 additions & 11 deletions web/modules/custom/blog/opdavies_blog.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*/
Expand Down
47 changes: 36 additions & 11 deletions web/modules/custom/blog/src/Entity/Node/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@

namespace Drupal\opdavies_blog\Entity\Node;

use Drupal\discoverable_entity_bundle_classes\ContentEntityBundleInterface;
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';
public const FIELD_SEND_TO_SOCIAL_MEDIA = 'field_send_to_social_media';
public const FIELD_SENT_TO_SOCIAL_MEDIA = 'field_sent_to_social_media';
public const FIELD_TAGS = 'field_tags';

private NodeInterface $node;

public function __construct(NodeInterface $node) {
$this->node = $node;
}

public function get(string $name) {
return $this->node->get($name);
}

public function getExternalLink(): ?array {
return ($link = $this->get(self::FIELD_EXTERNAL_LINK)->get(0))
? $link->getValue()
Expand All @@ -47,16 +48,32 @@ public function hasTweet(): bool {
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(...$args): void {
$this->node->set(...$args);
}

public function setTags(array $tags): void {
$this->set(self::FIELD_TAGS, $tags);
}
Expand All @@ -65,4 +82,12 @@ public function shouldSendToSocialMedia(): bool {
return (bool) $this->get(self::FIELD_SEND_TO_SOCIAL_MEDIA)->getString();
}

public function url(...$args): string {
return $this->node->url(...$args);
}

public static function createFromNode(NodeInterface $node): self {
return new self($node);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ public function sortTags(AbstractEntityEvent $event): void {
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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ public function create(array $overrides = []): Post {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ final class PostTest extends EntityKernelTestBase {
'link',
'taxonomy',

// Contrib.
'discoverable_entity_bundle_classes',

// Custom.
'opdavies_blog',
'opdavies_blog_test',
Expand Down
1 change: 0 additions & 1 deletion web/modules/custom/blog/tests/src/Kernel/PostTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ abstract class PostTestBase extends EntityKernelTestBase {
'link',

// Contrib.
'discoverable_entity_bundle_classes',
'hook_event_dispatcher',
'core_event_dispatcher',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

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 {

Expand All @@ -20,7 +21,6 @@ final class PushToSocialMediaTest extends EntityKernelTestBase {
'link',

// Contrib.
'discoverable_entity_bundle_classes',
'hook_event_dispatcher',
'core_event_dispatcher',

Expand Down Expand Up @@ -48,8 +48,8 @@ public function it_queues_a_post_when_it_is_created(): void {
$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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,8 +27,8 @@ public function it_reorders_tags_on_blog_posts_to_be_arranged_alphabetically():
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'],
Expand Down
2 changes: 0 additions & 2 deletions web/modules/custom/talks/opdavies_talks.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 0 additions & 11 deletions web/modules/custom/talks/opdavies_talks.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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().
*/
Expand Down
4 changes: 3 additions & 1 deletion web/modules/custom/talks/src/Collection/TalkCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\opdavies_talks\Collection;

use Drupal\node\NodeInterface;
use Drupal\opdavies_talks\Entity\Node\Talk;
use Drupal\paragraphs\ParagraphInterface;
use Illuminate\Support\Collection;
Expand All @@ -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());
}

}
Loading

0 comments on commit 130bb92

Please sign in to comment.