0

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,

5
  • If you add a <?php var_dump($media); ?> statement inside your if 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.
    – Irata
    Commented Nov 21, 2023 at 21:30
  • @Irata I have added a 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. Commented Nov 22, 2023 at 2:07
  • The condition block is checking $media, not $video, so you need to print_r($media) rather than $video.
    – Irata
    Commented Nov 22, 2023 at 9:16
  • @Irata $video and $media are both custom fields, and are encountering the same issue. I have included the var_dump for the $media custom field for reference. Commented Nov 22, 2023 at 18:01
  • @Irata After additional investigation, it looks like the custom field's value has whitespace stored when saving the article in Joomla. This applies to the media and video custom fields. The rawvalue is stored correctly. Commented Nov 22, 2023 at 18:22

2 Answers 2

1

In your Joomla 3 example you are checking for the existence and contents of a specific property, value, in the &media array.

In your Joomla 4 example you are checking for the existence and content of the whole object $media, not just a single property, therefore the condition is always going to be true. Yes $media exists and yes it is not empty.

However even though the value attribute has a space stored, your condition !empty($media) will always be true because the $media has all these other aspects of the object stored in it.

You should refine your condition to something like !empty($media->value)

Your code would look like this

<?php if(isset($media) && !empty($media->value)) : ?>
  <!-- Media custom field output -->
  <div class="media-block">
    <?php echo $media; ?>
  </div>
  <!-- Media custom field output -->
<?php endif; ?>
<?php var_dump($media); ?>
2
  • I tried that revision, but it did not work. When it was added to the conditional like if(isset($media->value) && !empty($media->value)) nothing is rendered in the <!-- Media block --> area on the page. Commented Nov 23, 2023 at 21:36
  • I have updated my answer to be clearer and included the var_dump to see what the actual properties are of $media at the time it is executing. If you removed the 'white space', that you mention in your earlier comment, from the property value then I expect your above condition to be true, $media->value is empty do not execute block.
    – Irata
    Commented Nov 23, 2023 at 22:48
0

In your Joomla 3 example you are checking for the existence and contents of a specific property, value, in the &media array.

In your Joomla 4 example you are checking for the existence and content of the whole object $media, not just a single property, therefore the condition is always going to be true. Yes $media exists and yes it is not empty.

However even though the value attribute has a space stored, your condition !empty($media) will always be true because the $media has all these other aspects of the object stored in it.

You should refine your condition to something like !empty($media->value)

Your code would look like this

<?php if(isset($media) && !empty($media->value)) : ?>
  <!-- Media custom field output -->
  <div class="media-block">
    <?php echo $media; ?>
  </div>
  <!-- Media custom field output -->
<?php endif; ?>
<?php var_dump($media); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.