1

I'm having a problem with a custom field type where I want to return 'some value' on the condition of there being no value in data. This works when I use a tag pair, but not when I use a single tag.

i.e.

{my_field}something here{/my_field}

will call replace_tag even when $data is empty, but...

{my_field}

will not even call replace_tag unless $data is not empty. In this case I want to force a 'default' value if the field is empty but it appears impossible.

Is this a bug or intended behaviour to improve performance? Is there some workaround I can use to trick ExpressionEngine into thinking $data has some value before the template is parsed to force it to call replace_tag on single tags?

Edit:

Trawling through the ExpressionEngine core files gives me the impression that what I'm trying to achieve is impossible though I guess there's a mild chance that an extension might be able to rescue me.

1

It does not fire if the field data is empty. You could probably use the channel_entries_row hook to modify your field value to show the default if blank. I wouldn't do this, though, as that hook will run on every channel:entries loop and can be quite expensive.

I'd suggest updating your database manually with the default value, and then change your FT's save method to add a default value if blank.

SQL to set a default value on an existing field (change XX to your actual field ID):

UPDATE exp_channel_data SET field_id_XX = 'your default' WHERE field_id_XX = '';

Save function setting a default if blank:

public function save($data)
{
    return $data ?: 'your default';
}
2
  • Thanks Rob. I coded up over the weekend an extension to do just what you suggested 'not' doing, LOL. It works but it is horribly hacky and as you point out, even with caching it's going to incur additional processing for every channel:entries row. I'll give the other idea a shot. :) Sep 22 '14 at 10:51
  • Worked a treat, when a user saves the field type settings I run that query and set the field value as 'EMPTY' which I then check for in replace_tag. Thanks again! Sep 22 '14 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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