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
This commit is contained in:
parent
8385d6fef7
commit
0f07d3d231
|
@ -129,4 +129,5 @@ content:
|
||||||
settings: { }
|
settings: { }
|
||||||
third_party_settings: { }
|
third_party_settings: { }
|
||||||
hidden:
|
hidden:
|
||||||
|
field_external_link: true
|
||||||
field_sent_to_social_media: true
|
field_sent_to_social_media: true
|
||||||
|
|
|
@ -51,6 +51,7 @@ content:
|
||||||
third_party_settings: { }
|
third_party_settings: { }
|
||||||
hidden:
|
hidden:
|
||||||
field_excerpt: true
|
field_excerpt: true
|
||||||
|
field_external_link: true
|
||||||
field_has_tweet: true
|
field_has_tweet: true
|
||||||
field_images: true
|
field_images: true
|
||||||
field_series: true
|
field_series: true
|
||||||
|
|
|
@ -33,6 +33,7 @@ content:
|
||||||
third_party_settings: { }
|
third_party_settings: { }
|
||||||
hidden:
|
hidden:
|
||||||
body: true
|
body: true
|
||||||
|
field_external_link: true
|
||||||
field_has_tweet: true
|
field_has_tweet: true
|
||||||
field_images: true
|
field_images: true
|
||||||
field_sent_to_social_media: true
|
field_sent_to_social_media: true
|
||||||
|
|
23
config/default/field.field.node.post.field_external_link.yml
Normal file
23
config/default/field.field.node.post.field_external_link.yml
Normal file
|
@ -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
|
19
config/default/field.storage.node.field_external_link.yml
Normal file
19
config/default/field.storage.node.field_external_link.yml
Normal file
|
@ -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
|
28
web/modules/custom/custom/hooks/node_links/alter.inc
Normal file
28
web/modules/custom/custom/hooks/node_links/alter.inc
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Node links alter hooks.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Drupal\Core\Url;
|
||||||
|
use Drupal\node\NodeInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements hook_node_links_alter().
|
||||||
|
*/
|
||||||
|
function custom_node_links_alter(array &$links, NodeInterface $node): void {
|
||||||
|
if (!method_exists($node, 'getExternalLink')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($link = $node->getExternalLink()) {
|
||||||
|
$links['node']['#links']['node-readmore']['url'] = Url::fromUri($link['uri']);
|
||||||
|
$links['node']['#links']['node-readmore']['title'] = t('Read more<span class="visually-hidden"> about @title</span> (<span class="visually-hidden">on </span>@domain)', [
|
||||||
|
'@domain' => $link['title'],
|
||||||
|
'@title' => $node->label(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
21
web/modules/custom/custom/hooks/preprocess/node.inc
Normal file
21
web/modules/custom/custom/hooks/preprocess/node.inc
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Node preprocess functions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements hook_preprocess_HOOK().
|
||||||
|
*/
|
||||||
|
function custom_preprocess_node(array &$variables): void {
|
||||||
|
if (!method_exists($variables['node'], 'getExternalLink')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($link = $variables['node']->getExternalLink()) {
|
||||||
|
$variables['url'] = $link['uri'];
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,12 @@ use Drupal\node\Entity\Node;
|
||||||
*/
|
*/
|
||||||
class Post extends Node implements ContentEntityBundleInterface {
|
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 {
|
public function hasBeenSentToSocialMedia(): bool {
|
||||||
return (bool) $this->get('field_sent_to_social_media')->getString();
|
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();
|
return (bool) $this->get('field_has_tweet')->getString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isExternalPost(): bool {
|
||||||
|
return (bool) $this->getExternalLink();
|
||||||
|
}
|
||||||
|
|
||||||
public function toTweet(): string {
|
public function toTweet(): string {
|
||||||
// TODO: Add tags.
|
// TODO: Add tags.
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ namespace Drupal\custom\EventSubscriber;
|
||||||
use Drupal\custom\Entity\Node\Post;
|
use Drupal\custom\Entity\Node\Post;
|
||||||
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
|
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
|
||||||
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
|
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
|
||||||
use Drupal\node\NodeInterface;
|
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
|
||||||
final class PushBlogPostToSocialMedia implements EventSubscriberInterface {
|
final class PushBlogPostToSocialMedia implements EventSubscriberInterface {
|
||||||
|
@ -28,7 +27,7 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var NodeInterface $entity */
|
/** @var Post $entity */
|
||||||
if ($entity->bundle() != 'post') {
|
if ($entity->bundle() != 'post') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -42,6 +41,10 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($entity->isExternalPost()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$url = \Drupal::configFactory()->get('opdavies_talks.config')
|
$url = \Drupal::configFactory()->get('opdavies_talks.config')
|
||||||
->get('zapier_post_tweet_url');
|
->get('zapier_post_tweet_url');
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue