Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joomla custom fields inside template

Tags:

php

joomla

I would like to customize my template for Joomla 3.7 so that I can use the new feature of Joomla 3.7, Custom fields (com_fields), and display and format them via CSS in my template where I need to display them.

Can someone suggest me the PHP code I should use in the template to display field(s), some example please.

Thanks in advance.

like image 233
Francesco Avatar asked May 11 '17 08:05

Francesco


People also ask

What are Joomla custom fields?

Custom fields in Joomla! is a technology to extend various item types (content articles, contact items, etc.) with custom-made fields. We could e.g. extend a content article with a text field named fact_checked_by, and every content article would then get a text field where the administrator could enter a name.

How are mandatory fields indicated in Joomla creation forms?

To define form fields in the configuration of an extension, you must include them in a named fieldset, such as <fieldset name="basic"> , that is within the <fields name="params"> section of the <config> element in your XML manifest file.


2 Answers

For everyone getting late to the party. In case you want to use your custom form fields in a Module-Override (which really are the only way to modify j!-templates, so google 'joomla template override') you can use this handy snippet:

<?php
    JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
    $jcFields = FieldsHelper::getFields('com_content.article', $item, true);
    $itemCustomFields = array();
    foreach($jcFields as $field) {
        $itemCustomFields[$field->name] = $field->rawvalue;
    }
?>

Now you cna use your customfields like so: itemCustomFields['customFieldName1']

Haven't tested in article overrides. May soon, if so, this will get updated.

like image 180
faxemaxe Avatar answered Sep 28 '22 07:09

faxemaxe


certainly not the right way to do it but I had the same need and I found a work around based on https://www.giudansky.com/news/12-coding/146-joomla-custom-fields

Copie default.php from /components/com_content/views/article/tmpl/default.php to templates/YOUR_THEME/html/com_content/article/default.php

Add following code line 25 :

$myCustomFields = array();
    foreach($this->item->jcfields as $field) {
        $myCustomFields[$field->name] = $field->value;
    } 

$GLOBALS['myCustomFields'] = $myCustomFields;

Typically you put on a global var the content of fields attached to your article. On your template page you can know retrieved value of your field. just print_r($GLOBALS['myCustomFields']); to view the content of your array.

That will do the trick waiting for a better answer..

like image 21
Cedric Avatar answered Sep 28 '22 06:09

Cedric