diff --git a/config/default/core.entity_form_display.node.post.default.yml b/config/default/core.entity_form_display.node.post.default.yml index 99bc3fc..96be121 100644 --- a/config/default/core.entity_form_display.node.post.default.yml +++ b/config/default/core.entity_form_display.node.post.default.yml @@ -129,4 +129,5 @@ content: settings: { } third_party_settings: { } hidden: + field_external_link: true field_sent_to_social_media: true diff --git a/config/default/core.entity_view_display.node.post.default.yml b/config/default/core.entity_view_display.node.post.default.yml index bad63da..8203254 100644 --- a/config/default/core.entity_view_display.node.post.default.yml +++ b/config/default/core.entity_view_display.node.post.default.yml @@ -51,6 +51,7 @@ content: third_party_settings: { } hidden: field_excerpt: true + field_external_link: true field_has_tweet: true field_images: true field_series: true diff --git a/config/default/core.entity_view_display.node.post.teaser.yml b/config/default/core.entity_view_display.node.post.teaser.yml index 6c4f01b..a1e15ce 100644 --- a/config/default/core.entity_view_display.node.post.teaser.yml +++ b/config/default/core.entity_view_display.node.post.teaser.yml @@ -33,6 +33,7 @@ content: third_party_settings: { } hidden: body: true + field_external_link: true field_has_tweet: true field_images: true field_sent_to_social_media: true diff --git a/config/default/field.field.node.post.field_external_link.yml b/config/default/field.field.node.post.field_external_link.yml new file mode 100644 index 0000000..e120bb0 --- /dev/null +++ b/config/default/field.field.node.post.field_external_link.yml @@ -0,0 +1,23 @@ +uuid: 09df4309-5909-4347-ae8a-e35f84e1930d +langcode: en +status: true +dependencies: + config: + - field.storage.node.field_external_link + - node.type.post + module: + - link +id: node.post.field_external_link +field_name: field_external_link +entity_type: node +bundle: post +label: 'External link' +description: '' +required: false +translatable: false +default_value: { } +default_value_callback: '' +settings: + link_type: 16 + title: 2 +field_type: link diff --git a/config/default/field.storage.node.field_external_link.yml b/config/default/field.storage.node.field_external_link.yml new file mode 100644 index 0000000..48094ed --- /dev/null +++ b/config/default/field.storage.node.field_external_link.yml @@ -0,0 +1,19 @@ +uuid: 2faf15c2-a456-485c-b7bd-30ff75e4cc21 +langcode: en +status: true +dependencies: + module: + - link + - node +id: node.field_external_link +field_name: field_external_link +entity_type: node +type: link +settings: { } +module: link +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/web/modules/custom/custom/hooks/node_links/alter.inc b/web/modules/custom/custom/hooks/node_links/alter.inc new file mode 100644 index 0000000..e49cfa9 --- /dev/null +++ b/web/modules/custom/custom/hooks/node_links/alter.inc @@ -0,0 +1,28 @@ +getExternalLink()) { + $links['node']['#links']['node-readmore']['url'] = Url::fromUri($link['uri']); + $links['node']['#links']['node-readmore']['title'] = t('Read more about @title (on @domain)', [ + '@domain' => $link['title'], + '@title' => $node->label(), + ]); + } +} diff --git a/web/modules/custom/custom/hooks/preprocess/node.inc b/web/modules/custom/custom/hooks/preprocess/node.inc new file mode 100644 index 0000000..aabde2b --- /dev/null +++ b/web/modules/custom/custom/hooks/preprocess/node.inc @@ -0,0 +1,21 @@ +getExternalLink()) { + $variables['url'] = $link['uri']; + } +} diff --git a/web/modules/custom/custom/src/Entity/Node/Post.php b/web/modules/custom/custom/src/Entity/Node/Post.php index 4f33f9b..b119c3c 100644 --- a/web/modules/custom/custom/src/Entity/Node/Post.php +++ b/web/modules/custom/custom/src/Entity/Node/Post.php @@ -18,6 +18,12 @@ use Drupal\node\Entity\Node; */ class Post extends Node implements ContentEntityBundleInterface { + public function getExternalLink(): ?array { + return ($link = $this->get('field_external_link')->get(0)) + ? $link->getValue() + : NULL; + } + public function hasBeenSentToSocialMedia(): bool { return (bool) $this->get('field_sent_to_social_media')->getString(); } @@ -26,6 +32,10 @@ class Post extends Node implements ContentEntityBundleInterface { return (bool) $this->get('field_has_tweet')->getString(); } + public function isExternalPost(): bool { + return (bool) $this->getExternalLink(); + } + public function toTweet(): string { // TODO: Add tags. diff --git a/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php b/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php index ae1e908..fd6581c 100644 --- a/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php +++ b/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php @@ -7,7 +7,6 @@ namespace Drupal\custom\EventSubscriber; use Drupal\custom\Entity\Node\Post; use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent; use Drupal\hook_event_dispatcher\HookEventDispatcherInterface; -use Drupal\node\NodeInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; final class PushBlogPostToSocialMedia implements EventSubscriberInterface { @@ -28,7 +27,7 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { return; } - /** @var NodeInterface $entity */ + /** @var Post $entity */ if ($entity->bundle() != 'post') { return; } @@ -42,6 +41,10 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { return; } + if ($entity->isExternalPost()) { + return; + } + $url = \Drupal::configFactory()->get('opdavies_talks.config') ->get('zapier_post_tweet_url');