Please see #295556: Userreference with allowed values from View.

I ran into a situation when patching the CCK userreference field where I needed to use a string filter for autocomplete purposes, and found that the name field has no filter associated with it. This simply adds it to the hook_views_data implementation. We figured others may also have a use for it.

CommentFileSizeAuthor
user-name-filter.patch446 bytesCPyle

Comments

yched’s picture

Many thanks, CPyle. I'll try to actually test your patch asap.
[edit - doh, I should go to sleep - this was intended for #295556: Userreference with allowed values from View ]

merlinofchaos’s picture

Note that the UID filter already has an autocomplete field. Why add another one that will be less efficient?

CPyle’s picture

I'll describe the use case.

I made a patch to CCK Userreference to allow using views to populate the allowed users you can select. It's autocomplete functionality is already built into the widget, it expects to just provide a string to the 'potential references' function. The views version of this function gets the view normally and then simply applies the string as a filter to the name field in the query with a 'contains' or 'startswith' operator (If such a string is provided).

    //Add an instance of the name field if it doesn't exist
    $fields = $view->get_items('field', $display);
    if (!isset($fields['name'])) {
      $view->add_item($display, 'field', 'users', 'name');
    }
    if ($string !== '') {
      // Add an instance of the 'name' filter.
      $options = array('value' => $string, 'operator' => 'starts');
      $id = $view->add_item($display, 'filter', 'users', 'name', $options);
    }

If you know a better way to go about doing this that doesn't drastically change the CCK codebase, I'd defer to your judgement. My userreference patch is already adding a string filter handler to 'users'/'name' via hook_views_data_alter(). This was the method that required the fewest changes to CCK.

This is the same method CCK nodereference uses, only it applies a string filter to a node's title.

merlinofchaos’s picture

You did not answer my question, instead you told me about CCK which isn't involved in this issue. I'm very confused.

CPyle’s picture

Status: Needs review » Closed (fixed)