Hi folks

This is a quick and basic intro in to creating content dynamically using code ie in a beforeStore or AfterStore field.

If I create some content for a content type, I might want to create content for another content type at the same time.

There is a method for adding Seblod content types, and a slightly different method for just Joomla.

EXAMPLE 1::Seblod Method

I have a content type called 'daffy-duck'. I want to fill in the form and at the same time create another piece of content for a content type called 'cauliflower' In the form (admin or site) is an 'Afterstore' field.

In the config for this afterstore field set the construction to 'free'.

And add your code, similar to this... 

// create
	$newStuff = new JCckContent( 
	    array('joomla_article') 
	);
	$addStuff = $newStuff->create(
	    'cauliflower',
	    array(
                // Joomla table ie #__content
	        'title'=> $fields['some_field_in_my_daffy_duck_content_type']->value . '-' . $config['pk'],
	        'state'=> 1,
	        'catid'=> $fields['some_field_in_my_daffy_duck_content_type']->value,
	        'access'=> 1,
	        'language'=> '*'
	    ),
	    array(
             // Seblod table ie #__cck_store_form_cauliflower
	     'some_db_column_name'=> $fields['some_field_in_my_daffy_duck_content_type']->value,	    
            )
	);
	// create END
<br>
NOTE: $config['pk'] is available AFTER you have stored the data from the main form. Primary key does not exist until then so in beforestore you can not do this. See below for a solution to getting id of most recent bit of content


Get the id of some content just saved (alter the table name as required):

// Get id 
$dbFunky = JFactory::getDbo();
$queryFunky= $dbFunky->getQuery(true);
$queryFunky
    ->select('MAX(' . $dbFunky->quoteName('id'). ')')
    ->from($dbFunky->quoteName('#__content'));
$dbFunky->setQuery($queryFunky);
$resultFunky = $dbFunky->loadResult();
<br>



EXAMPLE 2::Joomla Method to create Menu Item

In the Joomla menus db table (#__menu) there are the colums 'lft' and 'rgt'

These control hierarchy and ordering.

Here is an example of how to add a new menu item

// Create the menu item
$myMenuTable = JTableNested::getInstance('Menu');
// assign the values
$myMenuData = array(
    // Alter to your requirements
    // 'id' = '; // Automatic
    'menutype' => $fields['some_field_in_my_daffy_duck_content_type']->value, // Required
    'title' => $fields['some_field_in_my_daffy_duck_content_type']->value, // Required
    'alias' => $fields['some_field_in_my_daffy_duck_content_type']->value // Required
    'note' => $fields['some_field_in_my_daffy_duck_content_type']->value,
    'path' => $fields['some_field_in_my_daffy_duck_content_type']->value, // Required
    'link' => $fields['some_field_in_my_daffy_duck_content_type']->value, // Required
    'type' => 'component', // Required
    'published' => 1, // Required
    'parent_id' => 1, // Required
    'level' => 1, // Required
    'component_id' => 10003, // Required, look in db to find correct number, in #__menu and compare against #__extensions.extension_id
    // 'checked_out' => $fields['some_field_in_my_daffy_duck_content_type']->value,
    // 'checked_out_time' => $fields['some_field_in_my_daffy_duck_content_type']->value,
    // 'browserNav' => $fields['some_field_in_my_daffy_duck_content_type']->value,
    'access' => 1, // Required
    // 'img' => $fields['some_field_in_my_daffy_duck_content_type']->value,
    'template_style_id' => 3, // Required, you need to find this number yourself
    'params' => '{"show_list_title":"1","tag_list_title":"h2",' etc,
    // 'lft' => Not required,
    // 'rgt' => Not required,
     'home' => 0, (you would not be creating multiple home pages dynamically
    'language' => *, // Required, apply as you see fit
    'client_id' => 0 // Required, 0 is site, 1 is admin
);
// refer to db to confirm the correct value you require
$parentId = 1;
$myMenuTable->setLocation($parentId, 'last-child');
// save is the shortcut method for bind, check and store
if (!$myMenuTable->save($myMenuData))
{
  $this->setError($myMenuTable->getError());
  return false;
}
// Menu Item created
<br>

EXAMPLE 3::Add a Tag Reference

If you want to apply a tag to some piece of content

// Create Tag Item Map for member
$newTagLink = new stdClass();
$newTagLink->type_alias = 'com_content.article'; 
$newTagLink->core_content_id = $fields['some_field_in_my_daffy_duck_content_type']->value, // can't remember what this is
$newTagLink->content_item_id = $fields['some_field_in_my_daffy_duck_content_type']->value, // article id
$newTagLink->tag_id = $fields['some_field_in_my_daffy_duck_content_type']->value,; // id of tag you want to associate with
// $newTagLink->tag_date = '';
$newTagLink->type_id = 1; //  article. category = 5
// Insert the object into the content table.
$result = JFactory::getDbo()->insertObject('#__contentitem_tag_map', $newTagLink);
<br>

So there you have it, 3 methods for adding content/data to your db using afterstore/beforestore. Plus an example of how to get the id of most recent bit of content.

This really adds flexibility and power to your Seblod site... 

The Seblod method can be used for Users, Usergroups, Articles, Categories, Usernotes. Find the names in #__cck_core.storage_location

$newStuff = new JCckContent( 
    array('joomla_article') // or //
    array('joomla_category') // or //  
    array('joomla_user') // or //  
    array('joomla_user_group') // or //  
    array('joomla_user_note') // <- I have never tried this, though I assume it would be this or similar.
);<br>

Hope that helps

Bucklash