Menambahkan bidang tambahan/Penggantian
From Joomla! Documentation
Bagaimana menggunakan bidang tambahan di penggantian anda
Artikel dalam Seri ini
- Pendahuluan
- Parameter untuk semua Bidang Tambahan
- Bidang Kalender
- Bidang Kotak Centang
- Bidang Warna
- Bidang Editor
- Bidang Integer
- Bidang Daftar
- Bidang Daftar Gambar
- Bidang Media
- Bidang Radio
- Repeatable Field
- Bidang SQL
- Bidang Teks
- Bidang Area Teks
- Bidang URL
- Bidang Pengguna
- Bidang Kelompok Pengguna
- Bagaimana mengelompokkan bidang tambahan
- Komponen yang mendukung bidang tambahan
- Penerapan ke dalam komponen anda
- Gunakan bidang tambahan di penggantian anda
Bagaimana menggunakan bidang tambahan di penggantian anda
Pendahuluan
Kekuatan sebenarnya dari bidang tambahan adalah anda dapat menggunakannya di dalam penggantian (overrides) anda sendiri. Berikut adalah contoh bagaimana anda dapat melakukannya.
Mendapatkan bidang tambahan ke dalam penggantian anda
Pada dasarnya anda memiliki semua bidang tambahan sehubungan dengan butir saat ini yang dapat diakses lewat suatu properti baru di variabel $item
yang disebut jcfields
.
Properti $item->jcfields
adalah sebuah array yang memegang data selanjutnya per bidang, dimana satu bidang tampak seperti contoh berikut:
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
)
)
Hasilkan bidang dengan menggunakan FieldsHelper
Untuk menghasilkan (render) bidang, anda dapat gunakan FieldsHelper::render()
dengan melemparkan nilai yang dibutuhkan.
/**
* 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)
Contoh kode untuk penggantian menggunakan FieldsHelper
// 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 ?>
Contoh kode untuk suatu penggantian mentah
<?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 tidak mengandung bidang yang saya perlukan
Properti jcfields
dihasilkan dengan menggunakan event plugin onContentPrepare
dengan melemparkan konteks dari bidang tersebut. Plugin bidang kemudian membaca bidang itu dari database lalu menambahkan nilai ke dalam properti jcfields
.
Jadi, pastikanlah komponen yang anda gunakan juga menerapkan event onContentPrepare
dengan konteks yang anda pakai untuk bidang tersebut.
Apabila anda menggunakan komponen inti maka ini akan bekerja luar biasa.
Loading individual fields
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.