2020-05-29 21:35:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-24 09:26:44 +01:00
|
|
|
namespace Drupal\opdavies_blog\Entity\Node;
|
2020-05-29 21:35:21 +01:00
|
|
|
|
|
|
|
use Drupal\discoverable_entity_bundle_classes\ContentEntityBundleInterface;
|
2020-06-26 19:43:58 +01:00
|
|
|
use Drupal\node\Entity\Node;
|
2020-08-28 11:35:54 +01:00
|
|
|
use Drupal\taxonomy\Entity\Term;
|
|
|
|
use Illuminate\Support\Collection;
|
2020-05-29 21:35:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines an blog post node class.
|
|
|
|
*
|
|
|
|
* @ContentEntityBundleClass(
|
|
|
|
* label = @Translation("Blog post"),
|
|
|
|
* entity_type = "node",
|
|
|
|
* bundle = "post"
|
|
|
|
* );
|
|
|
|
*/
|
|
|
|
class Post extends Node implements ContentEntityBundleInterface {
|
|
|
|
|
2020-11-10 19:34:15 +00:00
|
|
|
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';
|
|
|
|
|
2020-08-14 14:01:23 +01:00
|
|
|
public function getExternalLink(): ?array {
|
2020-11-10 19:34:15 +00:00
|
|
|
return ($link = $this->get(self::FIELD_EXTERNAL_LINK)->get(0))
|
2020-08-14 14:01:23 +01:00
|
|
|
? $link->getValue()
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:35:54 +01:00
|
|
|
/**
|
|
|
|
* @return Collection|Term[]
|
|
|
|
*/
|
|
|
|
public function getTags(): Collection {
|
2020-11-10 19:34:15 +00:00
|
|
|
return new Collection($this->get(self::FIELD_TAGS)->referencedEntities());
|
2020-08-28 11:35:54 +01:00
|
|
|
}
|
|
|
|
|
2020-08-12 20:28:19 +01:00
|
|
|
public function hasBeenSentToSocialMedia(): bool {
|
2020-11-10 19:34:15 +00:00
|
|
|
return (bool) $this->get(self::FIELD_SENT_TO_SOCIAL_MEDIA)->getString();
|
2020-08-12 20:28:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 21:35:21 +01:00
|
|
|
public function hasTweet(): bool {
|
2020-11-10 19:34:15 +00:00
|
|
|
return (bool) $this->get(self::FIELD_HAS_TWEET)->getString();
|
2020-05-29 21:35:21 +01:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:01:23 +01:00
|
|
|
public function isExternalPost(): bool {
|
|
|
|
return (bool) $this->getExternalLink();
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:35:54 +01:00
|
|
|
public function setTags(array $tags): void {
|
2020-11-10 19:34:15 +00:00
|
|
|
$this->set(self::FIELD_TAGS, $tags);
|
2020-08-28 11:35:54 +01:00
|
|
|
}
|
|
|
|
|
2020-10-07 22:54:56 +01:00
|
|
|
public function shouldSendToSocialMedia(): bool {
|
2020-11-10 19:34:15 +00:00
|
|
|
return (bool) $this->get(self::FIELD_SEND_TO_SOCIAL_MEDIA)->getString();
|
2020-10-07 22:54:56 +01:00
|
|
|
}
|
|
|
|
|
2020-07-19 20:12:08 +01:00
|
|
|
public function toTweet(): string {
|
2020-09-04 12:04:55 +01:00
|
|
|
$parts = [
|
|
|
|
$this->label(),
|
|
|
|
$this->url('canonical', ['absolute' => TRUE]),
|
|
|
|
$this->convertTermsToHashtags(),
|
|
|
|
];
|
2020-07-19 20:12:08 +01:00
|
|
|
|
|
|
|
return implode(PHP_EOL . PHP_EOL, $parts);
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:04:55 +01:00
|
|
|
private function convertTermsToHashtags(): string {
|
|
|
|
return $this->getTags()
|
2020-09-04 12:24:05 +01:00
|
|
|
->filter(fn(Term $term) => !$this->tagsToRemove()
|
|
|
|
->contains($term->label()))
|
2020-09-04 12:04:55 +01:00
|
|
|
->map(fn(Term $term) => $this->convertTermToHashtag($term))
|
|
|
|
->implode(' ');
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:24:05 +01:00
|
|
|
private function tagsToRemove(): Collection {
|
|
|
|
// TODO: Move these values into configuration/settings.php.
|
|
|
|
return new Collection([
|
|
|
|
'Drupal Planet',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:04:55 +01:00
|
|
|
private function convertTermToHashtag(Term $tag): string {
|
2020-09-04 19:45:30 +01:00
|
|
|
return '#' . (new Collection(explode(' ', $tag->label())))
|
2020-09-04 20:56:04 +01:00
|
|
|
->map(fn(string $word): string => ucfirst($word))
|
|
|
|
->implode('');
|
2020-09-04 12:04:55 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 21:35:21 +01:00
|
|
|
}
|