Search code examples
viewjoomlamodulecomponentscode-conversion

Converting a Joomla module to view in existing component


We have a custom Joomla! component we developed to display various types of reports to our clients. In the Admin back-end, you would configure a service and a report within said Component. We then add a custom module tailored for each client which points to the report and load it into an otherwise blank page.

This works fine for having to tailor and almost entirely redesign each report based on client preferences, but we'd like to have the option of using a view without a module for anything standardized. I've gotten a start on converting one particular kind of report to it's own view and got the Administrator side set up without issue. The problem I'm running into is the front end display.

From what I gather, the front-end view should load view.html.php and metadata.xml before anything else. However, code on view.html.php doesn't seem to be executing (as tested with print statements before and after each line). Below is a sanitized version of the file and it's XML. For reference, we're running Joomla! 3.6.5.

PHP:

<?php
// No direct access to this file
defined('_JEXEC') or die;

// import Joomla view library
jimport('joomla.application.component.view');

class [ComponentName]ViewDashboard extends JViewLegacy
{       

    public function display($tpl = null)
    {
        /*[Large code block here, removed for sanitization]*/
        parent::display($tpl);
    }

}
?>

XML:

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <!-- View definition -->
    <view title="Dashboard">
        <!-- Layout options -->
        <options>
            <!-- Default layout's name -->
            <default name="Dashboard" />
        </options>
    </view>
</metadata>

Solution

  • Got it working. I had to add a model to the front end for it to display, despite other logic being present. Sanitized file is as follows, in root\components\com_ComponentName\models\modelName.php, where modelName matches the view.

    <?php
    // No direct access to this file
    defined('_JEXEC') or die('Restricted access');
    ini_set('memory_limit','1024M');
    
    // import Joomla modelitem library
    jimport('joomla.application.component.modelitem');
    
    
    class ComponentNameModelDashboard extends JModelItem
    {
    
        protected function populateState()
        {
            // Load the parameters.
            //print_r(JFactory::getApplication()->getParams());
            $this->setState('params', JFactory::getApplication()->getParams());
            parent::populateState();
        }
    
        public function getItem()
        {
            if (!isset($this->item)) 
            {
                $params    = clone $this->getState('params');
                $params->merge($this->item->params);
                $this->item->params=$params;
                $params = new JRegistry;
                $params->loadString($this->item->params,'JSON');
                $report=$params['report'];
                $db    = JFactory::getDbo();
                $query = $db->getQuery(true);
                $query->select('*')
                      ->from('#__DBTABLEHERE')
                      ->where('dashboard_name=\'' . (string)$report.'\'');
                $db->setQuery((string)$query);
            }
            return $this->item;
        }
    }