I have not seen this brought anywhere else, but it would be really useful if you could populate the allowed values list in a Userreference with a View of users.

Nodereference has this, and it doesn't seem like it would be a major leap for Userreference to have it as well.

Comments

yched’s picture

True - patch welcome.

criz’s picture

subscribed

CPyle’s picture

Assigned: Unassigned » CPyle

Code is finished and in testing. Needs commenting and I will submit the patch.

CPyle’s picture

Status: Active » Needs review
StatusFileSize
new50.63 KB

Diff'd from CCK 6.2 RC6.

This patch adds the ability to use views to populate the allowed values for a userreference field. It is implemented in exactly the same way as the functionality in nodereference.

yched’s picture

Cool - thanks for working on this, I'll try to review shortly.
Patch is strange, though. It removes (-) the whole current content of userrefermence.module, and then adds (+) the whole new file, instead of diffing only the parts that need to be diffed...

CPyle’s picture

I noticed that too. I used Tortoise SVN to create it. I'll install CVS and recreate it if necessary.

CPyle’s picture

StatusFileSize
new11.23 KB

Don't know what the problem was. I had a coworker create the patch and it worked fine, no wonkiness.

No code changes, just a correct patch file.

Edit: I might add, I am currently using this in a project to limit selections to users within the current node's og group context, and it is working wonderfully. I have not tested any more complex use cases, but this was a common request that I've seen with regards to those wanting this functionality.

yched’s picture

Thx, looks better.

+    // We do need title field, so add it if not present (unlikely, but...)
+    $fields = $view->get_items('field', $display);
+    if (!isset($fields['name'])) {
+      $view->add_item($display, 'field', 'node', 'name');
+    }
+    if ($string !== '') {
+      // Add an instance of the 'title' filter.
+      $options = array('value' => $string, 'operator' => 'contains');
+      $id = $view->add_item($display, 'filter', 'node', 'name', $options);
+    }

Comments are invalid :-) (there might be a few others left)
Refering to 'node', 'name' field and filters look strange - we shouldn't be referring to nodes, right ?

Although looking in views/modules/users.views.inc, it seems 'users'/'name' doesn't provide any filter ?

yched’s picture

For the autocomplete behavior, we need a way to filter users whose name starts with a given string.
Views currently doesn't provide any 'filter' for this. There's an 'argument' that does it, so we could use that argument instead, but this is probably going to be more complicated. This will make the code too different from the one in nodereference, and I wish we could merge them at some point.

Best thing would be a patch for views that adds the filter we need - others might need it too.
Faster way would probably be to add this filter ourselves in hook_views_data_alter().

CPyle’s picture

StatusFileSize
new11.36 KB

You're right. There was no filter set for the name field. I've tried adding it via hook_views_data_alter, but it really should probably be done via a patch to views.

I've fixed the other stuff left over from nodereference.

New patch version.

Thanks for reviewing this, it's a feature I've been wanting since Drupal 5, but views didn't support users at the time.

yched’s picture

Cool - please submit the patch for Views, referencing this issue. Let's see if merlinofchaos is willing to add it in views directly.

CPyle’s picture

I've submitted the patch to views. The hook_views_data_alter function in my userreference patch can obviously be removed when/if merlinofchaos puts this through.

#298396: Add string filter to users/name field.

yched’s picture

Many thanks, CPyle. I'll try to actually test your patch asap.

yched’s picture

Seems to work quite right, except for with autocomplete widget : we never get any completion results.

yched’s picture

Status: Needs review » Needs work

Problem is that in userreference_autocomplete() :

foreach (_userreference_potential_references($field, $string) as $uid => $name) {
  $matches[$name] = check_plain($name);
}

$name is an array, so $matches[$name] = check_plain($name); breaks.

I'd say the problem lies in userreference_plugin_style_php_array::render()

CPyle’s picture

Status: Needs work » Needs review
StatusFileSize
new10.73 KB

Looks like userreference by original design expects it's results to come in as uid->value, whereas nodereference accepts a views themed array.

I removed the extra array from userreference_plugin_style_php_array::render() and reverted the changes to userreference_allowed_values(). Seems to work great.

Also, name filter operator is now "Starts with" instead of "Contains."

Thanks for the input.

yched’s picture

Status: Needs review » Fixed

Problem was rather that userreference_autocomplete was not mimicking nodereference_autocomplete.
I fixed that and committed the thing - result is closer from patch #10.

Thanks for this !

Side note : there's now a lot of code duplication between nodereference.module and userreference.module... Food for though. Some of this (Views mode, in particular) might be worth abstracting out...

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

gunzip’s picture

will this ever implemented in the cck core ?

gunzip’s picture

Status: Closed (fixed) » Patch (to be ported)
yched’s picture

Status: Patch (to be ported) » Closed (fixed)

gunzip : The feature has been in CCK 6.x-2.1 for several months.
If the question is 'will this be backported to CCK D5 ?', the answer is 'no, because CCK D5 receives no new feature, *and* Views D5 does not lists users to begin with :-D'