When using selective filters with a node reference field the nid is displayed in the filter list instead of the node title. This makes the filters useless for this field type. This is not a problem when using a regular filter type. I am using Views-7.x-3.3.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tms8707056’s picture

Status: Active » Needs review
FileSize
1.83 KB

Here is a patch for views_handler_filter_selective.inc to add titles for node_reference fields. If there is a better way to do this, please speak up as this is my first patch.

harings_rob’s picture

I have no possibility to apply the patch. Can you upload or do a codedump of the full file? so i can change the contents?

harings_rob’s picture

Status: Needs review » Needs work

I have applied the patch and it did not fix the issue, still displaying ID's instead of text.

farrington’s picture

I think the problem is relevant to all sorts of references, at least I had the same problem with terms (showed tid instead of term name).

The patch was similar to that above. Perhaps change the issue title and summary?

harings_rob’s picture

Yes i managed to edit the patch to my needs (term names etc.)

clubminsk’s picture

please, post the patch here, many people is looking for your help

harings_rob’s picture

If you could tell me how to create a patch i'll upload the file

rolfmeijer’s picture

Information on how to create patches with git can be found at http://drupal.org/node/707484

Alternatively you could post your code here (presuming it’s not too long).

harings_rob’s picture

Status: Needs work » Needs review
  function get_value_options() {
    $this->value_options = array(0 => t('Actual values will be available at run-time'));
    if (empty($this->view->selective_oids) && !empty($this->view->inited)) {
      $handler = _views_filters_selective_get_handler($this->definition['proxy']);
      $oids = $this->get_oids();
      $options = empty($oids) ? array() : call_user_func($handler, $this, $oids);
      sort($options);
      if (!empty($options)) {
        // Encode the values to keep special chars.
        $this->value_options = array_combine(array_map('urlencode', array_values($options)), array_values($options));
      }

      // Modify value_options to include title for node reference fields.
      //if (isset($this->definition['options callback']) && $this->definition['options callback'] == 'node_reference_views_options') {
        foreach ($this->value_options as $nid) {
          $result = db_query('SELECT name FROM {taxonomy_term_data} WHERE tid = :nid', array(':nid' => $nid));
          $title = $result->fetchField();
          if (!empty($title)) {
            $this->value_options[$nid] = $title;
        }
      }
        // resort results based on title
        asort($this->value_options);

        
         
      }
    //}
  }
joachim’s picture

Status: Needs review » Needs work

Please could you post changes as an actual patch file please? d.org has plenty of help resources on how to do this if you're stuck.

PapaGrande’s picture

Status: Needs work » Needs review
FileSize
1.01 KB

I needed this too so I rolled a patch after a bit of cleanup of @tortelduif's good work in #9.

joachim’s picture

Status: Needs review » Needs work

Thanks!

That helps hugely in understanding what change is proposed here.

Unfortunately, I can now see straight away that this patch won't work. The handler views_handler_filter_selective is applied to every database field that supports a filter, and not all of them are nodes.

$this->value_options could contain node ids, but it could also be user ids, term ids, or even bananas if that's the table in question. So just trying to load nodes based on them is a mistake.

As a side note, this looks wrong too:

'SELECT name FROM {taxonomy_term_data} WHERE tid = :nid',

You're not going to find node names in the taxonomy term table!

planctus’s picture

@joachim
Certainly this is not a global solution but just a temporary fix for those who are dealing with a specific need for an exposed term reference field or node reference one..
I had the first need and the code works since :nid is just a placeholder, if you look at the code at #9 in the loop the variable is defined as $nid, surely this is because the original piece of code was written for a nodereference field, but if you like, you might change it as $tid and then define :tid as the placeholder in the query, it will reduce confusion, but it won't change anything...
You're still right then, this has to be fixed for all the the references, not just one of them.
Thanks,
Da.

danielnolde’s picture

The proposed patch is a way too specific way to solve the problem.
What about taxonomy based filters on term_reference fields for a taxonomy based view?
A lot of use cases are not based on nodes in Drupal 7.
Could you generalize the solution?

Anonymous’s picture

#11 fixes the issue

sioux’s picture

FileSize
19.17 KB

#11 worked for me- sort of... see attached. I have to select Any and Apply before the filtering will work.
Is there a way to configure so that step is not necessary, and the default list of locations is displayed by default - like it was under Drupal 6.

