From 2f24cf7235c0f9f7093d6b00256f4a32e9c5055e Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 22 May 2021 14:21:05 +0100 Subject: [PATCH] Combine hook content back in the .module file --- .../custom/blog/hooks/entity_type/build.inc | 20 ------ .../custom/blog/hooks/node_links/alter.inc | 28 --------- .../custom/blog/hooks/preprocess/block.inc | 18 ------ .../custom/blog/hooks/preprocess/node.inc | 21 ------- web/modules/custom/blog/opdavies_blog.module | 62 ++++++++++++++++--- 5 files changed, 52 insertions(+), 97 deletions(-) delete mode 100644 web/modules/custom/blog/hooks/entity_type/build.inc delete mode 100644 web/modules/custom/blog/hooks/node_links/alter.inc delete mode 100644 web/modules/custom/blog/hooks/preprocess/block.inc delete mode 100644 web/modules/custom/blog/hooks/preprocess/node.inc diff --git a/web/modules/custom/blog/hooks/entity_type/build.inc b/web/modules/custom/blog/hooks/entity_type/build.inc deleted file mode 100644 index 4978ce1..0000000 --- a/web/modules/custom/blog/hooks/entity_type/build.inc +++ /dev/null @@ -1,20 +0,0 @@ -setStorageClass(NodeStorage::class); - } -} diff --git a/web/modules/custom/blog/hooks/node_links/alter.inc b/web/modules/custom/blog/hooks/node_links/alter.inc deleted file mode 100644 index bc94afc..0000000 --- a/web/modules/custom/blog/hooks/node_links/alter.inc +++ /dev/null @@ -1,28 +0,0 @@ -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/blog/hooks/preprocess/block.inc b/web/modules/custom/blog/hooks/preprocess/block.inc deleted file mode 100644 index f9032b3..0000000 --- a/web/modules/custom/blog/hooks/preprocess/block.inc +++ /dev/null @@ -1,18 +0,0 @@ -getExternalLink()) { - $variables['url'] = $link['uri']; - } -} diff --git a/web/modules/custom/blog/opdavies_blog.module b/web/modules/custom/blog/opdavies_blog.module index 6bb1aa5..062e242 100644 --- a/web/modules/custom/blog/opdavies_blog.module +++ b/web/modules/custom/blog/opdavies_blog.module @@ -2,17 +2,59 @@ /** * @file - * Custom module. + * Custom blog code. */ -declare(strict_types=1); +use Drupal\Core\Url; +use Drupal\discoverable_entity_bundle_classes\Storage\Node\NodeStorage; +use Drupal\node\NodeInterface; -use Symfony\Component\Finder\Finder; - -$finder = Finder::create() - ->in(__DIR__ . DIRECTORY_SEPARATOR . 'hooks') - ->name('/.[php|inc]$/'); - -foreach ($finder as $file) { - include $file->getPathname(); +/** + * Implements hook_entity_type_build(). + */ +function opdavies_blog_entity_type_build(array &$entityTypes): void { + /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entityTypes */ + if (isset($entityTypes['node'])) { + $entityTypes['node']->setStorageClass(NodeStorage::class); + } +} + +/** + * Implements hook_node_links_alter(). + */ +function opdavies_blog_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 about @title (on @domain)', [ + '@domain' => $link['title'], + '@title' => $node->label(), + ]); + } +} + +/** + * Implements hook_preprocess_HOOK(). + */ +function opdavies_blog_preprocess_block(array &$variables): void { + // Add the 'markup' class to blocks. + if (in_array($variables['plugin_id'], ['views_block:featured_blog_posts-block_1'])) { + $variables['attributes']['class'][] = 'markup'; + } +} + +/** + * Implements hook_preprocess_HOOK(). + */ +function opdavies_blog_preprocess_node(array &$variables): void { + if (!method_exists($variables['node'], 'getExternalLink')) { + return; + } + + if ($link = $variables['node']->getExternalLink()) { + $variables['url'] = $link['uri']; + } }