From 0f07d3d23157a978f4cf567ca92f4a3b1d5fe8a8 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 14 Aug 2020 14:01:23 +0100 Subject: [PATCH] Add ability to add links to external posts Add the ability to add links to external blog posts within my blog feed. This is done based on a new `field_external_link` field that allows for adding the external link URL and the domain name as the title. The node links are then overridden to use the external link if there is one, so the node title and 'read more' links are changed to use the external link. Currently, automated tweets are not generated for external posts. Fixes #182 --- ....entity_form_display.node.post.default.yml | 1 + ....entity_view_display.node.post.default.yml | 1 + ...e.entity_view_display.node.post.teaser.yml | 1 + ...ld.field.node.post.field_external_link.yml | 23 +++++++++++++++ ...field.storage.node.field_external_link.yml | 19 +++++++++++++ .../custom/custom/hooks/node_links/alter.inc | 28 +++++++++++++++++++ .../custom/custom/hooks/preprocess/node.inc | 21 ++++++++++++++ .../custom/custom/src/Entity/Node/Post.php | 10 +++++++ .../PushBlogPostToSocialMedia.php | 7 +++-- 9 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 config/default/field.field.node.post.field_external_link.yml create mode 100644 config/default/field.storage.node.field_external_link.yml create mode 100644 web/modules/custom/custom/hooks/node_links/alter.inc create mode 100644 web/modules/custom/custom/hooks/preprocess/node.inc 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');