marcoka’s picture

for information, this only solves it for taxonomy fields. if you add an integer list, it will show the keys instead of values. "selective filters" seems to be broken.

7wonders’s picture

Seems to be the same case for users. Expecting username but shows UID.

Funksmaname’s picture

#11 worked for me for a term reference field.
Thanks!

sioux’s picture

By using a combination of tips I finally have selective filtering of taxonomy terms working.
1. I (manually) patched the views filters selective file using the #11 tip above.
2. In my view, under filters, I selected content: name-of-my-vocabulary (selective)
3. I exposed the filter & made 0 selections -see tip #13, step 2 at https://drupal.org/node/1204240#comment-6792218
I have selective filtering with my taxonomy terms instead of IDs!

jpstrikesback’s picture

Not to hijack but for anyone interested in this functionality specifically for Taxonomy Terms after trying this I went in a different direction and made:

Views Term Family

It has two filters:

  • one for optionally finding related terms with a selected term (parents, children, siblings)
  • and a second that accomplishes the selective bit by looking for terms on content when building the term select options. It also allows one to add related terms to the available select options and additionally query terms related to those selected (optionally by parent, children, sibling)
kclarkson’s picture

So it seems that the patch provided is not the solution. Is this something that can be easily fixed ?

Is it as simple as changing a few variables ? If so could a nice codemaster hook this up as it is a pretty important feature for people who share a tag field among different content types?

@jpstrikesback
Is it possible to use ur code from ur sandbox module to help patch views hacks ? Also I noticed that the maintainer needs a little help, maybe u could collaborate to eliminate module duplication. Just a thought :)

jpstrikesback’s picture

Hey kclarkson,

Off the top of my head I don't think what I've done would be so helpful for patching this module, but if its taxonomy terms and restricting the dropdown to terms on content then Views Term Family has a filter for that. I don't think my module creates much duplication tho, it's higher purpose is for filtering views by a term and its nearby relations such as parent, children, sibling terms.

Cheers :)

kclarkson’s picture

@jpstikesback

Thank you for clarifying the differences. It sounds like your module is exactly what I was looking for so I will give it a go.

jpstrikesback’s picture

@kclarkson, great to hear, if you end up using it and you experience any issues please let me know :)

jlyon’s picture

This worked great for me for taxonomy term selects, however I needed it to work for select list fields as well. The attached patch adds support for that. This is a patch against views_hacks-7.x-1.0-alpha1, which seems to include #1.

John Pitcairn’s picture

Patch at #11 applies cleanly and works for me, showing taxonomy term names in the exposed filter menu.

@jlyon: please use git to create patches. Your patch is specific to your installation path and does not apply.

sirko_el’s picture

Added node titles to patch: views_hacks-show_term_titles-1608498-26.patch

sirko_el’s picture

Sorry about dpm. Deleted now.

kclarkson’s picture

Version: 7.x-1.x-dev » 7.x-1.0-alpha1

I am also confirming that patch #11 worked on 7.x-1-alpha1

@sirko_el I tried applying your patch but it said corrupt on line 12.

jonhattan’s picture

Version: 7.x-1.0-alpha1 » 7.x-1.x-dev

#28 and #29 are not full patches. They are written over #26.

kclarkson’s picture

would be great if someone could combine 26 thru 29 so that it applies cleanly and we can get it submitted.

nasir.bd’s picture

I got nid instead of title in views exposed filter.

#1 patch, It is working file for me with a little change node_reference_views_options to node_reference_views_filter_options

// Modify value_options to include title for node reference fields.

      if (isset($this->definition['options callback']) &&
        $this->definition['options callback'] == 'node_reference_views_filter_options') {
          foreach ($this->value_options as $nid) {
            $result = db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $nid));
            $title = $result->fetchField();
            if (!empty($title)) {
              $this->value_options[$nid] = $title;
            }
          }
        // resort results based on title
        asort($this->value_options);
      }

Above code just copy & paste in below code

if (!empty($options)) {
        // Encode the values to keep special chars.
        $this->value_options = array_combine(array_map('urlencode', array_values($options)), array_values($options));
      }
pearl.liang’s picture

#33 works for me as well.

infojunkie’s picture

Issue summary: View changes
Status: Needs work » Closed (won't fix)

This module has been deprecated in favour of https://drupal.org/project/views_selective_filters. Please test there and reopen if necessary.