How to get the value of an individual field:
<?php echo CobaltApi::renderField($record, $field_id, $view, $default, $bykey); ?>
$record- Record ID or record object which you get through
$field_id- ID of the field you want to render.
list. Render view - either article list or full article view.
$default- Default display value if nothing was saved in the field.
$bykey- By default is is false. It tells either we get field by key or by ID. If it is true then
$field_idis treated as field key.
Don’t call it in list and full article templates to avoid unnecessary SQL queries. Every field value is accessible and already loaded there.
You can calculate different rating summaries with this API.
<?php $rating = CobaltApi::renderRating($type_id, $section_id, $condition); ?>
$type_id- ID of the content type. This will be used to understand what is rating parameters. If it is multiple property rating or single property and how to calculate results.
$section_id- ID of the section.
WHEREconditions. There is
ralias used for
#__js_res_recordtable. See examples.
This method will return
$rating['html']- This is ready to render HTML or current rating. It is created according to content type rating parameters.
$rating['total']- Total result as number from 0 to 100 where 0 is nothing and 100 is maximum rating.
$rating['num']- Total number of ratings given.
$rating['multi'] - If it is multiple property rating this is going to be an array with will contain
num per every property where
sum is a total result as number from 0 to 100 per property and
num is a total number of ratings given for this property.
<?php $rating['multi']['sum'] = 50; $rating['multi']['num'] = 2; $rating['multi']['sum'] = 89; $rating['multi']['num'] = 2;
This means that property 1 (index 0) average 50% rating given in 2 ratings and property 2 is 89%.
With this data returned you may use it in all different ways. For example you want to get rating of the current use in given section.
<?php $rating = CobaltApi::renderRating(1, 1, 'r.user_id = 10');?> <div> Total <?php echo $rating['total'] ?> out of <?php echo $rating['num'] ?> votes! </div>
On of the most useful helper in Cobalt API is
ItemsStore class which allow you quickly get record, section, category or type object.
<?php $record = ItemsStore::getRecord($record_id); $section = ItemsStore::getSection($section_id); $type = ItemsStore::getType($type_id); $category = ItemsStore::getCategory($cat_id);
This initialise parameters as well so you can get access to type or section parameters.
$record will not contain fields.
Another important class is Url. It created Urls. It is important to use this class to create Urls for Itemid to attach to URL correctly.
<a href="<?php echo JRoute::_(Url::record($record)); ?>"> <a href="<?php echo JRoute::_(Url::records($section)); ?>"> <a href="<?php echo Url::add($section, $type, $category); ?>">
Note that urls to add or edit record already passed through
$section may be as object returned by
ItemsStore or ID.
You can programmaticaly create new records. For that you can use API
CobaltApi::createRecord($data, $section_id, $type_id, $fields, $categories, $tags)
$datais an array. The key of the array is the name of the column in
_js_res_record. One parameter is required is a
title. All other parameters like
accesswill be set acording to your type parameters. Of course if you set then here, those will be overwriten.
$section_idid of the section
$type_idid of record type
$fields is an array of fields. The key is an ID fo the field. For example you want to set fields 12 and 34
[ 12 => ... 34 => ... ]
What to put as fields value? You can find it on Cobalt record form. Just look what values this or that field have. For emaple Status field has a single integer value so ti will be like
12 => 2 and Select field has an array of values
12 => ['value 1', 'Value 2'].
$categoriesarray of categories you want to put this record to. Even if it is only one category, should be an array
$tagslist aof tags. String array
['Tag 1', 'Tag 2]
<?php CobaltApi::createRecord( [ 'title' => 'test 3', 'access' => 1 ], 4, //section 5, // type [ 65 => 5, 67 => ['Red', 'Blue'] ], // fields , //categpries ['joomla', 'moomla'] // tags );
 is a new syntax for PHP array defenition. So
 is equal to
To update record use
<?php CobaltApi::updateRecord($redord_id, $data, $fields, $categories, $tags);
$record_id- id of the edited record.