模板内的 Joomla 自定义字段
Joomla custom fields inside template
我想为 Joomla 3.7 自定义我的模板,以便我可以使用 Joomla 3.7 的新功能,自定义字段 (com_fields),并通过 CSS 在我的我需要显示它们的模板。
有人可以建议我应该在模板中使用的 PHP 代码来显示字段,请提供一些示例。
提前致谢。
当然不是正确的方法,但我有同样的需求,我找到了一个基于 https://www.giudansky.com/news/12-coding/146-joomla-custom-fields
的解决方法
将 default.php 从 /components/com_content/views/article/tmpl/default.php 复制到
templates/YOUR_THEME/html/com_content/article/default.php
在第 25 行添加以下代码:
$myCustomFields = array();
foreach($this->item->jcfields as $field) {
$myCustomFields[$field->name] = $field->value;
}
$GLOBALS['myCustomFields'] = $myCustomFields;
通常,您将文章所附字段的内容放在全局变量中。
在您的模板页面上,您可以知道您的字段的检索值。
只是 print_r($GLOBALS['myCustomFields']);查看数组的内容。
这样就可以了,等待更好的答案..
我认为这绝对是错误的方法,但我很着急,所以我想出了这个快速数据库查询来查询模板中的 return 自定义字段值。这肯定违反了某种 joomla 协议吗?
显然,这假设您已经可以将 $articleid 添加到您的模板中,这是您文章的当前 ID。
我也在等待更好的解决方案,但希望这对您有所帮助
$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;
致迟到的所有人。如果您想在 Module-Override 中使用您的自定义表单字段(这确实是修改 j!-templates 的唯一方法,所以 google 'joomla template override')您可以使用这个方便的代码片段:
<?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;
}
?>
现在您可以像这样使用您的自定义字段:itemCustomFields['customFieldName1']
尚未在文章覆盖中进行测试。可能很快,如果是这样,这将得到更新。
我发现最容易理解 com_fields 在其渲染代码中是如何做到的。在 Joomla!3.7+ 中,您会在 [joomla_root]/components/com_fields/layouts/fields/render.php .
中找到它
以下是重现 Joomla 格式所需的主要部分:
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>
这将遍历组件或文章的所有可用标签。这个方法的好处是它仍然应用你包含在字段中的渲染 类。
确保将自动显示设置为不自动显示;否则你会在页面视图中看到它们两次。
如果您只想定位要显示的特定字段,您可以使用字段名称来定位它。 (标签和值对在下方。)有关详细信息,请参阅 the field Joomla docs。
我实现了这个小函数来获取特定的自定义字段值:
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;
}
用法:
$some_field_value = getCustomFieldValue('some-field-name', $some_article_id);
优化: 我将函数放入助手 class,实现变量 $fields、$field_ids 和 $model static 并检查它们是否已经加载,以防止重复加载相同的数据。
我想为 Joomla 3.7 自定义我的模板,以便我可以使用 Joomla 3.7 的新功能,自定义字段 (com_fields),并通过 CSS 在我的我需要显示它们的模板。
有人可以建议我应该在模板中使用的 PHP 代码来显示字段,请提供一些示例。
提前致谢。
当然不是正确的方法,但我有同样的需求,我找到了一个基于 https://www.giudansky.com/news/12-coding/146-joomla-custom-fields
的解决方法将 default.php 从 /components/com_content/views/article/tmpl/default.php 复制到 templates/YOUR_THEME/html/com_content/article/default.php
在第 25 行添加以下代码:
$myCustomFields = array();
foreach($this->item->jcfields as $field) {
$myCustomFields[$field->name] = $field->value;
}
$GLOBALS['myCustomFields'] = $myCustomFields;
通常,您将文章所附字段的内容放在全局变量中。 在您的模板页面上,您可以知道您的字段的检索值。 只是 print_r($GLOBALS['myCustomFields']);查看数组的内容。
这样就可以了,等待更好的答案..
我认为这绝对是错误的方法,但我很着急,所以我想出了这个快速数据库查询来查询模板中的 return 自定义字段值。这肯定违反了某种 joomla 协议吗? 显然,这假设您已经可以将 $articleid 添加到您的模板中,这是您文章的当前 ID。
我也在等待更好的解决方案,但希望这对您有所帮助
$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;
致迟到的所有人。如果您想在 Module-Override 中使用您的自定义表单字段(这确实是修改 j!-templates 的唯一方法,所以 google 'joomla template override')您可以使用这个方便的代码片段:
<?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;
}
?>
现在您可以像这样使用您的自定义字段:itemCustomFields['customFieldName1']
尚未在文章覆盖中进行测试。可能很快,如果是这样,这将得到更新。
我发现最容易理解 com_fields 在其渲染代码中是如何做到的。在 Joomla!3.7+ 中,您会在 [joomla_root]/components/com_fields/layouts/fields/render.php .
中找到它以下是重现 Joomla 格式所需的主要部分:
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>
这将遍历组件或文章的所有可用标签。这个方法的好处是它仍然应用你包含在字段中的渲染 类。
确保将自动显示设置为不自动显示;否则你会在页面视图中看到它们两次。
如果您只想定位要显示的特定字段,您可以使用字段名称来定位它。 (标签和值对在下方。)有关详细信息,请参阅 the field Joomla docs。
我实现了这个小函数来获取特定的自定义字段值:
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;
}
用法:
$some_field_value = getCustomFieldValue('some-field-name', $some_article_id);
优化: 我将函数放入助手 class,实现变量 $fields、$field_ids 和 $model static 并检查它们是否已经加载,以防止重复加载相同的数据。