Deprecated: Joomla\Input\Input implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in /homepages/13/d380392445/htdocs/Jlive/libraries/vendor/joomla/input/src/Input.php on line 41

Deprecated: Return type of Joomla\Input\Input::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /homepages/13/d380392445/htdocs/Jlive/libraries/vendor/joomla/input/src/Input.php on line 170

Deprecated: KunenaControllerApplicationDisplay implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in /homepages/13/d380392445/htdocs/Jlive/libraries/kunena/controller/application/display.php on line 21

Deprecated: preg_match_all(): Passing null to parameter #2 ($subject) of type string is deprecated in /homepages/13/d380392445/htdocs/Jlive/administrator/components/com_easyblog/includes/adsense/adsense.php on line 138
Creating a category on component install. - Macrotone Blogs
By Geoffrey Chapman on Wednesday, 15 October 2014
Category: Joomla Hints and Tips

Creating a category on component install.

We were looking at an easy way in which to create a default category for a Joomla component which we are developing. The problem is that a common insert directly into the table is not sufficient since the ‘#__categories’ table is actually a ‘nested’ table, and it would be necessary to rebuild the table entries after an insert anyway, otherwise the table could/would be corrupted.

We searched around for a possible solution and eventually found a method which we could adapt for our usage.  We present our resultant function below:

   1:     function createCategory( $title, $parent_id )
   2:     {
   3:        $user = JFactory::getUser();
   4:        $db   = JFactory::getDbo();
   5:   
   6:        // Check to see if the category already exists
   7:        $query   = "SELECT id from `#__categories` WHERE title ='".$title."' and extension = 'com_xxxx' ";
   8:        $db->setQuery($query);
   9:        $r_id    = $db->loadResult();
  10:   
  11:        if ( empty ($r_id) ) {
  12:            $table = JTable::getInstance('category');
  13:            $data = array();
  14:            $data['title']            = $title;
  15:            $data['parent_id']        = $parent_id;
  16:            $data['extension']        = 'com_xxxx';
  17:            $data['published']        = 1;
  18:            $data['language']         = '*';
  19:            $data['description']      = JText::_('COM_XXXX_DO_NOT_DELETE');
  20:            $data['created_user_id']  = $user->id;
  21:            $table->setLocation($data['parent_id'], 'last-child');
  22:            $table->bind($data);
  23:            if ($table->check()) {
  24:                $table->store();
  25:                $table->rebuildPath($table->id);
  26:                // echo '<p style="color: #5F9E30;">' . JText::sprintf('COM_XXXX_CREATED_CATEGORY',$title) . '</p>';
  27:                // Success
  28:            } else {
  29:                // Error
  30:                echo '<p style="color: #5F9E30;">' . JText::sprintf('COM_XXXX_ERROR_CREATING_CATEGORY',$title) . '</p>';
  31:            }
  32:         }
  33:     }

Our component we will call ‘com_xxxx’.  The above code is inserted in our install script, possibly in the ‘post install’ step, or earlier, depending upon whether we wish to use the created category for any sample data that we may include.

Lines 7 to 8 check the categories table to determine whether the record we wish to insert actually exists.  If it does then we skip over the insertion itself. 

Line 12 is where we load the Table class for the ‘#__categories’ table, and enables us to use its methods in lines 21 to 25.  Line 25 is the step where we ensure that the nested table is correctly Lines 13 to 20 exist to create the data in a form suitable to feed to the table methods.

We have chosen to display an informative message in the install output if for any reason we are unable to add the entry to the ‘#__categories’ table. 

This function has been proved to work very well for our usage, but could be easily be adapted for other purposes.

Leave Comments