1

I have a view that displays all reviews written by a user. One piece of information that is included in these reviews is the band that the user was playing in at the venue that the review is for. So, I have an exposed filter on the view that allows people to filter reviews by band name.

enter image description here http://www.gigindie.com/users/jhegg23

However, the exposed filter renders as a text field. I'd like to change this into a select list containing the currently viewed user's bands. So, instead of the user typing in the name of the band that they want to filter the reviews by, they can instead select a band from the list. I know that I can alter the form type within a custom module, so that it renders instead as a select box. However, I also need to populate the options of the filter with the names of the bands that the user is in.

Bands are a content type of their own. Is there a way to do this, with maybe a view or something?

EDIT - As requested in the below comment: It's D7, and it's a view of content (filtered by content type: review). The view is a block appearing in the user's profile, with a contextual filter for author UID from the page url. And yes, there is an entity reference. The user creates their bands, and then when they write a review, they select a band, which creates a reference in the review to the band.

  • Hi, is it Drupal 7 ? Is it a view of users or content ? And finally, when you say that bands are content types of theiry own, do you mean that you have entityreference between review and bands ? Thx. – Gregory Kapustin Dec 14 '12 at 22:03
  • Yes it's D7, and it's a view of content (filtered by content type: review). The view is a block appearing in the user's profile, with a contextual filter for author UID from the page url. And yes, there is an entity reference. The user creates their bands, and then when they write a review, they select a band, which creates a reference in the review to the band. – Mrweiner Dec 14 '12 at 22:58
0

Hum, If you're ok to build a module, you have to alter the form, like described here for example. Maybe there's a better way with hook_views_post_render.

But in all cases you'll have to build an options array. Thus you can do a db_select :

$result = db_select('field_reference', 'n')
->condition('bundle', 'band')
->fields('n', array('entity_id', 'field_band_nid'))
->execute()
->fetchAll();

$options = array();

foreach($result as $band) {
  $theband = node_load($band->field_band_nid);
  $options[$band->entity_id] = $theband->title;
}

I know, the node_load is a bit heavy, but I can't see how you can have the band title in the select list otherwise...

Careful, I didn't test it :)

If you don't want to build a module for that, there's an "ugly" way to do it : build a block with PhP filter, and create a simple HTML form with a dropdown menu created like this, and that leads to the same page, with a new argument like ?band_nid='n', and use it in a second contextual filter (with the field entity_reference).

Sorry for the mess, i hope other people have cleaner ways solve your problem.

  • Yeah I figured I'd have to build a module...that's no problem. I just had no idea how to go about filling the options array. Maybe that would have been a better way to phrase the question. :) I'm assuming your reference to node_load is that it has to pull every node, right? Is there any way to "filter" the node_load based on the current page? Can you pull url variables within modules? – Mrweiner Dec 14 '12 at 23:58
  • It's not actually every node, it's every node that's been found in the entity reference field table. But it's still more than the ones we have to display... Unfortunately, as your user is not necessarily the one who authored the band node, we'll have to do many queries to narrow this down... 1. Get all his authored reviews ; 2. Check for ones who have band reference field field ; 3. Get those nids ; 4. Make the above query. – Gregory Kapustin Dec 15 '12 at 0:07
  • Oh btw, I don't know if that form_alter has access to the view->args, in this case the user id we need to narrow down the node_load. I would try a dpm($form) and a dpm($view) to check it. If not, we'll have to go to hook_view_pre_render . – Gregory Kapustin Dec 15 '12 at 0:15
0

Views Reference Filter can be helpful. I you have to create a ER view of node title and then you can use this ER view in your main view exposed filter of type reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.