2

I develop a component that has a list view: index.php?option=com_mycomponent&view=listview&catid=1

I have published several menu elements of this component pointing to different categories;

  • cat1
  • cat2
  • cat3

As they are menú items, the SEF URL are:

  • www.myweb.com/cat1

  • www.myweb.com/cat2

  • www.myweb.com/cat3

So far so good, normal behaviour.

Also, I got a cookie with a location text. My customer want the URL to be diferent according to the value of the cookie.

If the cookie value is "madrid" the urls should be

  • www.myweb.com/madrid/cat1

  • www.myweb.com/madrid/cat2

  • www.myweb.com/madrid/cat3

If the cookie value is "barcelona" then, they should be

  • www.myweb.com/barcelona/cat1

  • www.myweb.com/barcelona/cat2

  • www.myweb.com/barcelona/cat3

and so on.

Beside that, there is nothing, the lists are exactly the same in every case is just a mather of changing the URL.

Any ideas how I can achieve this?

3
  • I can only point you in the right direction, I think - check languagefilter system plugin, you'll see how they manipulate url to add language prefix to links, you could use the same method to add location prefix
    – Marko D
    Oct 13, 2015 at 15:45
  • It is really a good advise. The plugin seems to do something really similar. Even the cookie part. Thanks I will try to follow but it is very complex. Oct 13, 2015 at 15:57
  • I don't know if it deserves an answer since there is no code, but I posted an answer in case some people skip through the comments... I am doing something similar now, and indeed it's complex
    – Marko D
    Oct 13, 2015 at 16:09

1 Answer 1

2

Check Language Filter system plugin, you'll see how they manipulate the url to add language prefix to links. You could use the same method to add location prefix based on cookie value.

Here is what I did, but bear in mind that I had a tricky request, I had to make it work together with language filter plugin AND make the country appear before the language code. Different countries can show the same item and languages are shared accros countries. It works in pair with a custom country extra field plugin for K2, but it's irrelevant for this. What I did is possible only from Joomla 3.4 and higher

defined('_JEXEC') or die;

use Joomla\Registry\Registry;

class PlgSystemCountryFilter extends JPlugin
{
   protected $app;

   public $countries = array('austria', 'belgium', 'czech-republic', 'poland', 'slovakia', 'ukraine');
   public $country;

   public function __construct(&$subject, $config)
   {
      parent::__construct($subject, $config);
      $this->app = JFactory::getApplication();
   }

   public function onAfterInitialise()
   {
      if ($this->app->isSite())
      {
         // country/fr/menus
         $path = str_replace(JUri::root(), '', JUri::current());

         $parts = explode('/', $path);
         if(!empty($parts[0]) && in_array($parts[0], $this->countries)) {
            $this->country = $parts[0];
            $router = $this->app->getRouter();

            $router->attachBuildRule(array($this, 'buildRule'), JRouter::PROCESS_BEFORE);
            $router->attachBuildRule(array($this, 'postprocessSEFBuildRule'), JRouter::PROCESS_AFTER);
            $router->attachParseRule(array($this, 'parseRule'), JRouter::PROCESS_BEFORE);
         }
      }
   }

   public function buildRule(&$router, &$uri)
   {
      if(!empty($this->country)) {
         $parts = explode('/', $uri->getPath());
         $lang = array_shift($parts);
         $uri->setPath(implode('/', $parts) . '/' . $this->country . '/' . $lang . '/');
      }
   }

   public function postprocessSEFBuildRule(&$router, &$uri)
   {    
      $uri->delVar('sitecountry');
   }

   public function parseRule(&$router, &$uri)
   {
      $path = $uri->getPath();
      $parts = explode('/', $path);
      if(!empty($parts[0]) && in_array($parts[0], $this->countries)) {
         $country = $parts[0];
      }

      $array = array();
      if(!empty($country)) {
         array_shift($parts);
         // if we are supposed to be on the root page /fr, then make it be like /fr/country so that per country homepage can be displayed
         // for this to work hidden menu with aliases /country must be created
         if(count($parts) == 1) {
            $parts[] = $country;
         }
         $uri->setPath(implode('/', $parts));

         $this->app->input->set('sitecountry', $country);
         $array = array('sitecountry' => $country);
      }
      return $array;
   }

}
3
  • Well, thanks again for the idea. I duplicated the plugin and start editing here and there, kwoing very few what I was doing but at the end I made it!! Oct 13, 2015 at 21:42
  • @PieroMarsilio, if you have time to post your solution here I'm sure other JSE users would find it useful :)
    – johanpw
    Oct 14, 2015 at 4:44
  • @johanpw I added code that I made, since what I am doing is similar. Still, it should be considered work in progress, for example I should throw 404 if someone tries to access url without country code
    – Marko D
    Oct 14, 2015 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.