Difference between revisions of "Adding custom fields/Overrides"
From Joomla! Documentation
< J3.x:Adding custom fields
m |
|||
(12 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
<noinclude><languages /></noinclude> | <noinclude><languages /></noinclude> | ||
− | { | + | |
+ | |||
+ | {{section portal heading|icon=edit|title=<translate><!--T:1--> | ||
How to use custom fields in your overrides</translate>}} | How to use custom fields in your overrides</translate>}} | ||
− | {{J3.x:Adding_custom_fields/TOC/< | + | {{J3.x:Adding_custom_fields/TOC/<translate><!--T:18--> |
+ | en</translate>}} | ||
<translate> | <translate> | ||
== How to use custom fields in your overrides == <!--T:2--> | == How to use custom fields in your overrides == <!--T:2--> | ||
Line 19: | Line 22: | ||
<translate><!--T:6--> | <translate><!--T:6--> | ||
− | Basically you have all the custom fields corresponding to the current item accessible via a new property in your <code>$item</code> variable called < | + | Basically you have all the custom fields corresponding to the current item accessible via a new property in your <code>$item</code> variable called <tt>jcfields</tt>.</translate> |
<translate><!--T:7--> | <translate><!--T:7--> | ||
− | The < | + | The <tt>$item->jcfields</tt> property is an array that holds the following data per field, where one field looks like this example:</translate> |
<source lang="php"> | <source lang="php"> | ||
Line 142: | Line 145: | ||
<translate> | <translate> | ||
− | === < | + | === <tt>$item->jcfields</tt> does not contain the fields I need === <!--T:12--> |
</translate> | </translate> | ||
<translate><!--T:13--> | <translate><!--T:13--> | ||
− | The < | + | The <tt>jcfields</tt> property is generated using the plugin event <tt>onContentPrepare</tt> by passing the context of the fields. The fields plugin then reads the fields from the database and adds the values to the jcfields property.</translate> |
<translate> | <translate> | ||
<!--T:14--> | <!--T:14--> | ||
Line 153: | Line 156: | ||
<translate><!--T:15--> | <translate><!--T:15--> | ||
If you use the core components this should work out of the box.</translate> | If you use the core components this should work out of the box.</translate> | ||
+ | |||
+ | <translate> | ||
+ | |||
+ | === Loading individual fields === <!--T:19--> | ||
+ | </translate> | ||
+ | |||
+ | <translate><!--T:20--> In order to add individual fields to your content, start by choosing specific names for your custom fields, using the '''Name''' field, which will be used to reference your field directly in the override code. In the <tt>Options</tt> tab, select '''No''' in the <tt>Automatic Display</tt> field to prevent it from being displayed automatically in any of the standard positions.</translate> | ||
+ | |||
+ | <translate><!--T:21--> Then, to enable direct access by name to custom fields in an override, place the code below at the beginning of your file. You should do this to every override PHP file that you want to place individual custom fields on.</translate> | ||
+ | |||
+ | <source lang="PHP"> | ||
+ | <?php foreach($item->jcfields as $jcfield) | ||
+ | { | ||
+ | $item->jcFields[$jcfield->name] = $jcfield; | ||
+ | } | ||
+ | ?> | ||
+ | </source> | ||
+ | |||
+ | <translate><!--T:22--> And lastly, you should add the field placement code at the spot you want the field label or value to be shown.</translate> | ||
+ | |||
+ | <translate><!--T:23--> To add the '''label''' of the field to your override, insert the code below, replacing <tt>name-of-field</tt> with the name of the field.</translate> | ||
+ | |||
+ | <source lang="PHP"><?php echo $item->jcFields['name-of-field']->label; ?></source> | ||
+ | |||
+ | <translate><!--T:24--> To add the '''value''' of the field to your override, insert the code below, replacing <tt>name-of-field</tt> with the name of the field. Beware: in the 3.x series the '''value''' is in fact the '''rawvalue''' https://github.com/joomla/joomla-cms/issues/20216</translate> | ||
+ | |||
+ | <source lang="PHP"><?php echo $item->jcFields['name-of-field']->rawvalue; ?></source> | ||
+ | |||
+ | <translate><!--T:25--> You can add this code to any part of your override. Examples: The content of a div, the src in an <code>img</code> tag, within a CSS class attribute, etc.</translate> | ||
Line 163: | Line 195: | ||
__NOTOC__ | __NOTOC__ | ||
<noinclude> | <noinclude> | ||
− | + | [[Category:Tutorials{{#translation:}}]] | |
− | [[Category:Tutorials]] | + | [[Category:Custom Fields{{#translation:}}]] |
− | [[Category:Custom Fields]] | + | [[Category:Beginners{{#translation:}}]] |
− | [[Category:Beginners]] | + | [[Category:Development{{#translation:}}]] |
− | [[Category:Development]] | + | [[Category:Component_Development{{#translation:}}]] |
− | [[Category:Component_Development]] | + | [[Category:Joomla! 3.x{{#translation:}}]] |
− | [[Category:Joomla! 3.x]] | + | [[Category:Joomla! 3.7{{#translation:}}]] |
− | [[Category:Joomla! 3.7]] | + | [[Category:Joomla! 3.8{{#translation:}}]] |
+ | [[Category:Joomla! Website Management{{#translation:}}]] | ||
</noinclude> | </noinclude> |
Latest revision as of 20:20, 23 December 2018
How to use custom fields in your overrides
Articles in this Series
- Introduction
- Parameters for all Custom Fields
- Calendar Field
- Checkboxes Field
- Color Field
- Editor Field
- Integer Field
- List Field
- List of Images Field
- Media Field
- Radio Field
- Repeatable Field
- Sql Field
- Text Field
- Textarea Field
- Url Field
- User Field
- Usergroup Field
- How can you group custom fields
- What components are supporting custom fields
- Implementation into your component
- Use custom fields in your overrides
How to use custom fields in your overrides[edit]
Introduction[edit]
The real power of custom fields is that you can use it in your own overrides. Here is a example of how you can do that.
Getting custom fields in your override[edit]
Basically you have all the custom fields corresponding to the current item accessible via a new property in your $item
variable called jcfields.
The $item->jcfields property is an array that holds the following data per field, where one field looks like this example:
Array
(
[4] => stdClass Object
(
[id] => 4
[title] => article-editor
[name] => article-editor
[checked_out] => 0
[checked_out_time] => 0000-00-00 00:00:00
[note] =>
[state] => 1
[access] => 1
[created_time] => 2017-04-07 12:08:59
[created_user_id] => 856
[ordering] => 0
[language] => *
[fieldparams] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[buttons] =>
[width] =>
[height] =>
[filter] =>
)
[initialized:protected] => 1
[separator] => .
)
[params] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[hint] =>
[render_class] =>
[class] =>
[showlabel] => 1
[disabled] => 0
[readonly] => 0
[show_on] =>
[display] => 2
)
[initialized:protected] => 1
[separator] => .
)
[type] => editor
[default_value] =>
[context] => com_content.article
[group_id] => 0
[label] => article-editor
[description] =>
[required] => 0
[language_title] =>
[language_image] =>
[editor] =>
[access_level] => Public
[author_name] => Super User
[group_title] =>
[group_access] =>
[group_state] =>
[value] => Bar
[rawvalue] => Bar
)
)
Render the field using the FieldsHelper[edit]
To render the field you can use FieldsHelper::render()
by passing the needed values.
/**
* Renders the layout file and data on the context and does a fall back to
* Fields afterwards.
*
* @param string $context The context of the content passed to the helper
* @param string $layoutFile layoutFile
* @param array $displayData displayData
*
* @return NULL|string
*
* @since 3.7.0
*/
public static function render($context, $layoutFile, $displayData)
Example code for the override using the FieldsHelper[edit]
// Load the FieldsHelper
<?php JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); ?>
<?php foreach ($this->item->jcfields as $field) : ?>
// Render the field using the fields render method
<?php echo FieldsHelper::render($field->context, 'field.render', array('field' => $field)); ?>
<?php endforeach ?>
Example code for a raw override[edit]
<?php foreach ($this->item->jcfields as $field) : ?>
// Render the field using the fields render method
<?php echo $field->label . ':' . $field->value; ?>
<?php endforeach ?>
$item->jcfields does not contain the fields I need[edit]
The jcfields property is generated using the plugin event onContentPrepare by passing the context of the fields. The fields plugin then reads the fields from the database and adds the values to the jcfields property.
So please make sure that the component you use also implements the onContentPrepare
event with the context you use for the fields.
If you use the core components this should work out of the box.
Loading individual fields[edit]
In order to add individual fields to your content, start by choosing specific names for your custom fields, using the Name field, which will be used to reference your field directly in the override code. In the Options tab, select No in the Automatic Display field to prevent it from being displayed automatically in any of the standard positions.
Then, to enable direct access by name to custom fields in an override, place the code below at the beginning of your file. You should do this to every override PHP file that you want to place individual custom fields on.
<?php foreach($item->jcfields as $jcfield)
{
$item->jcFields[$jcfield->name] = $jcfield;
}
?>
And lastly, you should add the field placement code at the spot you want the field label or value to be shown.
To add the label of the field to your override, insert the code below, replacing name-of-field with the name of the field.
<?php echo $item->jcFields['name-of-field']->label; ?>
To add the value of the field to your override, insert the code below, replacing name-of-field with the name of the field. Beware: in the 3.x series the value is in fact the rawvalue https://github.com/joomla/joomla-cms/issues/20216
<?php echo $item->jcFields['name-of-field']->rawvalue; ?>
You can add this code to any part of your override. Examples: The content of a div, the src in an img
tag, within a CSS class attribute, etc.