Francesco
Francesco

Reputation: 397

Joomla custom fields inside template

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.

Upvotes: 5

Views: 6740

Answers (5)

Benno
Benno

Reputation: 130

I implemented this small function to get specific custom field values:

function getCustomFieldValue($field_name, $article_id, $default_value = '') {

  // Load custom field list
  $fields = FieldsHelper::getFields('com_content.article', $article_id, true);
  $field_ids = array_column($fields, 'id', 'name');
  $model = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true));
  
  // Return the value if the field exists, otherwise the default
  return array_key_exists($field_name, $field_ids) 
    ? $model->getFieldValue($field_ids[$field_name] , $article_id) 
    : $default_value;
  }

Usage:

$some_field_value = getCustomFieldValue('some-field-name', $some_article_id);

Optimization: I placed the function into a helper class, implemented the variables $fields, $field_ids and $model static and checked if they are already loaded to prevent redundant loading of the same data.

Upvotes: 0

eglescout
eglescout

Reputation: 31

I found it was easiest to follow how com_fields does it in its rendering code. In Joomla!3.7+, you'll find it in [joomla_root]/components/com_fields/layouts/fields/render.php .

Here are the main parts you need to reproduce the formatting that Joomla has:

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
<dl class="fields-container">
<?php foreach ($this->item->jcfields as $field) : ?>
  <?php // If the value is empty do nothing ?>
  <?php if (!isset($field->value) || $field->value == '') : ?>
    <?php continue; ?>
  <?php endif; ?>
  <?php $class = $field->params->get('render_class'); ?>
  <dd class="field-entry <?php echo $class; ?>">
    <?php echo FieldsHelper::render($context, 'field.render', array('field' => $field)); ?>
  </dd>
<?php endforeach; ?>
</dl>

This loops through all available tags for the component or article. The nice thing about this method is it still applies the render classes you include with the fields.

Make sure to set Automatic Display to Do not automatically display on your fields; otherwise you will see them twice on your page view.

If you want to just target specific fields to show, you can use the name of the field to target it. (The label and value pair is underneath.) See the field Joomla docs for more info.

Upvotes: 1

faxemaxe
faxemaxe

Reputation: 81

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.

Upvotes: 7

Derek Webster
Derek Webster

Reputation: 31

This is absolutely the wrong way to do this I think but I was tearing my hair out so i came up with this quick db query to return custom field values in the template. surely this violates some kind of joomla protocol? obviously this assumes you can get $articleid into your template already which is the Current ID of your article.

I too am waiting on a better solution but hope this helps

$db =& JFactory::getDBO();

$sql = "select * from #__fields_values where `item_id` = $articleid";                                                                            
$db->setQuery($sql);  
$fieldslist = $db->loadObjectList();

echo $fieldslist[0]->value;
echo $fieldslist[1]->value;
echo $fieldslist[your field ID here]->value;

Upvotes: 3

Cedric
Cedric

Reputation: 76

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..

Upvotes: 6

Related Questions