3

After i follow the Joomla "Developing an MVC Component" document until step 15- "Adding ACL" everything works. Then I tried add a new view "category", but it did not work.

Here is my code:

In admin fields folder I created a hellocategory.php most code same with helloword,just change

protected $type = 'HelloCategory';

/**
 * Method to get a list of options for a list input.
 *
 * @return  array  An array of JHtml options.
 */
protected function getOptions()
{
    $db    = JFactory::getDBO();
    $query = $db->getQuery(true);
    // custom
    $query->select('#__categories.id as id,#__categories.title as category,#__categories.extension as exten');
    $query->from('#__categories');  
    $query->where($db->quoteName('extension') . ' LIKE '. $db->quote('com_helloworld'));
    // end custom
    $db->setQuery((string) $query);
    $messages = $db->loadObjectList();
    $options  = array();
    if ($messages)
    {
        foreach ($messages as $message)
        {
            $options[] = JHtml::_('select.option', $message->category);
        }
    }

    $options = array_merge(parent::getOptions(), $options);

    return $options;

view.html.php

class HelloWorldViewCategory extends JViewLegacy
{
    /**
     * Display the Hello World view
     *
     * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
     *
     * @return  void
     */
    function display($tpl = null)
    {
        // Assign data to the view
        $category = $this->get('Item');

        // Check for errors.
        if (count($errors = $this->get('Errors')))
        {
            JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');

            return false;
        }

        // Display the view
        parent::display($tpl);
    }
}

default.xml

<layout title="Category">
        <message>category</message>
</layout>
<fields
        name="request"
        addfieldpath="/administrator/components/com_helloworld/models/fields"
        >
    <fieldset name="request">
        <field
                name="id"
                type="hellocategory"
                label="COM_UNOFD_UNOFD_FIELD_GREETING_LABEL"
                description="COM_UNOFD_UNOFD_FIELD_GREETING_DESC"
                />
    </fieldset>
</fields>

default.php

<?php var_dump($category); ?>

output is Null

The Admin Menu works,but frontend can't get any items in that category data, is there any about develop single category view document or sample online? i tried google for solution four days,but still no idea, or the only way is get data from database? Many Thanks!

var_dump($this)will output

    object(HelloWorldViewCategory)#223 (15) { ["_name":protected]=> string(8) "category" ["_models":protected]=> array(0) { } ["_basePath":protected]=> 

string(55) "/var/www/clients/client0/web11/web/components/com_helloworld" ["_defaultModel":protected]=> NULL ["_layout":protected]=> string(7) "default" ["_layoutExt":protected]=> string(3) "php" ["_layoutTemplate":protected]=> string(1) "_" ["_path":protected]=> array(2) { ["template"]=> array(2) { [0]=>

 string(79) "/var/www/clients/client0/web11/web/templates/protostar/html/com_helloworld/category/"  bla bla bla....
1

Edit: Okay, now we have the bottom of your getOptions method(), the problem's in your view's display() method. $category is only available in the local scope of that method, you need to make it a class property.

In display() change:

$category = $this->get('Item');

to

$this->category = $this->get('Item');

In default.php:

var_dump($this->category);
  • Thanks for answer,in backend menu i can select a category,name and id correct,the question is i can't get data on frontend category view,by the way i did update my chellocategory.php code few minute ago – Anthony Aug 12 '16 at 16:53
  • Your '$category' variable is currently only available in the scope of your view's display() method, which is why you can't access it from default.php. You need to set it as a class property, so you should declare it using '$this->category' instead of just '$category'. var_dump($this->category) in your default.php should at least now output something. – chrBrd Aug 12 '16 at 17:53
  • confuse now...i did change $this->category = $this->get('Item'),and now var_dump($this->category) still output NULL,i update in question when var_dump($this) the output – Anthony Aug 12 '16 at 18:42
  • Do a var_dump($this->category) right after $this->category = $this->get('Item') in display(). That'll show the result of your database query, and it's probably null. What class is your getOptions() method in, and what class is it extending? – chrBrd Aug 12 '16 at 23:06
  • i found maybe i should add a document in site/models for category,but really not sure,do you know how many files should i create them for category show? thanks – Anthony Aug 14 '16 at 8:39
0

I'd suggest you to use link to index.php?option=com_categories&extension=com_YOUREXTENSION.

And add /helpers/yourextension.php file that will generate menu by having method

public static function addSubmenu($vName)
    {
        JHtmlSidebar::addEntry(
            JText::_('COM_YOUREXTENTION_ENTRY_ONE'),
            'index.php?option=com_yourextension',
            $vName == 'entryone'
        );
        JHtmlSidebar::addEntry(
            JText::_('COM_YOUREXTENTION_ENTRY_TWO'),
            'index.php?option=com_categories&extension=com_yourextension',
            $vName == 'entrytwo'
        );
    }

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.