From c1282ccde7f1d10dad470bd79ab7683cbc8a1c5d Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Mon, 18 Nov 2024 10:51:25 -0700 Subject: [PATCH 1/6] CuBoulder/ucb_article_syndication#3 Creates syndication article list when module is installed --- src/ArticleSyndication.php | 48 +++++++++++++++++++++++++++- ucb_article_syndication.info.yml | 4 ++- ucb_article_syndication.install | 1 + ucb_article_syndication.services.yml | 5 +++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/ArticleSyndication.php b/src/ArticleSyndication.php index 3de7191..882b38d 100644 --- a/src/ArticleSyndication.php +++ b/src/ArticleSyndication.php @@ -3,6 +3,10 @@ namespace Drupal\ucb_article_syndication; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\node\Entity\Node; +use Drupal\path_alias\AliasManagerInterface; +use Drupal\pathauto\PathautoState; +use Psr\Log\LoggerInterface; /** * The Article Syndication service contains functions used by the module. @@ -16,14 +20,38 @@ class ArticleSyndication { */ protected $configFactory; + /** + * The path alias manager. + * + * @var \Drupal\path_alias\AliasManagerInterface + */ + protected $aliasManager; + + /** + * The logger channel for this module. + * + * @var \Psr\Log\LoggerInterface + */ + protected $logger; + /** * Constructs the Article Syndication service. * * @param \Drupal\Core\Extension\ConfigFactoryInterface $config_factory * The config factory. + * @param \Drupal\path_alias\AliasManagerInterface $alias_manager + * The path alias manager. + * @param \Psr\Log\LoggerInterface $logger + * The logger channel for this module. */ - public function __construct(ConfigFactoryInterface $config_factory) { + public function __construct( + ConfigFactoryInterface $config_factory, + AliasManagerInterface $alias_manager, + LoggerInterface $logger + ) { $this->configFactory = $config_factory; + $this->aliasManager = $alias_manager; + $this->logger = $logger; } /** @@ -99,4 +127,22 @@ public function showSyndicationFields() { ->save(); } + /** + * Creates the `/syndication` article list. + */ + public function createSyndicationArticleList() { + if (preg_match('/node\/(\d+)/', $this->aliasManager->getPathByAlias('/syndication'))) { + $this->logger->warning('A syndication article list wasn’t created because a node already exists at that path.'); + } + else { + $node = Node::create([ + 'type' => 'ucb_article_list', + 'title' => 'Article Results', + 'path' => ['alias' => '/syndication', 'pathauto' => PathautoState::SKIP], + 'body' => '', + ]); + $node->enforceIsNew()->save(); + } + } + } diff --git a/ucb_article_syndication.info.yml b/ucb_article_syndication.info.yml index 5765a27..abef893 100644 --- a/ucb_article_syndication.info.yml +++ b/ucb_article_syndication.info.yml @@ -2,10 +2,12 @@ name: CU Boulder Article Syndication description: Extends the article content type to add the taxonomies needed for article syndication. core_version_requirement: ^10 || ^11 type: module -version: '1.0.1' +version: '1.1' package: CU Boulder dependencies: - cu_boulder_content_types - field - node + - path_alias + - pathauto - taxonomy diff --git a/ucb_article_syndication.install b/ucb_article_syndication.install index 6a3c158..9f25ae3 100644 --- a/ucb_article_syndication.install +++ b/ucb_article_syndication.install @@ -13,4 +13,5 @@ function ucb_article_syndication_install() { $articleSyndication = \Drupal::service('ucb_article_syndication'); $articleSyndication->showSyndicationFields(); + $articleSyndication->createSyndicationArticleList(); } diff --git a/ucb_article_syndication.services.yml b/ucb_article_syndication.services.yml index c6d27a4..97dc08a 100644 --- a/ucb_article_syndication.services.yml +++ b/ucb_article_syndication.services.yml @@ -1,5 +1,10 @@ services: + logger.channel.ucb_article_syndication: + parent: logger.channel_base + arguments: ['ucb_article_syndication'] ucb_article_syndication: class: 'Drupal\ucb_article_syndication\ArticleSyndication' arguments: - '@config.factory' + - '@path_alias.manager' + - '@logger.channel.ucb_article_syndication' From a3dfb0d452b7c4d1beb42afc15e30df1ea72e953 Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Mon, 18 Nov 2024 10:56:25 -0700 Subject: [PATCH 2/6] CuBoulder/ucb_article_syndication#3 Updates syndication article list path --- src/ArticleSyndication.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ArticleSyndication.php b/src/ArticleSyndication.php index 882b38d..8eafd80 100644 --- a/src/ArticleSyndication.php +++ b/src/ArticleSyndication.php @@ -13,6 +13,11 @@ */ class ArticleSyndication { + /** + * The path alias at which the syndication article list should reside. + */ + const SYNDICATION_PATH = '/syndicate'; + /** * The config factory. * @@ -131,14 +136,14 @@ public function showSyndicationFields() { * Creates the `/syndication` article list. */ public function createSyndicationArticleList() { - if (preg_match('/node\/(\d+)/', $this->aliasManager->getPathByAlias('/syndication'))) { + if (preg_match('/node\/(\d+)/', $this->aliasManager->getPathByAlias($this::SYNDICATION_PATH))) { $this->logger->warning('A syndication article list wasn’t created because a node already exists at that path.'); } else { $node = Node::create([ 'type' => 'ucb_article_list', 'title' => 'Article Results', - 'path' => ['alias' => '/syndication', 'pathauto' => PathautoState::SKIP], + 'path' => ['alias' => $this::SYNDICATION_PATH, 'pathauto' => PathautoState::SKIP], 'body' => '', ]); $node->enforceIsNew()->save(); From 45bc63b27fdbbf57e7d79750b3b33913232054e1 Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Mon, 18 Nov 2024 11:08:50 -0700 Subject: [PATCH 3/6] CuBoulder/ucb_article_syndication#3 Refreshes comment describing `createSyndicationArticleList` function --- src/ArticleSyndication.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ArticleSyndication.php b/src/ArticleSyndication.php index 8eafd80..038725c 100644 --- a/src/ArticleSyndication.php +++ b/src/ArticleSyndication.php @@ -133,7 +133,12 @@ public function showSyndicationFields() { } /** - * Creates the `/syndication` article list. + * Creates the syndication article list. + * + * The syndication article list is a special article list which allows + * category, audience, and unit term ids to be passed in as URL parameters. + * Its main use case is to be used as the "read more" page for the Campus + * News block. */ public function createSyndicationArticleList() { if (preg_match('/node\/(\d+)/', $this->aliasManager->getPathByAlias($this::SYNDICATION_PATH))) { From 64432309d35e5584ffe97c79794b6c51a2415c6b Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Mon, 18 Nov 2024 11:15:58 -0700 Subject: [PATCH 4/6] CuBoulder/ucb_article_syndication#3 Includes path alias in warning messsage when failing to create a syndication article list --- src/ArticleSyndication.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ArticleSyndication.php b/src/ArticleSyndication.php index 038725c..4ba3ec0 100644 --- a/src/ArticleSyndication.php +++ b/src/ArticleSyndication.php @@ -139,10 +139,13 @@ public function showSyndicationFields() { * category, audience, and unit term ids to be passed in as URL parameters. * Its main use case is to be used as the "read more" page for the Campus * News block. + * + * A new article list will be created only if a node doesn’t already exist at + * the same path. */ public function createSyndicationArticleList() { if (preg_match('/node\/(\d+)/', $this->aliasManager->getPathByAlias($this::SYNDICATION_PATH))) { - $this->logger->warning('A syndication article list wasn’t created because a node already exists at that path.'); + $this->logger->warning('A syndication article list wasn’t created because a node already exists at the path ' . $this::SYNDICATION_PATH . '.'); } else { $node = Node::create([ From f614eea4d2710e4e8cb03210a8b9c667edc4991c Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Tue, 19 Nov 2024 13:42:10 -0700 Subject: [PATCH 5/6] CuBoulder/ucb_article_syndication#3 Adds minor code refactor of the `createSyndicationArticleList` function --- src/ArticleSyndication.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ArticleSyndication.php b/src/ArticleSyndication.php index 4ba3ec0..25cf5ea 100644 --- a/src/ArticleSyndication.php +++ b/src/ArticleSyndication.php @@ -144,14 +144,18 @@ public function showSyndicationFields() { * the same path. */ public function createSyndicationArticleList() { - if (preg_match('/node\/(\d+)/', $this->aliasManager->getPathByAlias($this::SYNDICATION_PATH))) { - $this->logger->warning('A syndication article list wasn’t created because a node already exists at the path ' . $this::SYNDICATION_PATH . '.'); + $pathAlias = $this::SYNDICATION_PATH; + if (preg_match('/node\/(\d+)/', $this->aliasManager->getPathByAlias($pathAlias))) { + $this->logger->warning("A syndication article list wasn’t created because a node already exists at the path alias $pathAlias."); } else { $node = Node::create([ 'type' => 'ucb_article_list', 'title' => 'Article Results', - 'path' => ['alias' => $this::SYNDICATION_PATH, 'pathauto' => PathautoState::SKIP], + 'path' => [ + 'alias' => $pathAlias, + 'pathauto' => PathautoState::SKIP, + ], 'body' => '', ]); $node->enforceIsNew()->save(); From 619e16ed1cb5c38e335d899386f056134659e368 Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Tue, 10 Dec 2024 11:03:39 -0700 Subject: [PATCH 6/6] Bumps version --- ucb_article_syndication.info.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ucb_article_syndication.info.yml b/ucb_article_syndication.info.yml index abef893..f652dec 100644 --- a/ucb_article_syndication.info.yml +++ b/ucb_article_syndication.info.yml @@ -2,7 +2,7 @@ name: CU Boulder Article Syndication description: Extends the article content type to add the taxonomies needed for article syndication. core_version_requirement: ^10 || ^11 type: module -version: '1.1' +version: '1.2' package: CU Boulder dependencies: - cu_boulder_content_types