Comment form and settings in a simple module?
Is it possible to have a comment form with settings (similar to the blog comments/settings) in a simple module or does this need to be an advanced module?
I'm using a modified version of the simple news module from the tutorial and would like to add a comments section to each article.
Comments
If you want to break it out of an advanced module, you would most likely need to replicate some of that functionality within your application folder (which is essentially what an advanced module folder is).
How do I implement the correct path to load an application/config file within a simple module? Is there another constant like MODULES_PATH? I can't figure out how to import that file in my new model.
I'm basing this on the blog posts model:
require_once(MODULES_PATH.'/blog/config/blog_constants.php');
Also, the blog settings model uses a view file to create the admin page. Is this possible in a simple module?
The comments module is more or less set up but I can't figure out how to get the settings module to run without throwing errors and notices.
I've copied/renamed all of the view and controller files from the blog folder to the application folder and attempted to make all the necessary modifications. I think my issues are mainly coming from the library and controller files (especially with the module_load method).
I realize this is likely a tough one to answer but if you could at least point me in the right direction I would be really grateful.
Getting comments implemented is the last thing I need to implement before building out the rest of the site but it's proving to be a lot more complicated that I expected.
Lastly, there is a "views" parameter and a "views_location" parameter you can set on your simple module's configuration to change the different views of the simple module as well as what module folder to pull from (e.g. in your MY_fuel_modules.php file):
...// 'views' => array( 'list' => '_layouts/module_list', 'create_edit' => '_layouts/module_create_edit', 'delete' => '_layouts/module_delete' ), 'view_location' => 'app', // app is the application folder... the default is 'fuel' ...
So I would extend Base_module_model instead of My_Module?
https://github.com/daylightstudio/FUEL-CMS/tree/demo
I have the view enabled now but I can't figure out how to pass the form_fields method to it. How would you create a form in the view?
I've been looking at the settings controller as a guide but can't get it to work. Do I need to use form_builder somehow?
you open the module (just like blog settings)?
The only way I'm able to see the form is on the create_edit view page by calling <?=$form?> from the view. I only need one view for this, as I'm not creating or deleting anything. I'm just editing.
I'm completely confused about how to do this without a controller.
I don't know if I'm going about this the right way but here is what I have in my news_comments_settings view to render the form:
<form method="post" action="" id="form"> <?php $this->load->model('news_comments_settings_model'); $field_values = $this->news_comments_settings_model->find_all_array_assoc('name'); $this->load->library('form_builder'); //print_r($this->form_builder); $field_values = $this->news_comments_settings_model->find_all_array_assoc('name'); $fields = array(); $fields['title'] = array(); $fields['description'] = array('size' => '80'); $fields['uri'] = array('value' => 'blog'); $fields['theme_path'] = array('value' => 'default'); $fields['theme_layout'] = array('value' => 'blog', 'size' => '20'); $fields['theme_module'] = array('value' => 'blog', 'size' => '20'); $fields['use_cache'] = array('type' => 'checkbox', 'value' => '1'); $fields['allow_comments'] = array('type' => 'checkbox', 'value' => '1'); $fields['monitor_comments'] = array('type' => 'checkbox', 'value' => '1'); $fields['use_captchas'] = array('type' => 'checkbox', 'value' => '1'); $fields['save_spam'] = array('type' => 'checkbox', 'value' => '1'); $fields['akismet_api_key'] = array('value' => '', 'size' => '80'); $fields['multiple_comment_submission_time_limit'] = array('size' => '5', 'after_html' => lang('form_label_multiple_comment_submission_time_limit_after_html')); $fields['comments_time_limit'] = array('size' => '5', 'after_html' => lang('form_label_comments_time_limit_after_html')); $fields['cache_ttl'] = array('value' => 3600, 'size' => 5); $fields['asset_upload_path'] = array('default' => 'images/blog/'); $fields['per_page'] = array('value' => 1, 'size' => 3); $this->form_builder->id = 'form'; $this->form_builder->label_layout = 'left'; $this->form_builder->form->validator = &$this->news_comments_settings_model->get_validation(); //$this->form_builder->submit_value = null; $this->form_builder->use_form_tag = FALSE; $this->form_builder->set_fields($fields); $this->form_builder->display_errors = FALSE; $this->form_builder->name_array = 'news_comments_settings'; $this->form_builder->submit_value = 'Save'; $this->form_builder->set_field_values($field_values); $vars = array(); $vars['form'] = $this->form_builder->render($fields); ?> <?=$this->form_builder->render($fields)?> </form>
This creates the form but I still can't get it to save. You mentioned using on_before_post earlier but I don't know how to go about this. It looks like the blog settings controller saves the data directly from the controller. Is this the approach I should take in the view?
If this is any help, when I do click save on the form the url changes from:
http://localhost/otpp/fuel/news_comments_settings/
to
http://localhost/otpp/fuel/news_comments_settings/items/offset/0
With regards to the on_before_post hook, I was referring to model hooks which can be found in the documentation at the bottom of this page:
http://www.getfuelcms.com/user_guide/libraries/my_model
$fields = $this->news_comments_settings_model->form_fields();
And then:
<?=$this->form_builder->render($fields)?>
Still can't figure out how to save it though.
I had tried creating a controller for this based on the blog_settings controller but could not get it to work. How would you go about this in a simple module for the admin section?
$this->load->module_model(BLOG_FOLDER, 'blog_settings_model'); $this->load->module_library(BLOG_FOLDER, 'fuel_blog'); $this->js_controller_params['method'] = 'add_edit'; $field_values = $this->blog_settings_model->find_all_array_assoc('name'); if (!empty($_POST['settings'])) { // format data for saving $save = array(); foreach($field_values as $field => $value) { $settings = $this->input->post('settings', TRUE); $val = (isset($settings[$field])) ? $settings[$field] : ''; $save[] = array('name' => $field, 'value' => trim($val)); } $this->fuel_blog->remove_cache(); $this->blog_settings_model->save($save); $this->session->set_flashdata('success', lang('data_saved')); redirect($this->uri->uri_string()); }
I have managed to get this to save to the db (wrong data though) and I feel I'm close to cracking this.
I think it's the $_POST['settings'] variable that I don't quite understand and how the fuel_blog library file and blog config file fits in. Do I need these for my purposes and if so, how would I set these up in the application folder structure?
<input type="text" name="settings[akismet_key]" />
I don't think you'll need the fuel_blog library or the config for what you are trying to do.
One more question on this before I completely give up on this.
There doesn't seem to be a way for me to save this in the list view. It just keeps redirecting me to .../fuel/news_comments_settings/items/offset/0 when I hit save and doesn't do anything.
You mentioned it would be easiest to create a controller for this like the blog settings controller but I don't see how this is possible in a simple module.
If this is possible, how do I set it up?
I don't know what I'm missing but I'm giving up on this route. Looks like my only option
is to try to recreate everything in an advanced module.
I need to be able to load the module in the admin nav like an advanced module
without configuring it in My_fuel_modules (it seems). Maybe I'm just really confused at this point but it doesn't seem like I can avoid the need for the list, create_edit and delete views as well as the list_items() function.