It looks like you're new here. If you want to get involved, click one of these buttons!
function form_fields($values = array())
{
$related = array('documents' => 'documents_to_pages_model');
$fields = parent::form_fields();
$fields['date_added']['type'] = 'hidden';
// ******************* NEW RELATED DOCUMENT FIELD BEGIN *******************
$CI =& get_instance();
$CI->load->model('documents_model');
$CI->load->model('documents_to_pages_model');
$document_options = $CI->documents_model->options_list('id', 'title', array('active' => 'yes'), 'title');
$document_values = (!empty($values['id'])) ? array_keys($CI->documents_to_pages_model->find_all_array_assoc('document_id', array('page_id' => $values['id']))) : array();
$fields['documents'] = array('label' => 'Documents', 'type' => 'array', 'class' => 'add_edit documents', 'options' => $document_options, 'value' => $document_values, 'mode' => 'multi');
// ******************* NEW RELATED DOCUMENT FIELD END *******************
$yes = lang('form_enum_option_yes');
$no = lang('form_enum_option_no');
$fields['cache']['options'] = array('yes' => $yes, 'no' => $no);
return $fields;
}
Comments
The primary key of the fuel_pages table is 'id' so I'm puzzled. If I hard code the value of $values['id'] in the find_all_array_assoc() then everything works as expected (eg set the value as '11' where the url is /fuel/pages/edit/11).
Also, the on_after_save function is working and that does utilise values['id']:
function on_after_save($values) { $data = (!empty($this->normalized_save_data['documents'])) ? $this->normalized_save_data['documents'] : array(); $this->save_related('documents_to_pages_model', array('page_id' => $values['id']), array('document_id' => $data)); }
so I'm concluding no array "$values" is passed to form_fields() - hence the default empty array argument used in the form_fields() code above is the reason there is never a 'id' to send to the "find_all_array_assoc()" ?
For want of anything else, I can get the page id using:
$this_id = (is_numeric($this->uri->segment($this->uri->total_segments()))) ? $this->uri->segment($this->uri->total_segments()) : NULL;
On a related matter, if I wanted to retrieve the id of the fuel_page in a particular view or block, what would be the best method?
$fields = $this->model->form_fields($saved);
This will be fixed in the next major release.
For the pages_model, the "pages" actually does reference the fuel_pages table because the __construct method in base_module_model references the config parameter "tables" which is an array of aliases to tables.
$location = uri_path(); $CI =& get_instance(); $CI->load->module_model(FUEL_FOLDER, 'pages_model'); $page = $CI->pages_model->find_by_location($location); $pid = $page['id'];
but maybe that's a bit verbose. I think there is probably a means of doing the same but with fuel_model(), but I haven't tried yet.
On the bright side I've achieved what I wanted to do, create a document attachment to fuel_pages. To round it off nicely I would find a way of making the combo boxes for the attachment process appear last on the admin page (eg after the layout variables), but I think that would require more tinkering with the fuel admin views - it's not something that can be shuffled around with the form_fields() configuration.
To have the fields appear last, you can set the "order" field parameter to something over 1000. By default, published/active are set to like 999 so they will appear last:
$fields['documents'] = array('label' => 'Documents', 'type' => 'array', 'class' => 'add_edit documents', 'options' => $document_options, 'value' => $document_values, 'mode' => 'multi', 'order' => '10000');
I wanted to set the documents field last in the entire "edit/create" form, but it is only possible to make it last of the "page information" section, using the "order" key (I think this is what you intended with your suggestion).
Conversely, if I set
$fields['page_title']['order'] = 1;
in form_fields() for pages_model.php, then the page variable field "page_title" does shift position (to the 3rd row in the form) but then causes an error
There is an error with this layout because it contains one or more of the following reserved words: location, layout, published, cache, date_added, last_modified, last_modified_by, page_title, documents
which breaks the "layout variables" section.
Looks like the page variables cannot be re-ordered either. The form_fields() mustn't use keys present in page variables.
That warning appears because you still have the "documents" form field as part of the pages module and that is checked to prevent conflicts on submit between data for the pages module and data for the page variables module.