1

I need to display links of a custom component on breadcrumbs. How can i get it done?

Example my component (site part) has the following url:

site.com/gallery/animals/ where animals is the URL of the component and gallery is the menu item alias

My breadcrumb shows:

home/gallery instead of home/gallery/animals

UPDATE

The "problem" lies in modules/mod_breadcrumbs/helper specifically in the getPathWay() method; It returns the path but not including the url of the component. The print_r function gave me:

Joomla\CMS\Pathway\SitePathway Object ( [_pathway:protected] => Array ( [0] => stdClass Object ( [name] => Gallery [link] => index.php?option=com_galley&view=gallery&Itemid=361 ) ) [_count:protected] => 1 ) where index.php?option=com_galley&view=gallery&Itemid=361 is the url of the menu item; it should have given me (i expected it at least) something like this:

Joomla\CMS\Pathway\SitePathway Object ( [_pathway:protected] => Array ( [0] => stdClass Object ( [name] => Gallery [link] => index.php?option=com_galley&view=galler&Itemid=361 ) [link] => &view=gallery&Itemid=91 ) ) [_count:protected] => 1 ) where &view=gallery&Itemid=91 would be my component url.

Is there any way to add the url of custom component to the getPathWay() method?

UPDATE

I found a method to add an item to the pathway; the method is addItem(string $name, string $link)

Something like this:

$app= JFactory::()->getApplication();
$pathway=$app->getPathWay();
$pathway->addItem('Animals' , 'animals');

it works but now i need a way to automatically get it from the URL and add it to the method addItem()

  • I was actually realising that the URL of a custom component (site part) didn't appear on breadcrumbs. I will probably have to override the breadcrumbs and make some modifications. I haven't tried anything yet. – Thiago Guimarães Apr 8 at 22:57
0

The solution is to add a function to your view.html.php file of your component

The function will:

  1. Get url parameters

  2. Retrieve the name/title of the current page from the database and add a new item to the path with the addItem() method.

The function:

        /* 
        Function to add a new item to the breadcrumbs.
        The function name can be whatever you want
        */
        function addNewItemBreadcrumb(){

           //it will initialize the variable input in order to get the url parameters
           $input= JFactory::getApplication()->input;

           /* get the id parameter from the url, even if your url is a friendly url. Example: 
           site.com/index.php?com_yourcomponent?&view=article&id=37&Itemid=103 or site.com/article the below code will get the id
           parameter which is 37 */ 

          //get the id of the page. We will use this to retrieve the name of the article from the database

           $id=$input->get('id');

           //here we will retrieve the name of the article from the database

           $db=JFactory::getDbo();
           $q=$db->getQuery(true);
           $q->select('name_of_the_column_you_want_to_display');
           $q->from('#__yourcomponent_yourtable')
           $q->where('id='.$db->quote($id));
           $name=$db->setQuery(true);
           $name=$db->loadResult();


          //add a new item to the breadcrumbs

          $app=JFactory::getApplication();
          $pathway=$app->getPathWay();

          //this will add a new breadcrumb item with the name of the article we got from the database

          return $pathway->addItem($name);  

         /* if you also want to add a link you can do the following:
         return $pathway->addItem($name, JRoute::_('index.php?option=your_component&view=your_view&id='.$id));  
          */


 }

Now just call the function in your view.html.php

$this->addNewItemBreadcrumb();
| improve this answer | |
  • 1
    You shouldn't modify mod_breadcrumbs. You can add pathway items in your component's views. – Sharky Apr 9 at 15:13
  • Yes i have tried and it's worked out. Thank you. – Thiago Guimarães Apr 9 at 15:25
  • @Sharky your solution is even better because we no longer need to verify the component's name nor the view's name. – Thiago Guimarães Apr 9 at 16:36
  • 1
    You also probably don't need to make database queries. Normally the item should already be available at this point. – Sharky Apr 10 at 16:19
  • @Sharky indeed it works, however I tried adding a new pathitem to the setDocument function and it displayed like two empty items. – Thiago Guimarães Apr 11 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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