get(self::FIELD_EXTERNAL_LINK)->get(0)) ? $link->getValue() : NULL; } /** * @return Collection|Term[] */ public function getTags(): Collection { return new Collection($this->get(self::FIELD_TAGS)->referencedEntities()); } public function hasBeenSentToSocialMedia(): bool { return (bool) $this->get(self::FIELD_SENT_TO_SOCIAL_MEDIA)->getString(); } public function hasTweet(): bool { return (bool) $this->get(self::FIELD_HAS_TWEET)->getString(); } public function isExternalPost(): bool { return (bool) $this->getExternalLink(); } public function markAsSentToSocialMedia(): self { $this->set(self::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE); return $this; } public function setTags(array $tags): void { $this->set(self::FIELD_TAGS, $tags); } public function shouldSendToSocialMedia(): bool { return (bool) $this->get(self::FIELD_SEND_TO_SOCIAL_MEDIA)->getString(); } public function toTweet(): string { $parts = [ $this->label(), $this->url('canonical', ['absolute' => TRUE]), $this->convertTermsToHashtags(), ]; return implode(PHP_EOL . PHP_EOL, $parts); } 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 { return '#' . (new Collection(explode(' ', $tag->label()))) ->map(fn(string $word): string => ucfirst($word)) ->implode(''); } }