Chapter 3: Blog type
In the blog.php file we define our class as follows:
class ARKContextsMyCCKBlog extends ARKContextsMyCCKItem
{
//Rest of the code goes here…
}
We extend the ARKContextsMyCCKItem class we defined in the item.php file for the item type. This is because most of the work is already done by that class; all we need to do is make slight modifications to the triggerContentPlugins and the Save method.
Here is our full implementation for the ARKContextsMyCCKBlog class:
class ARKContextsMyCCKBlog extends ARKContextsMyCCKItem
{
public function triggerContentPlugins($rawText)
{
$item = new stdclass;
$text = '';
if (isset($rawText))
{
$pattern = '#<hr\s+id=("|\')system-readmore("|\')\s*\/*>#i';
$tagPos = preg_match($pattern, $rawText);
if ($tagPos == 0)
{
$text = $rawText;
}
else
{
list ($text, $rawText) = preg_split($pattern, $rawText, 2);
}
}
$item->text = $text;
$params = new JObject;
$params->set('inline',false);
$dispatcher = JEventDispatcher::getInstance();
JPluginHelper::importPlugin('content');
$dispatcher->trigger('onContentPrepare', array ('com_mycck.category', &$item, &$params, 0));
return array( 'data'=>$item->text);
}
public function save($data,$type = 'body')
{
if($this->id == null)
return array( 'title'=>'','data'=>'');
if($type == 'title')
{
$data['title'] = strip_tags($data['title']);
$data['title'] = html_entity_decode($data['title']);
}
if(isset($data['articletext']))
$data['articletext'] = base64_decode($data['articletext']);
$this->table->save($data);
//We need to process data as we are sending it back to the client
JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models');
$model = JModelLegacy::getInstance('article','ContentModel');
$item = $model->getItem($this->id);
$item->text = $item->introtext;
//let's detect if any plugin tags are being used
//if so let's inform the system to warn the user
$message = $this->detectPluginTags($item->text);
$dispatcher = JEventDispatcher::getInstance();
JPluginHelper::importPlugin('content');
$item->params->set('inline',false); //set this so inline plugin does not pick this up
$dispatcher->trigger('onContentPrepare', array ('com_mycck.category', &$item, &$item->params, 0));
return array( 'title'=>$item->title,'data'=>$item->text,'message'=>$message);
}
}
TriggerContentPlugins Method
The method is basically the same as the implementation of the parent class, except here we are not simply striping out the read more tag but we are splitting the text and only returning the introduction part.
Save Method
Saving the actual data is the same as in the Item type class. However, when we are returning the data, just as in the trigger ContentPlugins method when we are dealing with the HTML content, we only return the introduction text.
Also, we change this line of code:
$dispatcher->trigger('onContentPrepare', array ('com_mycck.item', &$item, &$item->params, 0));
To
$dispatcher->trigger('onContentPrepare', array ('com_mycck.category', &$item, &$item->params, 0));
Please take note: that how we deal with the introduction text and the main text for our items in our component, maybe different to how you deal with it in yours. You may use some string truncation function and if that is the case you will need to use that method instead.