Create a category during a component installation ?

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
duddy67
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 229
Joined: Sun Jul 12, 2009 12:04 pm

Create a category during a component installation ?

Post by duddy67 » Sat Aug 06, 2011 2:47 pm

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.

Wootah
Joomla! Explorer
Joomla! Explorer
Posts: 332
Joined: Thu Aug 11, 2011 5:00 am

Re: Create a category during a component installation ?

Post by Wootah » Mon Jan 16, 2012 9:50 pm

Hi did you get any code to do this?

User avatar
fmmarzoa
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 123
Joined: Sun Nov 11, 2007 1:15 pm
Location: Spain
Contact:

Re: Create a category during a component installation ?

Post by fmmarzoa » Mon Jan 16, 2012 11:20 pm

sh404 extension creates one. Perhaps installing it and examining its code may help you.
“The media's the most powerful entity on earth. They have the power to make the innocent guilty and to make the guilty innocent, and that's power. Because they control the minds of the masses.” -- Malcolm X

Biomehaniker
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Tue Jul 11, 2006 8:17 pm

Re: Create a category during a component installation ?

Post by Biomehaniker » Thu Feb 09, 2012 10:16 am

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

User avatar
inturias
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Sat Sep 26, 2009 3:02 pm
Location: Bolivia
Contact:

Re: Create a category during a component installation ?

Post by inturias » Thu May 03, 2012 5:33 am

Thanks a lot,

This information served me A LOT!!
Leer, leer, leer...

duddy67
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 229
Joined: Sun Jul 12, 2009 12:04 pm

Re: Create a category during a component installation ?

Post by duddy67 » Wed Mar 27, 2013 9:49 am

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.

steelfox
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Tue Jul 28, 2009 3:32 pm

Re: Create a category during a component installation ?

Post by steelfox » Fri Sep 20, 2013 12:46 pm

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 ?


Locked

Return to “Joomla! 2.5 Coding”