Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

[SW-827] Added alter hook for cardLinkEnhancer to handle the url belongs to other sites. #82

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tide_site.module
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @MdNadimHossain , I think you have to define your own alter API first, otherwise it wouldn't work, an example can be found here https://github.com/dpc-sdp/tide_core/blob/develop/modules/tide_workflow_notification/tide_workflow_notification.api.php.
correct me if I am wrong @GROwen .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant a tide_site.api.php or you can use drupal/symfony event system.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}
}
}