Displaying tags in modules

j9cj9c
edited October 2015 in Modules
Hi! I'd like to learn how to display tags only of the category selected when creating an entry in a module.

I have a module that lets the user add merchandise. I have created a merchandise category. It has subcategories like apparel, electronics, etc. When creating an entry, the module should list these subcategories. Each subcategory has multiple tags. I'd like the module to display tags only of that subcategory. If the user selects the apparel subcategory, the tags that would be listed would be only of those from the apparel subcategory.

Is there a way to do this?

Comments

  • edited 9:39AM
    This sounds like a a dropdown that needs to be dynamically created based on the values of another dropdown correct? If so, check out the dependent field type:
    http://docs.getfuelcms.com/general/forms#dependent
  • j9cj9c
    edited 9:39AM
    I've tried this but it doesn't work. Am I missing something?

    $fields['tags'] = array('type' => 'dependent', 'depends_on' => 'category_id', 'url' => fuel_url('modules/fuel/tags/ajax/options'));
  • edited 9:39AM
    Try changing the url value to:
    fuel_url('tags/ajax/options')
  • j9cj9c
    edited 9:39AM
    Thank you so much admin! It works now :)
  • j9cj9c
    edited 9:39AM
    I just encountered a bug using this method. It doesn't update the category or tag at all. Whenever I make a new entry in the module and set a category and tag to it, it's supposed to come out of the view. After implementing the code though, it doesn't.
  • edited 9:39AM
    Can you post example to help explain your issue?
  • j9cj9c
    edited 9:39AM
    I have a category Apparel. Under Apparel I have two tags: Men and Women.

    In my merchandise module, I have two dummy data. Data A is categorized under Apparel and under the tag Men. Data B is under category Apparel and tag Women. Data A was created before I implemented the code above. Data B is created after I implemented the code above. I have a view which retrieves items under a certain category or tag which searches based on the uri. If I go to the url "www.site.com/merchandise/apparel/men" it will show Data A. But if I go to the women's section (url would then be "www.site.com/merchandise/apparel/women" Data B does not show at all. Even if I just go to the Apparel category, Data A will show but Data B will not. If I edit Data A, the changes I made won't take effect at all, so if I change Data A to have the Women tag, it will still come out having the Men tag.
  • edited 9:39AM
    How are you apparel data for www.site.com/merchandise/apparel/men and www.site.com/merchandise/apparel/women?
  • j9cj9c
    edited 9:39AM
    If I understand your question correctly, you're asking how I get the data for the categories, yes?

    Here is my code:

    $slug = uri_segment(4); if($slug) { $product = fuel_model('merchandises', array('where' => array('slug' => $slug))); if(!empty($product)) { foreach($product as $p) { echo '<div id="product-info">'; echo '<div id="product-image">'; echo '<img src="' . img_path('merchandise/' . $p->image) . '"></img>'; echo '</div>'; // right side echo '<div id="product-details">'; // product name echo '<div id="product-name">' . $p->name . '</div>'; // price echo '<div id="product-price">' . $p->price . '</div>'; // size echo '<div id="product-name">' . $p->size . '</div>'; // color echo '<div id="product-name">' . $p->color . '</div>'; echo '</div>'; echo '</div>'; echo '<div id="product-description">'; echo $p->description; echo '</div>'; } } else { redirect_404(); // echo "no product"; } } else { // subcategory page $slug = uri_segment(3); if($slug) { $tags = fuel_model('tags', array('where' => array('slug' => $slug))); if(!empty($tags)) { foreach($tags as $tag) { foreach($tag->merchandises as $i) { echo '<div class="item-box">'; // image echo '<div class="item-image">'; echo '<img src="' . img_path('merchandise/') . $i->image . '"></img>'; echo '</div>'; // info echo '<div class="item-text">'; echo $i->name; echo '</br>'; echo 'Php ' . $i->price; echo '</br>'; echo $i->color; echo '</div>'; echo '</div>'; } } } else { redirect_404(); // echo "no subcategory"; } } else { // category page $slug = uri_segment(2); if($slug) { $category = fuel_model('categories', array('where' => array('slug' => $slug))); if(!empty($category)) { foreach($category as $cat) { $tags = fuel_model('tags', array('where' => array('category_id' => $cat->id))); foreach($tags as $tag) { foreach($tag->merchandises as $i) { echo '<div class="item-box">'; // image echo '<div class="item-image">'; echo '<img src="' . img_path('merchandise/') . $i->image . '"></img>'; echo '</div>'; echo '<div class="item-text">'; echo $i->name; echo '</br>'; echo 'Php ' . $i->price; echo '</br>'; echo $i->color; echo '</div>'; echo '</div>'; } } } } else { redirect_404(); // echo "no category"; } } else { // main merchandise page $all = fuel_model('merchandises'); if(!empty($all)) { foreach($all as $i) { echo '<div class="item-box">'; // image echo '<div class="item-image">'; echo '<img src="' . img_path('merchandise/') . $i->image . '"></img>'; echo '</div>'; // info echo '<div class="item-text">'; echo $i->name; echo '</br>'; echo 'Php ' . $i->price; echo '</br>'; echo $i->color; echo '</div>'; echo '</div>'; } } else { } } } }
  • edited 9:39AM
    Sometimes it helps to see the actual query being run on the model. There is a debug_query() method I like to use on the model directly. To do so, after this:
    $tags = fuel_model('tags', array('where' => array('slug' => $slug)));
    Try running and see what it returns:
    CI()->merchandises_model->debug_query();
  • j9cj9c
    edited 9:39AM
    It just prints out "SHOW COLUMNS FROM `merchandise`"
  • edited 9:39AM
    This may sound weird but try running it twice (the first time it should cache the columns):
    $tags = fuel_model('tags', array('where' => array('slug' => $slug))); $tags = fuel_model('tags', array('where' => array('slug' => $slug))); CI()->merchandises_model->debug_query();
  • j9cj9c
    edited 9:39AM
    It's still not working and shows "SHOW COLUMNS FROM `merchandise`"
  • edited 9:39AM
    Try placing it in the foreach loop:
    foreach($tag->merchandises as $i) { CI()->merchandises_model->debug_query(); ....
  • j9cj9c
    edited 9:39AM
    Hi! Sorry I haven't tried that solution. That feature was scrapped from the system :(
Sign In or Register to comment.