Best Way to Iterate through Categories...?

edited December 2013 in News & Announcements
What would be the best way to iterate through parent categories, then look up tags that belong to the child categories etc.?
And then look up pages that have one or more of those tags?

How much can be done through framework vs direct select query?

Using the generic tags and categories modules.


  • edited 2:42PM
    Some of this is explained here:

    However, the last part, tying it all back to the pages_model isn't directly supported out of the box but can be accommodated by overwriting the fuel_pages_model. Here's how to do that:

    1. Add the following to the MY_fuel_modules.php file to allow us to overwrite the model associated with the Pages module:
    $config['module_overwrites']['pages'] = array('model_name' => 'my_pages_model', 'model_location' => 'app');

    2. Create the file "my_pages_model" file in the fuel/application/models folder. Note that we've extended the fuel_pages_model but just added the $has_many relationship. This will add a Tags field when creating a page:
    require_once(FUEL_PATH.'models/fuel_pages_model.php'); class My_pages_model extends Fuel_pages_model { public $has_many = array('tags' => array('model' => array(FUEL_FOLDER => 'fuel_tags_model'))); }

    3. The tags field doesn't look great so I would suggest creating a new custom tag field to handle that by using something like select2. More on custom fields can be found here:
    To use that go to the following link and download the code:

    4. Rename the downloaded folder to just say "select2" and put it in the assets/js/ folder

    5. Move the select2.css file and put it in a assets/css/select2/ folder

    6. Open up the select2.css file and replace all url(...) image paths to user "../../js/select2/" instead

    7. Now we will create a class to house the code for creating the custom field. To do that create a class called MY_custom_fields and put it in the fuel/application/libraries/ folder:
    class MY_custom_fields { function tags($params) { $form_builder =& $params['instance']; $js = '<script type="text/javascript"> $(function(){ $(".field_type_tag").select2(); }) </script> '; $form_builder->add_js($js); $params['class'] = (!empty($params['class'])) ? $params['class'].' field_type_tag' : 'field_type_tag'; $params['type'] = 'select'; $params['multiple'] = TRUE; return $form_builder->create_select($params); } }

    8. Lastly, create the associations for the custom field by adding the following to the fuel/application/config/custom_fields.php configuration file:
    $fields['tags'] = array( 'class' => 'MY_custom_fields', 'function' => 'tags', 'css' => 'select2/select2', 'js' => array( 'select2/select2', ), 'represents' => array('name' => 'tags'), );

    I know it seems like a lot of steps and it may be something we will try and improve upon in the future, however, this should allow you to make the appropriate associations you want to pages using tags.
  • edited 2:42PM
    The 'tags' in the $has_many causes the error "There is an error with this layout because it either doesn't exist or contains one or more of the following reserved words: location, layout, published, cache, date_added, last_modified, last_modified_by, tags, language, navigation_label" to be thrown.

    Changing the name addresses that - I went with "Page Tags"

    I'm going for a series of checkboxes, allowing multiple tags per page; but the select dropdown method might prove useful to someone else.

    I'm exploring changing it into a multi field.
  • edited 2:42PM
    I ended up going another direction; I'm pretty new to Fuel itself, and I may find better ways to do things.
    But for now this was easier:
  • edited 2:42PM
    Now I just need to code out pulling up the page previews/page links for content with specific categories/tags
  • edited 2:42PM
    That error shows up probably because the Layout variables has a "tags" field in MY_fuel_layouts.php
Sign In or Register to comment.