How To Link different Views Together in Joomla

As you add views to your component, you'll frequently also want to link those views to each other, so that visitors can navigate around your component seamlessly. Let's add some of these links now to help out our views.

List of Malls

So if we go to the Malls view right now, you'll notice that we have a list of all the malls that are available, but they're not linked.

So let's fix that by adding links to these malls.

Open malls file for editing

First, what we need to do is go to the model in our site component com_noidacity which have created in earlier tutorials and open up the malls.php file. We need to update this model because at the moment, we're not getting the mall ID and the mall name, so add the following code to do that.

  1. <?php
  2. defined( '_JEXEC' ) or die;
  3.  
  4. jimport('joomla.application.component.modellist');
  5.  
  6. class NoidaCityModelMalls extends JModelList
  7. {
  8. public function getListQuery()
  9. {
  10. $query = parent::getListQuery();
  11.  
  12. $query->select('mall_id, mall_name, mall_teaser');
  13. $query->from('#__noidacity_malls')
  14. ->where('published = 1');
  15.  
  16. return $query;
  17. }
  18. }

So we updating this function, we've added few columns to the query and that way we can get the mall_id as well as the mall_teaser. Now that we've updated the model, let's go back and update the view.

Editing view html file

So let's go to the Malls view. Another thing that we want to do is we want to have the header directly in the layout file, and not assign it in view.html.php. So to do that, go to Malls and go to view.html.php and just remove the header assignment and also remove the header property.

Code for view.html.php currently looks like this:

  1. <?php
  2. defined( '_JEXEC' ) or die;
  3.  
  4. jimport('joomla.application.component.view');
  5.  
  6. class NoidaCityViewMalls extends JView
  7. {
  8. protected $header;
  9. protected $items;
  10.  
  11. public function display($tpl = null){
  12. $this->header = 'Noida City Malls';
  13. $this->items = $this->get('Items');
  14.  
  15. parent::display($tpl);
  16. }
  17. }

After removing the header property and header assignment the code will look like this

  1. <?php
  2. defined( '_JEXEC' ) or die;
  3.  
  4. jimport('joomla.application.component.view');
  5.  
  6. class NoidaCityViewMalls extends JView
  7. {
  8. protected $items;
  9.  
  10. public function display($tpl = null){
  11. $this->items = $this->get('Items');
  12.  
  13. parent::display($tpl);
  14. }
  15. }

Editing Layout file

Finally, let's update the layout. Go to the tmpl folder in your malls view, and open up default.php and replace the previous code with this code

  1. <?php defined( '_JEXEC' ) or die; ?>
  2. <h1><?php echo JTEXT::_('COM_NOIDACITY_MALLS'); ?></h1>
  3.  
  4. <?php foreach($this->items as $item): ?>
  5. <p>
  6. <?php $url = 'index.php?option=com_noidacity&view=mall&id='.$item->mall_id; ?>
  7. <a href="<?php echo JRoute::_($url); ?>"><?php echo $this->escape($item->mall_name) ?></a>
  8. - <?php echo $this->escape($item->mall_teaser); ?>
  9. </p>
  10.  
  11. <?php endforeach; ?>

So you'll notice now the header is being translated from the JText function and we're also cycling over all the items as we did before. But now, we're building links for every item and outputting that so that we can navigate to those malls.

Malls showing in frontend joomla

So if we go to our frontend and hit Refresh, you'll notice now we're not only have links to the malls, we also have the short description for each mall, and if we click on the link, it goes to the mall.

Single Mall View Joomla

Now, one thing you'll notice on the mall is that as you go to the bottom, there is no link back to the list of all the malls. So let's fix that as well.

Editing Mall for the links

Let's go back to the Mall view and in the tmpl folder there is the default layout.So open up the default.php file and add the following code at the bottom

  1. <div id="mall_footer">
  2. <?php $url = 'index.php?option=com_noidacity&view=malls'; ?>
  3. <a href="<?php echo JRoute::_($url); ?>"><?php echo JText::_('COM_NOIDACITY_MALLS_RETURN'); ?></a>
  4. </div>

So the end result of default.php for mall view will look like this

  1. <?php defined( '_JEXEC' ) or die; ?>
  2. <h1><?php echo $this->escape($this->item->mall_name); ?></h1>
  3. <h2><?php echo "Place name near this mall: ". $this->escape($this->item->place_name); ?></h2>
  4.  
  5. <div id="mall">
  6. <div id="mall_left">
  7. <p id="mall_description">
  8. <?php echo $this->item->mall_description; ?>
  9. </p>
  10.  
  11. <h2><?php echo JTEXT::_('COM_NOIDACITY_MALLS_DETAILS'); ?></h2>
  12.  
  13. <dl>
  14.  
  15. <dt><?php echo JTEXT::_('COM_NOIDACITY_MALL_NAME'); ?></dt>
  16. <dd><?php echo $this->escape($this->item->mall_name); ?></dd>
  17. <dt><?php echo JTEXT::_('COM_NOIDACITY_MALLS_LOCATION'); ?></dt>
  18. <dd><?php echo $this->escape($this->item->mall_location); ?></dd>
  19.  
  20. </dl>
  21. </div>
  22. <div id="mall_footer">
  23. <?php $url = 'index.php?option=com_noidacity&view=malls'; ?>
  24. <a href="<?php echo JRoute::_($url); ?>"><?php echo JText::_('COM_NOIDACITY_MALLS_RETURN'); ?></a>
  25. </div>
  26. </div>

Link Back tour mall

So when we save that file and we hit Refresh and go to the bottom of the screen, we now have a link that goes right back to the Malls and it brings us right back here where we can go visit another mall. So now it's possible for us to navigate from a list of malls to a single mall and then navigate right back to the list. Adding these links are making this component come together, and it's going to make it easier for visitors to use it.