Beside all Joomla objects and properties that can be accessed as in any Joomla code (e.g. JFactory::getUser() ) there are few additional variables available in the Code Pack events:
$fields
An array that holds all data from the fields, each field can be accessed by its name e.g. $fields['art_title'].
Each field has mutiple properties that can be manipulated e.g. value, form, typo etc. - to manipulate value you would do e.g.
$fields['art_title']->value = "some title";
$config
An array of all configuration of the context e.g.
- $config['pk'] - holds primary key, which in case of article is Id of the article (#__content table)
- $config['isNew'] =- its value is 1 for Add and 0 for Edit
Before Render
As name of the event says, it is executed before content, list or form is rendered.
To change value of the field in the content you generally need to change value property as shown above. If you used typography on the field you will also need to change typo property - this would be mandatory for some fields like image field.
To change output on the list (and on content for some fields that use multiple data bits to render output) you also need to change html or some other property - .e.g. for upload image you need to change html property, e.g. code bellow would render default image if upload image field has no stored value
if (empty($fields['image_field']->value)) { $fields['image_field']->value = 'images/test.jpg'; $fields['image_field']->html = '<img src="/images/test.jpg" title="test" alt="test" />'; }
To manipulate form fields value you will need to change value and form properties - what gets output is form property which at that point contains form html string like
<input type="text" name="art_title" value="My username is #username">
The bellow code would replace #username string from article title field with current users username in the form. You can do the same in the conent view, just leave out the last line manipulating form property.
$username = JFactory::getUser()->username; $fields['art_title']->value = str_replace('#username', $username, $fields['art_title']->value ); $fields['art_title']->form = str_replace( '#username', $username, $fields['art_title']->form );
Before Store
Event is executed before form is stored to the database. It has access to the same properties as Before Render, but to manipulate values that will get stored, you will also need to manipulate storages array of the $config variable following this pattern:
$config['storages'][Storage_Table][Storage_Column]
E.g. to change article title field at this point, we need to know its storage table (#__content for Article object, #__users for User, #__categories for Category etc) and its storage column (NOT name), then we can assign it new value
$config['storages']['#__content']['title'] = "Some new title";
To avoid having to know table and column you can use its storage_field and storage_table properties, so generic code to alter fields value on storage would be (just change fields name from $fields array):
$name = $fields['art_title']->storage_field; $table = $fields['art_title']->storage_table; $new_value = 'my_new_value'; $config['storages'][$table][$name] = $new_value; $fields['art_title']->value = $new_value;
After Store
After store has access to the same variables as before store, main difference is that it is executed after data was already stored to the database so you cannot change Seblod processing from here, its purpose is to allow you to use values to do something else e.g. to store some fields value to some custom database table or call some 3rd party API code to .e.g. publish article to the facebook or twitter.
Code CSS and JS
Using this fields you can add some css styling or javascript to your content type - it differs from other methods that add css/js globally as it is added just to this content type, so you can avoid errors or enable different behaviour.