How do you set default values for Template Field (FuelCMS 1.0)

I'm using the Template field and everything for the 'add' portion is working great, multiple items are being inserted into the right tables, all data is saved correctly...the relevant code below.

$fields['date_ranges'] = array( 'display_label' => TRUE, 'type' => 'template', 'label' => 'Date Ranges', 'non_sortable' => TRUE, 'fields' => array( 'Dates' => array('class' => 'no_editor datepick' ), ), 'add_extra' => TRUE, 'repeatable' => TRUE );

My question is about when I am editing. How do I get the multiple instances of the template to display and work like it does during 'add'. I don't want to put them in 'fields' because then clicking 'add another' will duplicate all the fields and they won't be stored in the same array when submitted.

Any help would be appreciated.

Comments

  • edited 11:47PM
    I'm not quite sure I follow. Are you wanting a scenario where you select a something from say a dropdown and it changes the fields below it? If so, there is a "block" field that can be used to select from a list of Fuel block layouts which act as sub layouts.
  • edited 11:47PM
    The code is for an Advanced Module, and it is inside of the form_fields function for a model. It allows multiple date ranges to be entered when adding a record. The date ranges are saved correctly in the database, but I am unsure how to edit those same date ranges. How do I pass the data ranges from the database back to this form field?

    For most form fields I would set the 'value' with a string or array. But I don't see how to create and populate the template field values when editing a record.
  • edited 11:47PM
    When you say saved correctly, is the data serialized into a single text field? If so, did you set the field as a serialized in the model property like so:
    public $serialized_fields = array('dates');

    Another option is to either overwrite the find_one_array method on your model (inherited from the MY_Model class), or create a new method on your model to return the data in a specific format and specify the "edit_method" for the module in your MY_fuel_modules.php config file like so:
    $config['modules']['news'] = array( 'edit_method' => 'my_find_one_array', .... );
    Does that make sense?
  • edited 11:47PM
    The data is an array containing each of the dates entered which are captured in the on_before_clean function and saved to a different table.

    Maybe I am not using the Template type or add_extra/repeatable options correctly or have different expectations of how it should work.

    But if I am able to create a form field that I can duplicate and add content to within the CMS, then capture this dynamically added form field on submit. I assumed there was an easy way on the editing of that form, to show the fields in the same way as when they were created.

    I will go back and look at how I am handling the user being able to enter an unlimited number of date ranges and possibly re-work how it is done.

    Thanks for your time.
  • edited 11:47PM
    Normally, we'll save that information to a big text field as serialized data. However, if you are saving it in another table, you'll need to modify the method used to retrieve the values to edit. The default method on the model is "find_one_array". You can either overwrite that method or create your own and then change the "edit_method" mentioned earlier. In that method, you could do something like the following (not tested so just look at it as an example):
    function my_find_one_array($where, order = NULL){ $data = parent_find_one_array($where, $order); $CI =& get_instance(); $CI->load->model('my_other_model'); $related_dates = $CI->my_other_model->find_all_array(array('foreign_id' => current($where)); $data['dates'] = $related_dates; return $data; }
    That Template field is looking for an array of associative arrays for it's value.
  • edited 11:47PM
    Makes total sense now. Thanks for the help. Will report back how it worked for me.
  • edited June 2013
    I can't believe how hard I was trying to make it. With just the one extra line of code it is working exactly how I wanted. :)

    For completeness sake if anyone else is looking to do multiple instances of dates (or anything really) the model code is below (pseudo code really, cleaned out unnecessary stuff for the example):

    At the top of the model:
    public $serialized_fields = array('date_ranges');

    In the table for this model, I have a 'text' field called date_ranges that is used to store the data in a serialized array. I am not using the built in date field because I wanted to use the jquery datePick plugin that allows ranges within a single text box.
    function form_fields($values = array()) { $fields = parent::form_fields(); $fields['date_ranges'] = array( 'display_label' => TRUE, 'type' => 'template', 'label' => 'Date Ranges', 'non_sortable' => TRUE, 'fields' => array( 'Dates' => array('class' => 'no_editor datepick'), ), 'repeatable' => TRUE ); $this->form_builder->set_fields($fields); return $fields; }

    I need the dates in a separate table for searching purposes, so before the record is updated:
    function on_before_clean($values) { //remove existing dates from dates_table $this->db->query('delete from dates_table where foreign_id = '.$values['id']); foreach($values['date_ranges'] as $date_range){ $dates = explode(" - ", $date_range['Dates']); $this->db->query('insert into dates_table (foreign_id, date_start, date_end) values ('.$values['id'].',"'.$dates[0].'","'.$dates[1].'")' ); } return $values; }
Sign In or Register to comment.