Comment form and settings in a simple module?

edited November 2011 in Modules
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.


  • edited 1:49AM
    The blog comments and settings modules could indeed be used as simple modules but they are bundled under a folder to keep all the MVC functionality regarding it together (comment view, processing of comments, etc).

    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).
  • edited 1:49AM
    OK, thanks. I'll dig into this and see what I can do :)
  • edited 1:49AM
    Having a couple of issues...

    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:

    Also, the blog settings model uses a view file to create the admin page. Is this possible in a simple module?
  • edited 1:49AM
    Well I figured out it was APPPATH I was looking for but now News_base_controller is throwing call to a member function on a non-object errors.
  • edited 1:49AM
    Could you give me an idea of where to start with getting the settings module to work as 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.
  • edited 1:49AM
    The blog settings module is a little different and actually uses a controller to save the information. However, I think it can be replicated by manipulating your form_fields method on your settings model to display all the appropriate setting fields the way you want. And then, you can use an on_before_post model hook to format the data into an array of records (similar to the blog settings controller), which can then be saved.

    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' ...
  • edited 1:49AM
    This is starting to make a bit more sense now.

    So I would extend Base_module_model instead of My_Module?
  • edited 1:49AM
    Yes (and I think you mean MY_Model right)? MY_Model is a basic extension of the Model class and Base_module_model extends MY_Model for things specific to FUEL.
  • edited 1:49AM
    Also, you may want to look at the demo branch for an example site if you haven't already:
  • edited 1:49AM
    Yes, sorry, I meant model and thanks for the link.

    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?
  • edited 1:49AM
    A model's form_fields() method returns back an associative array that can be used by Form_builder to create your forms. So in short, yes, if you are wanting to use a model's form_fields method, you will need to use it with form_builder to render the form.
  • edited 1:49AM
    Is it possible to have the form appear on the first page that you land on when
    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.
  • edited 1:49AM
    The blog actually uses a controller because it doesn't follow the normal select from list and edit flow. Did you try changing the modules "views" => "list" parameter mentioned above?
  • edited 1:49AM
    Yup, I did try the list parameter and wasn't able to render a form in that view until this morning.

    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:



  • edited 1:49AM
    Hmm... in this case it may actually be cleaner if you create controller to save just like the blog settings page. Otherwise you wind up creating more code to work around it then simply just creating a controller to handle it. The default module that most of the modules get routed through, have the normal list, create/edit views and in this case, we don't have that.

    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:
  • edited 1:49AM
    I was able to render the form based on the model's form_fields in the list view this way:
    $fields = $this->news_comments_settings_model->form_fields();

    And then:

    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?
  • edited 1:49AM
    I think this code from the blog_settings controller might be what I'm looking for...
    $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?
  • edited 1:49AM
    Basically, that code grabs all the blog settings and loops through them. While looping through it checks the $_POST['settings'] variable and adds it to the $save array. The settings form uses an array syntax for the field names so when it's posted, it comes through as an array like so:
    <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.
  • edited 1:49AM
    I see. Thanks for your patience with this.

    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?
  • edited 1:49AM
    You are right in that simple modules rely on the fuel/modules/fuel/controllers/module.php controller to do all the work. However, you can create a controller whether it be an advanced module controller that exists in it's own module folder or you can create your own controller in the application/controllers directory. As long as it inherits from the Fuel_base_controller like the module controller does ( and like the blog settings controller), it should still work.
  • edited 1:49AM
    Thanks for all your help and patience with this but I cannot get this to work.

    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.
Sign In or Register to comment.