nblgeoweb VIP
Total posts: 178
07 Apr 2015 13:21

Can I specify the default for a field to be a value from php? For example, I have a text field that I would like to pre-fill with the full name of the user ($user->name). I tried putting <?php echo $user->name;?> into the default parameter for the field but it didn't work.

Last Modified: 09 Apr 2015


pepperstreet VIP
Total posts: 3,837
07 Apr 2015 19:06

nblgeoweb into the default parameter for the field but it didn't work.

In backend? You can't put any code into fields and params directly. Hence the possible security issues!


You can pre-fill fields in your (custom) cobalt template.
Most likely in a copy of the default submission form or in the field input template.
Make sure you have established a user object...


nblgeoweb VIP
Total posts: 178
08 Apr 2015 02:43

Thanks Pepperstreet. I used another CCK where I could use code for field defaults but I can totally understand the security issues. Since it is only being done on the backend it shouldn't be a huge concern though. I can put it in the template. I just wanted to make sure there wasn't an easy way in the backend I could use before I modified the template.

I see I can get the default value for a field from $field->params->get('params.default_val') but how do I set this to a new default? I haven't tried to do that before. I've tried <?php $field->params->set('params.default_val','newvalue');?> and simply <?php $field->value="newvalue";?> with no luck.


danielbidala VIP
Total posts: 153
08 Apr 2015 06:14

Or you may simply use a bit of javascript in your form template php (place this code the end of template php).

fieldID = the id of your field

<?php $user = JFactory::getUser(); ?>

<script type="text/javascript">
    var currentUser = '<?php echo $user->name; ?>';
    jQuery('#fieldID').val(currentUser);
</script>
Powered by Cobalt