From 69bd6998b6926bf227d6192ac17040cb2a736833 Mon Sep 17 00:00:00 2001 From: MdNadimHossain Date: Mon, 22 Mar 2021 11:13:44 +1100 Subject: [PATCH 1/3] [SW-827] Added alter hook for cardLinkEnhancer to handle the url belongs to other sites. --- tide_site.module | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tide_site.module b/tide_site.module index eb9d4ed..0056e9a 100644 --- a/tide_site.module +++ b/tide_site.module @@ -800,3 +800,49 @@ function tide_site_share_link_token_view_alter(array &$build, EntityInterface $t } } } + +/** + * Implements hook_tide_card_link_enhancer_undo_transform_alter(). + * + * @see Drupal\tide_landing_page\Plugin\jsonapi\FieldEnhancer\CardLinkEnhancer::doUndoTransform() + */ +function tide_site_tide_card_link_enhancer_undo_transform_alter(&$data, &$context) { + if (empty($data['pid']) || empty($data['url'])) { + return; + } + + /** @var \Drupal\tide_site\TideSiteHelper $helper */ + $helper = \Drupal::service('tide_site.helper'); + /** @var \Drupal\tide_site\AliasStorageHelper $alias_helper */ + $alias_helper = \Drupal::service('tide_site.alias_storage_helper'); + + $data['origin_alias'] = $data['alias']; + $data['alias'] = $alias_helper->getPathAliasWithoutSitePrefix($data); + + /** @var \Drupal\path_alias\Entity\PathAlias $path_entity */ + $path_entity = PathAlias::load($data['pid']); + if ($path_entity) { + $node = $alias_helper->getNodeFromPathEntity($path_entity); + if ($node) { + // Get the Site ID parameter. + $request = \Drupal::request(); + $site_id = $request->get('site'); + if (!$site_id) { + return; + } + + // The URL from Tide API is already relative. + $data['origin_url'] = $data['url']; + // Check if the link belongs to the current site. + if ($helper->isEntityBelongToSite($node, $site_id)) { + $site_url = ''; + } + else { + $site_url = $helper->getEntityPrimarySiteBaseUrl($node); + $data['url'] = $helper->getNodeUrlFromPrimarySite($node); + } + // Remove site prefix from the URL. + $data['url'] = $alias_helper->getPathAliasWithoutSitePrefix(['alias' => $data['url']], $site_url); + } + } +} From 819be59c6ef9f18c795d49b1880cc68710c577ec Mon Sep 17 00:00:00 2001 From: MdNadimHossain Date: Mon, 22 Mar 2021 12:05:20 +1100 Subject: [PATCH 2/3] [test-branch] Added changes for handling different smei sites links. --- src/TideSiteHelper.php | 39 +++++++++++++++++++++++++++++++++++++++ tide_site.module | 33 +-------------------------------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/TideSiteHelper.php b/src/TideSiteHelper.php index 1251fab..ac9b34b 100644 --- a/src/TideSiteHelper.php +++ b/src/TideSiteHelper.php @@ -10,6 +10,7 @@ use Drupal\node\NodeInterface; use Drupal\taxonomy\TermInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; +use Shaper\Util\Context; /** * Class TideSiteHelper. @@ -708,4 +709,42 @@ public function getRouteCacheId($path, $site_id) { return $cid; } + /** + * Function to handle URL that belongs to other sites. + */ + public function handleApiUrl($data, Context $context) { + /** @var \Drupal\tide_site\AliasStorageHelper $alias_helper */ + $alias_helper = \Drupal::service('tide_site.alias_storage_helper'); + + $data['origin_alias'] = $data['alias']; + $data['alias'] = $alias_helper->getPathAliasWithoutSitePrefix($data); + + /** @var \Drupal\path_alias\Entity\PathAlias $path_entity */ + $path_entity = PathAlias::load($data['pid']); + if ($path_entity) { + $node = $alias_helper->getNodeFromPathEntity($path_entity); + if ($node) { + // Get the Site ID parameter. + $request = \Drupal::request(); + $site_id = $request->get('site'); + if (!$site_id) { + return; + } + + // The URL from Tide API is already relative. + $data['origin_url'] = $data['url']; + // Check if the link belongs to the current site. + if ($this->isEntityBelongToSite($node, $site_id)) { + $site_url = ''; + } + else { + $site_url = $this->getEntityPrimarySiteBaseUrl($node); + $data['url'] = $this->getNodeUrlFromPrimarySite($node); + } + // Remove site prefix from the URL. + $data['url'] = $alias_helper->getPathAliasWithoutSitePrefix(['alias' => $data['url']], $site_url); + } + } + } + } diff --git a/tide_site.module b/tide_site.module index 0056e9a..3dca992 100644 --- a/tide_site.module +++ b/tide_site.module @@ -813,36 +813,5 @@ function tide_site_tide_card_link_enhancer_undo_transform_alter(&$data, &$contex /** @var \Drupal\tide_site\TideSiteHelper $helper */ $helper = \Drupal::service('tide_site.helper'); - /** @var \Drupal\tide_site\AliasStorageHelper $alias_helper */ - $alias_helper = \Drupal::service('tide_site.alias_storage_helper'); - - $data['origin_alias'] = $data['alias']; - $data['alias'] = $alias_helper->getPathAliasWithoutSitePrefix($data); - - /** @var \Drupal\path_alias\Entity\PathAlias $path_entity */ - $path_entity = PathAlias::load($data['pid']); - if ($path_entity) { - $node = $alias_helper->getNodeFromPathEntity($path_entity); - if ($node) { - // Get the Site ID parameter. - $request = \Drupal::request(); - $site_id = $request->get('site'); - if (!$site_id) { - return; - } - - // The URL from Tide API is already relative. - $data['origin_url'] = $data['url']; - // Check if the link belongs to the current site. - if ($helper->isEntityBelongToSite($node, $site_id)) { - $site_url = ''; - } - else { - $site_url = $helper->getEntityPrimarySiteBaseUrl($node); - $data['url'] = $helper->getNodeUrlFromPrimarySite($node); - } - // Remove site prefix from the URL. - $data['url'] = $alias_helper->getPathAliasWithoutSitePrefix(['alias' => $data['url']], $site_url); - } - } + $helper->handleApiUrl($data, $context); } From 1a3241b246bfb2b72d8051c2fceca9d1974c42c9 Mon Sep 17 00:00:00 2001 From: MdNadimHossain Date: Mon, 22 Mar 2021 12:29:46 +1100 Subject: [PATCH 3/3] [test-branch] Updated lint fix. --- src/TideSiteHelper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/TideSiteHelper.php b/src/TideSiteHelper.php index ac9b34b..6a82f60 100644 --- a/src/TideSiteHelper.php +++ b/src/TideSiteHelper.php @@ -715,10 +715,10 @@ public function getRouteCacheId($path, $site_id) { public function handleApiUrl($data, Context $context) { /** @var \Drupal\tide_site\AliasStorageHelper $alias_helper */ $alias_helper = \Drupal::service('tide_site.alias_storage_helper'); - + $data['origin_alias'] = $data['alias']; $data['alias'] = $alias_helper->getPathAliasWithoutSitePrefix($data); - + /** @var \Drupal\path_alias\Entity\PathAlias $path_entity */ $path_entity = PathAlias::load($data['pid']); if ($path_entity) { @@ -730,7 +730,7 @@ public function handleApiUrl($data, Context $context) { if (!$site_id) { return; } - + // The URL from Tide API is already relative. $data['origin_url'] = $data['url']; // Check if the link belongs to the current site.