5

I have a view that displays matching nodes, based on contextual arguments.

I'd like to create an exposed filter that filters by node title, displaying a drop down (select) menu with options for every node in the result set.

I can modify / set the contents of the exposed filter using hook_form_alter, but the views query hasn't executed when this hook is called, so I don't have access to the result set. If I use hook_views_post_execute then I have access to the result set, but the exposed filters form has already been rendered.

I know that I could write an EntityFieldQuery to select the nodes and set them within hook_form_alter, but that isn't very DRY as I would effectively be re-writing (and executing) the same query that Views will be executing anyway.

What's the best practice way to achieve what I'm looking for?

2 Answers 2

0

Since this has gone unanswered, I decided to get creative and it's very easy with Entity Reference, Views Reference Filter and optional Rules.

  1. Add an entity reference field to the content type that will reference its own node. So node/5's field will reference node/5. This can be done with Rules automatically. Old nodes can be updated with VBO if needed.
  2. In your view, Add > Entity Reference with the exact same filters etc. That should be automatic. Remove unwanted fields. Save the view.
  3. Go back to your regular view, add an exposed filter (only for that pane) for the entity reference field we just created with (reference filter) written beside it.
  4. In the exposed filter's settings, choose the Entity Reference view we just created. If it's just using contextual filters, don't worry about sending arguments.

The logic: The view will process the entity reference view first and provide its results to the first view as an exposed filter. I just tested it and it works perfectly. Takes about five minutes.

0

The closest solution to this with core Views is to use the Summary option of a Contextual filter, but use it in a separate Views Display of type Attachment.

In your case to show title it may be the Title contextual filter, or node ID contextual filter set to display the title instead of the ID (use Relationship if necessary, I haven't tested this).

The Attachment will display along with the results, but because of the Contextual filter set to Summary it will work like an Exposed filter, showing all the options from the results set.

You may need to tweak the appearance of the Summary to make it look more like a drop-down or something you had in mind, by default it will be a list of links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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