I seem to be having some issues with my custom theme node.tpl files.

Where I have a custom field I use code like this:

<?php if (isset($content['field_myfield'])) { ?>
  <div class="myfieldclass">
    <?php print render($content['field_myfield']); ?>
  </div>
<?php } ?>

This obviously works on Drupal and has (afaik) been working up to now on my Backdrop sites.

Unfortunately now, when the field is empty I am still getting the markup on the page, where I should be seeing nothing:

<div class="myfieldclass">
</div>

Unsure if something has changed in core or if I've been doing it wrong. Could someone point out what the issue might be please?

Accepted answer

Okay I've managed to locate the issue here.

My config files were messed up.

The "field.instance.node.nodetype.field_fieldname.json" files had this setting for the default value (where no default value had been set in the field config):

"default_value": [
        {
            "value": "",
        }
    ],

This appears to cause rows in the relevant database table to be inserted with a blank value column. My node template code returns positive because the row exists, although without value.

Upon checking with a fresh install of Backdrop the config files should have this code:

"default_value": null,

Not sure how the config files had got to this state, but hopefully this might help someone else in the future.

Comments

Just off the top of my head, $content['field_myfield'] is a render array (indicated by the fact that you call render() on it), which means there's a lot more information in that array than just the value of the field. Therefore isset() will possibly still return TRUE even when the field value is empty (there might be field prefixes, etc. set in the array). I suspect there's a better way to check if the field has a value before rendering it, but I'm not sure (can't remember doing this in Backdrop recently). Maybe others can shed more light...

mazze's picture

Hi, I never used the isset, but the !empty string.

This works in my templates:

    <?php if(!empty($content['field_category'])): ?>
    <div class="uk-text-small"><?php print render($content['field_category']); ?></div>
    <?php endif; ?>

Thanks, but I'm afraid your code doesn't work for me either.

Not sure what's going on here, but Node Reference fields appear to work correctly with my code.

This means your field value is empty. Can you use Devel and post the output here?

    <?php dpm($content['field_myfield']); ?>

Okay I've managed to locate the issue here.

My config files were messed up.

The "field.instance.node.nodetype.field_fieldname.json" files had this setting for the default value (where no default value had been set in the field config):

"default_value": [
        {
            "value": "",
        }
    ],

This appears to cause rows in the relevant database table to be inserted with a blank value column. My node template code returns positive because the row exists, although without value.

Upon checking with a fresh install of Backdrop the config files should have this code:

"default_value": null,

Not sure how the config files had got to this state, but hopefully this might help someone else in the future.