From 10e4616e01a2b078078bf4a6771b4dffab463872 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 4 Sep 2020 12:24:05 +0100 Subject: [PATCH] Don't include certain terms as hashtags in tweets References #24 --- .../opdavies_blog/src/Entity/Node/Post.php | 9 +++++++ .../tests/src/Kernel/Entity/Node/PostTest.php | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/web/modules/custom/opdavies_blog/src/Entity/Node/Post.php b/web/modules/custom/opdavies_blog/src/Entity/Node/Post.php index 8aee8ab..78c93e8 100644 --- a/web/modules/custom/opdavies_blog/src/Entity/Node/Post.php +++ b/web/modules/custom/opdavies_blog/src/Entity/Node/Post.php @@ -61,10 +61,19 @@ class Post extends Node implements ContentEntityBundleInterface { private function convertTermsToHashtags(): string { return $this->getTags() + ->filter(fn(Term $term) => !$this->tagsToRemove() + ->contains($term->label())) ->map(fn(Term $term) => $this->convertTermToHashtag($term)) ->implode(' '); } + private function tagsToRemove(): Collection { + // TODO: Move these values into configuration/settings.php. + return new Collection([ + 'Drupal Planet', + ]); + } + private function convertTermToHashtag(Term $tag): string { $tagName = strtolower($tag->label()); $tagName = "#{$tagName}"; diff --git a/web/modules/custom/opdavies_blog/tests/src/Kernel/Entity/Node/PostTest.php b/web/modules/custom/opdavies_blog/tests/src/Kernel/Entity/Node/PostTest.php index f9df752..7e1c29e 100644 --- a/web/modules/custom/opdavies_blog/tests/src/Kernel/Entity/Node/PostTest.php +++ b/web/modules/custom/opdavies_blog/tests/src/Kernel/Entity/Node/PostTest.php @@ -67,6 +67,32 @@ final class PostTest extends EntityKernelTestBase { $this->assertSame($expected, $post->toTweet()); } + /** @test */ + public function certain_terms_are_not_added_as_hashtags(): void { + /** @var Post $post */ + $post = Node::create([ + 'field_tags' => [ + Term::create(['vid' => 'tags', 'name' => 'Drupal']), + Term::create(['vid' => 'tags', 'name' => 'Drupal Planet']), + Term::create(['vid' => 'tags', 'name' => 'PHP']), + ], + 'status' => NodeInterface::PUBLISHED, + 'title' => 'Drupal Planet should not be added as a hashtag', + 'type' => 'post', + ]); + $post->save(); + + $expected = <<assertSame($expected, $post->toTweet()); + } + protected function setUp() { parent::setUp();