Page 1 of 1

Create a category during a component installation ?

Posted: Sat Aug 06, 2011 2:47 pm
by duddy67
Hi,

I'm wondering if it's possible to create one category during my component installation.
It would be nice that my component has already his own category and be ready to use.

But the thing is that when a component category is created Joomla automaticaly add a new entry
in the __assets table wich looks pretty tricky to do manually.
I put some MySQL queries in the script.php and I made few trials but it doesn't work.
The creation of the category in the __categories table is ok, but the insert in the __assets
table fails. Joomla makes an insert for the new component and that's all, my query is ignored. ???

Is there any example somewhere, can someone helps me ?


Thanks.

Re: Create a category during a component installation ?

Posted: Mon Jan 16, 2012 9:50 pm
by Wootah
Hi did you get any code to do this?

Re: Create a category during a component installation ?

Posted: Mon Jan 16, 2012 11:20 pm
by fmmarzoa
sh404 extension creates one. Perhaps installing it and examining its code may help you.

Re: Create a category during a component installation ?

Posted: Thu Feb 09, 2012 10:16 am
by Biomehaniker
This is currently quite easy (I´m using the sh404sef extension way).

Here is a "snipplet" which you can use. This must be added to the script which is triggered during the component installation (in the helloworld.xml) via

Code: Select all

  <scriptfile>script.php</scriptfile>
add the following "snipplet" into the script.php in the installation section:

Code: Select all

      // Create categories for our component
    $basePath = JPATH_ADMINISTRATOR . '/components/com_categories';
    require_once $basePath . '/models/category.php';
    $config = array( 'table_path' => $basePath . '/tables');
    $catmodel = new CategoriesModelCategory( $config);
    $catData = array( 'id' => 0, 'parent_id' => 0, 'level' => 1, 'path' => 'testentry', 'extension' => 'com_helloworld'
    , 'title' => 'TestEntry', 'alias' => 'testentry', 'description' => '<p>This is a default test entry categorie</p>', 'published' => 1, 'language' => '*');
    $status = $catmodel->save( $catData);
  
    if(!$status) 
	{
		JError::raiseWarning(500, JText::_('Unable to create default content category!'));
    }
And voila ... some default categories added :pop

Re: Create a category during a component installation ?

Posted: Thu May 03, 2012 5:33 am
by inturias
Thanks a lot,

This information served me A LOT!!

Re: Create a category during a component installation ?

Posted: Wed Mar 27, 2013 9:49 am
by duddy67
A bit late but thanks for this tip. It's very usefull. ;D
However, I would advise to set parent_id to 1 (not to zero) as zero is
reserved to the ROOT category.

Re: Create a category during a component installation ?

Posted: Fri Sep 20, 2013 12:46 pm
by steelfox
Biomehaniker wrote:This is currently quite easy (I´m using the sh404sef extension way).

Here is a "snipplet" which you can use. This must be added to the script which is triggered during the component installation (in the helloworld.xml) via

Code: Select all

  <scriptfile>script.php</scriptfile>
add the following "snipplet" into the script.php in the installation section:

Code: Select all

      // Create categories for our component
    $basePath = JPATH_ADMINISTRATOR . '/components/com_categories';
    require_once $basePath . '/models/category.php';
    $config = array( 'table_path' => $basePath . '/tables');
    $catmodel = new CategoriesModelCategory( $config);
    $catData = array( 'id' => 0, 'parent_id' => 0, 'level' => 1, 'path' => 'testentry', 'extension' => 'com_helloworld'
    , 'title' => 'TestEntry', 'alias' => 'testentry', 'description' => '<p>This is a default test entry categorie</p>', 'published' => 1, 'language' => '*');
    $status = $catmodel->save( $catData);
  
    if(!$status) 
	{
		JError::raiseWarning(500, JText::_('Unable to create default content category!'));
    }
And voila ... some default categories added :pop
how to add category and subcategory, when you don't know what Id will be of your category ?