0

I have the following problem:

I set a URL with JRoute like this:

 JRoute::_('index.php?option=com_gallery&view=gallery');

This gives me example.site/gallery, which is correct, however, if I want to display this same link, in a different view - example - view finishing, it does not show this same link, but it shows the link of the view "finishing" instead.

So, in short, if I'm displaying this JRoute::_('index.php?option=com_gallery&view=gallery') in the view "finishing", instead of displaying example.site/gallery, it displays example.site/finishing

Actually it happens with any link; if I do this: echo JRoute::_('index.php') it gives me example.site/finishing

UPDATE:

My Router File:

<?php


defined('_JEXEC') or die;

class GalleryRouter implements JComponentRouterInterface
{

    public function build(&$query)
    {
        $segments = array();
        if (isset($query['id']))
        {
            $db = JFactory::getDbo();
            $qry = $db->getQuery(true);
            $qry->select('alias');
            $qry->from('#__gallery_images');
            $qry->where('id = ' . $db->quote($query['id']));
            $db->setQuery($qry);
            $alias = $db->loadResult();
            $segments[] = $alias;
            unset($query['id']);
        }
        unset($query['view']);
        return $segments;
    }

    public function parse(&$segments)
    {
        $vars = array();

        $db = JFactory::getDbo();
        $qry = $db->getQuery(true);
        $qry->select('id');
        $qry->from('#__gallery_images');
        $qry->where('alias = ' . $db->quote($segments[0]));
        $db->setQuery($qry);
        $id = $db->loadResult();

        if(!empty($id))
        {
            $vars['id'] = $id;
            $vars['view'] = 'gallery';

        }

        return $vars;
    }

    public function preprocess($query)
    {
        return $query;
    }
}

Hidden menu links:

index.php?option=com_gallery&view=gallery which gives me: example.site/gallery

index.php?option=com_gallery&view=finising which gives me example.site/finishing

If I do an echo JRoute::_('index.php?option=com_gallery&view=gallery') in the view "finishing", instead of giving me example.site/gallery it displays example.site/finishing.

I'm not sure how JRoute works but as far as I know, it should display example.site/gallery regardless of the view it is being dispayed on, but pelase, correct if I am wrong.

2
  • 1
    Thiago could you add your router.php file to the question, and also the Link and alias in any menuitem (including hidden menuitems) which point to your com_gallery component. Thanks. Apr 27, 2020 at 22:14
  • Yes, these are hiddden menu items. Apr 28, 2020 at 10:59

1 Answer 1

0

You can use your custom router code to find a menuitem which matches the view you enter in your JRoute::_() call, and I think it's easiest to do this in the preprocess method.

Add this code into your router.php preprocess method:

if (isset($query['view'], $query['Itemid']))
{
    $sitemenu = JFactory::getApplication()->getMenu();
    $targetView = $query['view'];
    $currentMenuitem = $sitemenu->getItem($query['Itemid']);
    if (strpos($currentMenuitem->link, "view=$targetView") === FALSE)  // if view doesn't match
    {
        $galleryMenuitems = $sitemenu->getItems(array("component"), array("com_gallery"));
        foreach($galleryMenuitems as $menuitem)
        {
            if (strpos($menuitem->link, "view=$targetView") !== FALSE)  // if this menuitem's view matches
            {
                $query['Itemid'] = $menuitem->id;
                break;
            }
        }
    }
}  

$query['view'] is the view you asked for in your JRoute call.

$query['Itemid'] is the Itemid associated with the current menuitem that the Joomla router is using – which is (probably) going to be the menuitem the user is on.

The code finds the link associated with the current menuitem, and if it doesn't match the view you're looking for then it gets all your component's menuitems and searches through them to find a link which includes your view.

Alternatively if all your component views are named the same as the corresponding menuitem alias, then you can easily find that using

$menuitems = $sitemenu->getItems(array("component","alias"),array("com_gallery",$targetView), true);
if ($menuitems)
{
    $query['Itemid'] = $menuitems[0]->id;
}

A guide to the Joomla menu/menuitems API is at https://docs.joomla.org/Menu_and_Menuitems_API_Guide

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.