Actions

J2.5 talk

Difference between revisions of "Developing a MVC Component/Adding categories"

From Joomla! Documentation

m (Added link for ACL issues with category menu)
 
(7 intermediate revisions by 4 users not shown)
Line 16: Line 16:
  
 
People who are using Joomla >= 2.5.5. will probably encounter a problem with categories menu - buttons for basic user actions missing (such as "New", "Edit" etc.) The solution can be found here: http://forum.joomla.org/viewtopic.php?f=642&t=730171
 
People who are using Joomla >= 2.5.5. will probably encounter a problem with categories menu - buttons for basic user actions missing (such as "New", "Edit" etc.) The solution can be found here: http://forum.joomla.org/viewtopic.php?f=642&t=730171
 +
 +
== Additional explanation about the controller helloworld.php file changes would be helpful. ==
 +
 +
The controller helloworld.php file changes from
 +
 +
<blockquote>// set default view if not set<br/>
 +
            JRequest::setVar('view', JRequest::getCmd('view', 'HelloWorlds'));</blockquote>
 +
 +
To
 +
 +
<blockquote>// set default view if not set<br/>
 +
            $input = JFactory::getApplication()->input;<br/>
 +
            $input->set('view', $input->getCmd('view', 'HelloWorlds'));</blockquote>
 +
 +
without any explanation as to the changes.
 +
 +
 +
*** JRequest is depricated and JInput is to be used
 +
 +
== The helper filename is important ==
 +
 +
I think this is the first place the helper file was discussed, and I found out the hard way that the file name needs to be the component name (in lower case I think). HelloWorldHelper.php (named after the class) seemed to work in every respect except for maintaining the submenu when on the categories submenu item. It has to be helloworld.php
 +
 +
== Hiding the submenu on edit forms. ==
 +
 +
I would suggest adding the code to hide the submenu on the edit form. If the user has the submenu items on the list view and goes to an edit form, the submenu is really not valuable and could cause the user to lose changes with an accidental click. The code could be done like this to only show the submenu on the list views.
 +
 +
<pre>
 +
public static function addSubmenu($submenu)
 +
{
 +
 +
  $subMenuViews = array("messages","categories");
 +
  // Prevent the menus from displaying on edit form views like message and category
 +
  if (in_array($submenu,$subMenuViews))
 +
  {
 +
 +
    JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
 +
          'index.php?option=com_helloworld', $submenu == 'messages');
 +
    JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),
 +
          'index.php?option=com_categories&view=categories&extension=com_helloworld',
 +
          $submenu == 'categories');
 +
 +
  }
 +
 +
  // set some global property
 +
  $document = JFactory::getDocument();
 +
  $document->addStyleDeclaration('.icon-48-helloworld ' .
 +
  '{background-image: url(../media/com_helloworld/images/tux-48x48.png);}');
 +
  if ($submenu == 'categories')
 +
  {
 +
    $document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION_CATEGORIES'));
 +
  }
 +
}</pre>
 +
 +
== Difference to the attached zip-File ==
 +
 +
In the zip File, there's a difference to the tutorial.
 +
If the zip File is installed, there's a Triangle left to the Admin-Menu "Helloworld" and there's a kind of "popup"-Submenu.
 +
 +
The Difference is in the helloworld.xml install file:
 +
<source lang='xml'>
 +
<submenu>
 +
<menu view="helloworlds">COM_HELLOWORLD_SUBMENU_MESSAGES</menu>
 +
<menu link="option=com_categories&amp;view=categories&amp;extension=com_helloworld">COM_HELLOWORLD_SUBMENU_CATEGORIES</menu>
 +
</submenu>
 +
</source>
 +
Is there a reason, why it's not in the tutorial?

Latest revision as of 14:46, 3 May 2013

Why $query = new JDatabaseQuery; but not $query = &new JDatabaseQuery; ?

In ../models/fields/helloworld.php on line 29

If you use the line, $query = new JDatabaseQuery;, component works, but when helloworlds is added as menu item it causes the following error when item is clicked:

Fatal error: Cannot instantiate abstract class JDatabaseQuery in C:\xampp\htdocs\test_site\administrator\components\com_helloworld\models\fields\helloworld.php on line 29

Reverting back to code from part 11, $query = $db->getQuery(true);, does not throw error.

Just an observation.


---

People who are using Joomla >= 2.5.5. will probably encounter a problem with categories menu - buttons for basic user actions missing (such as "New", "Edit" etc.) The solution can be found here: http://forum.joomla.org/viewtopic.php?f=642&t=730171

Contents

Additional explanation about the controller helloworld.php file changes would be helpful.

The controller helloworld.php file changes from

// set default view if not set
JRequest::setVar('view', JRequest::getCmd('view', 'HelloWorlds'));

To

// set default view if not set
$input = JFactory::getApplication()->input;
$input->set('view', $input->getCmd('view', 'HelloWorlds'));

without any explanation as to the changes.


      • JRequest is depricated and JInput is to be used

The helper filename is important

I think this is the first place the helper file was discussed, and I found out the hard way that the file name needs to be the component name (in lower case I think). HelloWorldHelper.php (named after the class) seemed to work in every respect except for maintaining the submenu when on the categories submenu item. It has to be helloworld.php

Hiding the submenu on edit forms.

I would suggest adding the code to hide the submenu on the edit form. If the user has the submenu items on the list view and goes to an edit form, the submenu is really not valuable and could cause the user to lose changes with an accidental click. The code could be done like this to only show the submenu on the list views.

public static function addSubmenu($submenu) 
{

  $subMenuViews = array("messages","categories");
  // Prevent the menus from displaying on edit form views like message and category
  if (in_array($submenu,$subMenuViews))
  {

    JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
           'index.php?option=com_helloworld', $submenu == 'messages');
    JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),
           'index.php?option=com_categories&view=categories&extension=com_helloworld',
           $submenu == 'categories');

  }

  // set some global property
  $document = JFactory::getDocument();
  $document->addStyleDeclaration('.icon-48-helloworld ' .
  '{background-image: url(../media/com_helloworld/images/tux-48x48.png);}');
  if ($submenu == 'categories') 
  {
     $document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION_CATEGORIES'));
  }
}

Difference to the attached zip-File

In the zip File, there's a difference to the tutorial. If the zip File is installed, there's a Triangle left to the Admin-Menu "Helloworld" and there's a kind of "popup"-Submenu.

The Difference is in the helloworld.xml install file:

                <submenu>
                        <menu view="helloworlds">COM_HELLOWORLD_SUBMENU_MESSAGES</menu>
                        <menu link="option=com_categories&amp;view=categories&amp;extension=com_helloworld">COM_HELLOWORLD_SUBMENU_CATEGORIES</menu>
                </submenu>

Is there a reason, why it's not in the tutorial?