News:

Dear jDownloads users, There is currently no Joomla 6 compatible version available. However, we are working on it and hope to release it shortly.

Main Menu
Support-Forum

4SEF Hook for Jdownloads URLs

Started by Zoom, 15.05.2024 21:26:11

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zoom

Hi,

I'm trying to upgrade from Joomla 3 to Joomla 4.

The main issue I've had is that I was using JoomSEF before, and it does not support Joomla 4.

So I'm using 4SEF instead, but it does not support jdownloads in the same way as JoomSEF could.

I reached out to the developer of 4SEF, and they created a hook that would filter the URL to the desired effect before it was registered. This stripped out the /download, category ID, and download ID from the URL. My designed SEF URL is: categoryname/downloadname.

This works great when the URL is from something in Jdownloads, but it does not work from things like tag pages.

That's because the non-SEF URL for tags does not include the category ID of Jdownloads and, therefore, can't be added to the URL.

I contacted the SEF developer again, who said they could not fix it. To quote them:

QuoteHowever, 4SEF is not going to be able to ADD the category. This would require knowing how JDownloads stores categories and being able to identify the category an item belongs to (plus read that category title from the database).

I'd suggest talking to the JDownloads developer here: if you provide them this code, they should be able to modify it to provide the category where it's missing.

Also, they should be able to add the category in the non-sef URL, which may solve the issue from the start.

So, the question is, do you think you can fix this code to add the category ID of Jdownloads for the URL?

I'm desperate for this to work, and will gladly compensate for any time spent on it!

Here's the hook code:

<?php
/**
* 4SEF hooks file
*
* You can use 2 variables to access 4SEO content:
*
* $factory: access variables
* $hooks: add handlers
*/

use Weeblr\Wblib\Forsef\Wb;

// no direct access
defined('WBLIB_EXEC') || die;

/**
* Filter the built URL pair (sef + nonSef) object just before it's stored to the database. Pagination and suffix has been attached
* but dynamic variables are not present as they are not stored to DB. This currently includes:
*
* - /feed/rss and /feed/atom suffixes
* - print suffix
*
* @version 1.0.1
* @date    2023-11-08 16:43:24
*
* @api     forsef
* @package 4SEF\filter\build
* @var forsef_before_store_url
* @since   1.0.0
*
* @param Data\Urlpair $urlPair
* @param Uri\Uri      $uriToBuild
* @param Uri\Uri      $platformUri
*
* @return Data\Urlpair $urlPair
*
*/

function wblTranformSefUrlPath($originalPath, $catid, $id)
{
   
// separate into segments at each / sign
   
$urlSegments = explode(
       
'/',
       
$originalPath
   
);

   
// drop the leading segment, the menu item: download
   
array_shift($urlSegments);

   
// put back together the remaining segment
   
$modifiedPath = implode('/', $urlSegments);

   
// remove the category id from the beginning of the string
   
$modifiedPath = Wb\lTrim(
       
$modifiedPath,
       
$catid . '-'
   
);

   
// remove the download id from the beginning of the string
   
if ($id)
   {
       
$modifiedPath = str_replace(
           
'/' . $id . '-',
           
'/',
           
$modifiedPath
       
);
   }

   return
$modifiedPath;
}

$hooks->add(
   
'forsef_before_store_url',
   function (
$urlPair, $uriToBuild, $platformUri) {

       
$option = $uriToBuild->getVar('option');
       
$view   = $uriToBuild->getVar('view');
       
$id     = (int)$uriToBuild->getVar('id');
       
$catid  = (int)$uriToBuild->getVar('catid');
       if (
'com_jdownloads' !== $option)
       {
           return
$urlPair;
       }

       if (!
in_array(
           
$view,
           [
               
'download',
               
'category'
           
]
       )
       )
       {
           return
$urlPair;
       }

       
// the SEF as built by 4SEF: download/category/download-name
       
$modifiedSef  = wblTranformSefUrlPath($urlPair->get('sef'), $catid, $id);
       
$modifiedPath = wblTranformSefUrlPath($urlPair->get('base_path'), $catid, $id);

       
// set the modified SEF into the URL pair
       
$urlPair->set(
           
'sef',
           
$modifiedSef
       
);
       
$urlPair->set(
           
'base_path',
           
$modifiedPath
       
);

       return
$urlPair;
   }
);

  •  

Arno

Hallo,
unfortunately, I have very little time for jDownloads support at the moment. Therefore, I cannot help you here in the short term. Sorry. Perhaps at a later date.

In general, however, you should consider whether you want to be dependent on other third-party components in the future. As it will happen again and again that such extensions will be discontinued due to the necessary customisation work with Joomla.
Best Regards / Gruß
Arno
Please make a Donation for jDownloads and/or write a review on the Joomla! Extensions directory!
  •  

ColinM

Hi
Using SEF in Downloads was supported in jD3.9 and is planned for jD 4 but because of the numerous changes in Joomla! 4 and 5 over a short time and the need to fix bugs this work has not yet been done.

Colin
Colin M
  •