#1 February 24, 2015 8:51pm

pier
Member
Registered: February 12, 2015
Posts: 17

Filter Many to Many field in list view

I need to add categories to a module with a M2M field. I'm not using the included tags because those categories have to be multilingual.

The M2M field works fine, problem comes with list views. I'd like the user to be able to filter table items based on those categories. For example in a table of images filter "family photos", or "travel photos".

Is this possible?

Maybe someone here can recommend me a better way to solve than using a M2M field.

Thanks in advance

Last edited by pier (February 24, 2015 8:56pm)

Offline

#2 February 25, 2015 11:25am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Filter Many to Many field in list view

Unfortunately there's no easy way to accomplish that. What I would do to sort of add that kind of functionality is hack it in view view parsers and search. Basically, add a column to your view that has data you wouldn't normally use and then use a view parser to fill that column with the related category names. Something like this:

$categories = array();
$q = sqlquery("SELECT name FROM product_categories JOIN product_category_relationships ON product_categories.id = product_category_relationships.category WHERE product_category_relationships.product = '".$item["id"]."'");
while ($f = sqlfetch($q)) {
    $categories[] = $f["name"];
}
$value = implode(", ",$categories);

The more complicated way to do this is to build out your own custom view (by placing a default.php file in /custom/admin/modules/whatever-your-module-route-is/). If you're interested in that more advanced route let me know -- I've done it for an product / category relationship where you can filter based on categories, but it's definitely a much more complicated approach!

Offline

#3 February 25, 2015 11:46am

pier
Member
Registered: February 12, 2015
Posts: 17

Re: Filter Many to Many field in list view

Basically, add a column to your view that has data you wouldn't normally use and then use a view parser to fill that column with the related category names.

And would the search box work over that new "fake" column too?

Offline

#4 February 25, 2015 11:48am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Filter Many to Many field in list view

Yep, since everything is cached in bigtree_module_view_cache even though that data isn't coming from that column it's still searchable (since we run that view parser and insert the data returned in $value in the cache). Basically the search field searches the cached data columns, not the actual data columns.

Offline

#5 February 25, 2015 11:54am

pier
Member
Registered: February 12, 2015
Posts: 17

Re: Filter Many to Many field in list view

Thanks again for your help Tim.

Do you have a tutorial for the custom view thing?
Do you think it would be possible to implement a thumbnail in a custom view?

Offline

#6 February 25, 2015 12:02pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Filter Many to Many field in list view

There's no custom view tutorials, unfortunately. There's a variety of ways you can go about making them -- when you create a custom admin page you're given freedom to do whatever you want really, so you have a blank slate. You just write code and it gets dumped into the admin's layout.

You can take a look at how some of the views are done in /core/admin/auto-modules/views/ ad /core/admin/ajax/auto-modules/views/.
We hope to build out some nice documentation for building your own views in the coming year as one of BigTree 4.3's planned features is the ability to plug-in custom view types that can be shared with the community as extensions.

Offline

#7 February 25, 2015 12:21pm

pier
Member
Registered: February 12, 2015
Posts: 17

Re: Filter Many to Many field in list view

Thanks, I'll check that!

Seems like BigTree will be my CMS of choice for the following years!

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.