I am using the following code to pull in some custom fields into an override of com_content > category > blog_item.php:
$video = $this->item->jcfields[4]->value;
$media = $this->item->jcfields[8]->value;
Further down in the file, I have added the following blocks of code to perform some conditionals:
<?php if(isset($media) && !empty($media)) : ?>
<!-- Media custom field output -->
<div class="media-block">
<?php echo $media; ?>
</div>
<!-- Media custom field output -->
<?php endif; ?>
For some reason the if conditionals are not working as expected. The Media custom field output block is always being displayed, even if the custom field has no defined value in the database.
Is this an issue with the implementation of the custom fields in the override or something else?
In Joomla 3, I was using the following for adding custom fields to com_content template overrides:
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$context = 'com_content.article';
$article = $this->item;
$fields = FieldsHelper::getFields($context, $article, true);
foreach($fields as $field) {
$fields[$field->name] = $field;
}
<!-- Media custom field output -->
<?php if(isset($fields['media']->value) && !empty($fields['media']->value)): ?>
<?php $class = $fields['media']->params->get('render_class'); ?>
<?php echo $fields['media']->value; ?>
<?php endif; ?>
<!-- Media custom field output -->
I prefer the shortened version for Joomla 4, and would like to keep using it if possible.
Update with print_r
of $video
variable
stdClass Object
(
[id] => 4
[title] => YouTube Video
[name] => youtube-video
[checked_out] =>
[checked_out_time] =>
[note] =>
[state] => 1
[access] => 1
[created_time] => 2022-03-23 16:16:30
[created_user_id] => 385
[ordering] => 0
[language] => *
[fieldparams] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[size] => responsive
[width] => 480
[height] => 270
[autoplay] => 0
[cc_load_policy] => 0
[color] => red
[controls] => 1
[disablekb] => 0
[start] => 0
[end] => 0
[fs] => 1
[iv_load_policy] => 1
[loop] => 0
[modestbranding] => 0
[rel] => 1
[privacy_mode] => 0
)
[initialized:protected] => 1
[separator:protected] => .
)
[params] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[hint] =>
[class] =>
[label_class] =>
[show_on] => 2
[showon] =>
[render_class] =>
[value_render_class] =>
[showlabel] => 1
[label_render_class] =>
[display] => 0
[prefix] =>
[suffix] =>
[layout] =>
[display_readonly] => 0
[searchindex] => 0
[rules] => Array
(
[0] => stdClass Object
(
[matching_method] => all
[enabled] => 1
[rules] => Array
(
[0] => stdClass Object
(
[name] =>
[enabled] => 1
)
)
)
)
[acf_custom_css] =>
[acf_layout_override] => default
)
[initialized:protected] => 1
[separator:protected] => .
)
[type] => acfyoutube
[default_value] =>
[context] => com_content.article
[group_id] => 0
[label] => YouTube Video
[description] =>
[required] => 0
[only_use_in_subform] => 0
[language_title] =>
[language_image] =>
[editor] =>
[access_level] => Public
[author_name] => Mike Hermary
[group_title] =>
[group_access] =>
[group_state] =>
[group_note] =>
[value] =>
[rawvalue] =>
)
Update two with var_dump of $media custom field
object(stdClass)#2173 (33)
["id"]=> int(8)
["title"]=> string(14) "External Media"
["name"]=> string(14) "external-media"
["checked_out"]=> NULL
["checked_out_time"]=> NULL
["note"]=> string(0) ""
["state"]=> int(1)
["access"]=> int(1)
["created_time"]=> string(19) "2022-04-08 11:18:06
["created_user_id"]=> int(385)
["ordering"]=> int(0)
["language"]=> string(1) "*"
["fieldparams"]=>
object(Joomla\Registry\Registry)#1563 (3) {
["data":protected]=> object(stdClass)#1556 (4) {
["iframeheight"]=> string(4) "100%"
["iframescrolling"]=> string(4) "auto"
["iframeasync"]=> string(1) "1"
["iframeparams"]=> string(23) "allowfullscreen loading"
}
["initialized":protected]=> bool(true)
["separator":protected]=> string(1) "."
}
["params"]=> object(Joomla\Registry\Registry)#1561 (3) {
["data":protected]=> object(stdClass)#1554 (12) {
["hint"]=> string(0) ""
["class"]=> string(0) ""
["label_class"]=> string(0) ""
["show_on"]=> string(1) "2"
["render_class"]=> string(0) ""
["value_render_class"]=> string(0) ""
["showlabel"]=> string(1) "0"
["label_render_class"]=> string(0) ""
["display"]=> string(1) "0"
["layout"]=> string(0) ""
["display_readonly"]=> string(1) "1"
["rules"]=> array(1) {
[0]=> object(stdClass)#1553 (3) {
["matching_method"]=> string(3) "all"
["enabled"]=> string(1) "1"
["rules"]=> array(1) {
[0]=> object(stdClass)#1552 (2) {
["name"]=> string(0) ""
["enabled"]=> string(1) "1"
}
}
}
}
}
["initialized":protected]=> bool(true)
["separator":protected]=> string(1) "."
}
["type"]=> string(9) "acfiframe"
["default_value"]=> string(0) ""
["context"]=> string(19) "com_content.article"
["group_id"]=> int(0)
["label"]=> string(14) "External Media"
["description"]=> string(68) "Use this field to embed audio and video media from external websites"
["required"]=> int(0)
["only_use_in_subform"]=> int(0)
["language_title"]=> NULL
["language_image"]=> NULL
["editor"]=> NULL
["access_level"]=> string(6) "Public"
["author_name"]=> string(12) "Mike Hermary"
["group_title"]=> NULL
["group_access"]=> NULL
["group_state"]=> NULL
["group_note"]=> NULL
["value"]=> string(42) " "
["rawvalue"]=> string(0) ""
}
Cheers,
<?php var_dump($media); ?>
statement inside yourif
block to see what the real properties are of $media at the time the condition is checked. It may not be as empty as you think.print_r
of the$video
variable to my original question. There does not seem to be any values assigned to the one selected from the blog listing. This means it should not display if empty.var_dump
for the $media custom field for reference.