Fuel Pages Hooks...can I register for them?

edited April 2013 in Modules
I've written an Image Slider module. I want to be able to add multiple instances to a page and be able to reorder them. I have a "Widgets" tab that I created in the MY_Fuel_Layouts.php, but how would I go about populating that tab with a list of available widgets?

I was hoping I could do it through a module or model hook for the Fuel Pages. Is this possible?

Comments

  • edited April 2013
    This is using the 1.0 beta (I'm guessing because you mention tabs which is a 1.0 feature)? If so, try the following in your MY_fuel_layouts.php in the appropriate layout:
    .... 'widgets' => array('type' => 'multi', 'model' => 'widgets', 'sorting' => TRUE)
    This field will give you a multi select field and the sorting option will allow you to drag and drop the ordering. The end result is a set of saved IDs in order. Then in your view/layout/block (where ever you are rendering it), you can use your model's "find_within" method like so to grab those records:
    $CI->load->model('widgets'); $widgets = $CI->widgets->find_within($widgets);
  • edited April 2013
    Awesome! So, everything works except for the sorting. I get a checkbox list of my 'Widgets', but there's no drag and drop functionality. Inspecting the DOM, I don't see any draggable/droppable classes on the elements. Am I missing something?

    I have no errors on the page and I can manually add draggable/droppable to other elements.

    ---

    Ok, we must have had an error somewhere because the multi-select/sorting UI element is present now, BUT when I save the page the values aren't being saved properly. The value is always 1 in the DB. When the edit page reloads all of my selections are gone.
  • edited 3:37PM
    Add the following parameter:
    'mode' => 'multi'
    Without it, it will default to checkboxes if there are less then or equal to 5 options and the sorting doesn't work with that.
  • edited 3:37PM
    Thanks, I ended up with more than 5 items so it enabled itself, but like I said in my follow up, the value is always being stored as 1 now :s
  • edited 3:37PM
    I just pushed a change to the 1.0 beta that will for that to multi mode if "sorting" => TRUE
  • edited April 2013
    The value in fuel_page_variables is still 1, instead of ["xx", "yy", "zz"]
  • edited 3:37PM
    What are the values associated with each option? If you inspect the DOM, you should be able to find the actual select field being used to generate what's on the screen. Or you can add 'class' => 'no_combo' as an attribute to the field type and it will be its normal self. It could be that the values for each option are 1. By default, the options_list method is used on the model to generate the select options. You can specify a different method on your model by using an array instead of a string:
    'model' => array('widgets' => 'my_options_list');
  • edited April 2013
    Here is the select:
    <select name="vars--widgets_on_page[]" id="vars--widgets_on_page" class="field_type_multi field_type_multi" multiple="multiple" style="display: none;">
    <option value="34" label="Widget 1">Widget 1</option>
    <option value="32" label="Widget 2">Widget 2</option>
    <option value="29" label="Widget 3">Widget 3</option>
    <option value="28" label="Widget 4">Widget 4</option>
    </select>
    And this is what one of the <li> looks like
    <li id="vars--widgets_on_page_0">Widget 1<span style="display: none;" id="vars--widgets_on_page_0_val">0</span></li>
    Should the value in the span be the value of the select?

    When I move one of the items to the right column, the respective option gets selected="selected"
  • edited 3:37PM
    Adding the 'no_combo' class and selecting one or more values still results in a value of 1 in the database :(

    Thanks for your assistance BTW. I'm loving FuelCMS :D
  • edited 3:37PM
    No problem. Glad you like FUEL CMS.

    The span "_val" is the index to the corresponding value. The select widget looks correct as well. The next step would be to see what that looks like in the post or right before it's getting saved. To do that we'll have to get our hands a little dirty. Do you see the proper values being passed in the post if you add something like this to the fuel/modules/fuel/controllers/pages.php file right after line 117 or so?
    echo '<pre>'; print_r($_POST); echo '</pre>';

    Is there perhaps another field named "widgets" that's overwriting the value?
  • edited 3:37PM
    Looks like they're coming through correctly:
    [vars--widgets_on_page] => Array
    (
    [0] => 29
    [1] => 33
    )
  • edited 3:37PM
    OK... next step will be to see what's getting sent on save. So in the same controller, under the "_save_page_vars" method, there is a foreach loop around line 477 that loops through the fields for your layout and checks a $vars variable for a corresponding value before saving them to the DB. Are those values in that $vars variable?
  • edited 3:37PM
    in _save_page_vars this:

    if (strncmp('vars--', $key, 4) === 0)
    {
    $new_key = end(explode('--', $key));
    $vars[$new_key] = $val;
    if($key == 'vars--widgets_on_page') {
    echo '<pre>';
    echo var_export($vars['widgets_on_page']);
    echo '</pre>';
    }
    }
    outputs this:

    )array (
    0 => '29',
    1 => '33',
    )
    But when I get inside
    if (!empty($vars) && is_array($vars))
    this:

    echo '<pre>';
    echo var_export($vars['widgets_on_page']);
    echo '</pre>';
    Outputs this:
    '1'
  • edited 3:37PM
    What does the $posted variable look like? Is it perhaps overwriting the key "widgets_on_page" in the lop? Maybe there is a "vars--widgets_on_page" and a "widgets_on_page" post param?
  • edited April 2013
    There is a 'vars--widgets_on_page' => array(..) and a 'widgets_on_page' => '1' which would explain the value of '1' in the database. Any idea where that second 'widgets_on_page' key might be coming from?

    --

    There is a hidden input on the edit page form as such:

    <input type="hidden" name="vars--sorting_widgets_on_page" id="vars--sorting_widgets_on_page" value="true" class="field_type_text sorting" />
  • edited 3:37PM
    If I force $vars['widgets_on_page'] = array('28','29'); in
    if (!empty($vars) && is_array($vars)) {...}
    The values are saved correctly and loaded correctly when I edit the page again.
  • edited 3:37PM
    Based on this loop, I would assume that only the posted values that start with 'vars--' would be added to the $vars array, but it doesn't seem that way...somehow the value of '1' in the posted value 'widgets_on_page' is getting into the $vars array?

    foreach($posted as $key => $val)
    {
    if (strncmp('vars--', $key, 4) === 0)
    {
    $new_key = end(explode('--', $key));
    $vars[$new_key] = $val;
    }
    }
  • edited 3:37PM
    Can you provide the layout configuration that's having the issue in MY_fuel_layouts to see if there is anything there that be be causing the problem? It may be a hidden field but probably not the one you posted (it's a different name).
  • edited 3:37PM
    What if you change it the 4 to a 6 like so:
    foreach($posted as $key => $val) { if (strncmp('vars--', $key, 6) === 0) { $new_key = end(explode('--', $key)); $vars[$new_key] = $val; } }
  • edited 3:37PM

    $config['layouts']['residential_home'] = array(
    'fields' => array(
    'Header' => array('type' => 'fieldset', 'label' => 'Header', 'class' => 'tab'),
    'page_title' => array('label' => lang('layout_field_page_title')),
    'meta_description' => array('label' => lang('layout_field_meta_description')),
    'meta_keywords' => array('label' => lang('layout_field_meta_keywords')),
    'Body' => array('type' => 'fieldset', 'label' => 'Body', 'class' => 'tab'),
    'body_title' => array('label' => 'Body Title'),
    'body' => array('label' => lang('layout_field_body'), 'type' => 'textarea', 'description' => lang('layout_field_body_description')),
    'Properties' => array('type' => 'fieldset', 'label' => 'Properties', 'class' => 'tab'),
    '520main' => array('label' => '520 Main Summary', 'type' => 'textarea', 'description' => ''),
    '600south' => array('label' => '600 South Summary', 'type' => 'textarea', 'description' => ''),
    '251platts' => array('label' => '251 Platts Summary', 'type' => 'textarea', 'description' => ''),
    '232oakdale' => array('label' => '232 Oakdale Summary', 'type' => 'textarea', 'description' => ''),
    'Widgets' => array('type' => 'fieldset', 'label' => 'Widgets', 'class' => 'tab'),
    'widgets_on_page' => array('type' => 'multi', 'model' => 'Widgets_model', 'sorting' => TRUE, 'mode' => 'multi'),
    )
    );
  • edited 3:37PM
    Unfortunately, changing the 4 to a 6 did not help :(
  • edited April 2013
    Definitely has something to do with the 'vars--exists_vars--widgets_on_page' value being passed in?

    If I filter it out where $vars is being set, everything is fine.

    When $key is either 'vars--widgets_on_page' or 'vars--exists_vars--widgets_on_page' this end(explode('--', $key)); returns 'widgets_on_page'
  • edited 3:37PM
    Adding a limit of 2 to the explode like this
    $new_key = end(explode('--', $key, 2));
    But, this is likely going to cause a problem elsewhere. The $vars array ends up with a 'widgets_on_page' key and a 'exists_vars--widgets_on_page' key.
  • edited 3:37PM
    Ah... OK... let me see what I can do here.
  • edited 3:37PM
    OK... I think I have another solution although I believe yours will work as well. If you go to the fuel/modules/fuel/libraries/Fuel_custom_fields.php on line 1120 change:
    'exists_'.$params['name'];
    TO
    'exists_'.$params['orig_name'];
    does that work?
  • edited 3:37PM
    That seems to work! Thanks so much :)
  • edited 3:37PM
    I'll post that as a fix so you can pull from it later today.
Sign In or Register to comment.