I'm trying to make a select list field similar to the field 'Assigned' in this issue form.

User-reference would work if it allowed me to filter out non-subscribers of the current list, but the settings only allow for php to set default users. And it doesn't make sense for the extra overhead to define a role for every single group - if a list of subscribers can be obtained otherwise.

So hm, I think - go with a select list with the allowed values set by some custom php.

It's cck meets og so I piece together some code from the og.module and working from either of these queries:

$sql = og_list_users_sql(0,0,'ou.is_admin DESC, ou.is_active ASC, u.name ASC');
  $result = db_query($sql, $gid);
 $sql = "SELECT DISTINCT(u.uid), u.* FROM {og_uid} ogu INNER JOIN {users} u ON ogu.uid = u.uid WHERE ogu.nid = $gid AND ogu.is_active = 1 AND u.status = 1 ORDER BY ogu.created DESC";
  $result = db_query($sql, $gid);

I get this working. Both in the devel 'execute PHP' block and also as a computed field on the form itself. Great.

But when I enter it for the select list on my field, it returns blank select lists in my form. So scooping out the correct $result is ok, but returning an array of values that cck will take for its select list is where I'm completely stuck.

Since my grasp of theming functions is better than manipulating arrays, I try and return themed results with assorted functions including one of my own that returns the results wrapped within array(); like this (and following this example ):
array(1 => Admin, 2 => User1, 3 => Guest3, 4 => Trish, 5 => TBA);

Looks all well and good, but it doesn't fill out the list like I'd expect. Probably because it's not really an array.

So I look again at http://drupal.org/node/125464 and am trying to adapt it, but so far no-go

while($row = db_fetch_array($result)){
$rows[] = $row['uid'];
}
return $rows;
CommentFileSizeAuthor
#9 userreferencephp.zip15.72 KBCPyle

Comments

brst t’s picture

Title: allowed values php code - select list » allowed values php code - select list with og subscribers

Oh, I posted too soon, or given how long I've been at this, really - not soon enough.

Here's the whole damn bit that goes into the 'Php code' part of the text select list field in CCK-- returning a select list of members (aka subcribers) of the current group.

$group_node = og_set_group_context();
    $gid = $group_node->nid;
 $sql = "SELECT DISTINCT(u.uid), u.* FROM {og_uid} ogu INNER JOIN {users} u ON ogu.uid = u.uid WHERE ogu.nid = $gid AND ogu.is_active = 1 AND u.status = 1 ORDER BY ogu.created DESC";
  $result = db_query($sql);
while($row = db_fetch_array($result)){
$rows[] = $row['name'];
}
return $rows;

It doesn't include the manager of the group, but then that's og wonkiness.

I have my select lists; I'm happy.

brst t’s picture

It doesn't include the manager of the group, but then that's og wonkiness.

Correction: it does include the manager of the group, now that I have upgraded the userplus module.

I have my select lists; I'm happy.

brst t’s picture

Title: allowed values php code - select list with og subscribers » Allow option to code a filter on the user reference select list-- 5 og members vs 150 users?
Component: text.module » userreference.module
Category: support » feature

Still happy, but the results from these select lists are crap on the views end.

Still able to use it for the one field that doesn't factor into any filters, but back to using an autocomplete user reference field, with an og member list sitting as a reference over there in sidebar-left.

So, after all this, I'm renaming and changing this to a feature request.

On the user reference field creation form:

Allow optional filter code to be entered so that big site lists may be whittled down and restricted to an og member list. Result: instead of listing all 150 users, there's a chance to list the 5 that are members in og group A, the 4 in group B, the 8 in group C...

brst t’s picture

Updating this request after looking more closely at the node reference module, again.

The Data Settings used in the configuration of that content type would definitely work with this, particularly 'Advanced - Nodes that can be referenced (View)'. Then it could be used to filter users based on profile fields as well.

tannerjfco’s picture

I decided to give this a shot for my site, but it's throwing mysql syntax errors on the CCK edit page and randomly on other pages, though the select list is working. any idea what's going on here?

MedicSean37’s picture

did this ever get working? i need the same thing.

CPyle’s picture

Hello, after running into the same problem wherein I need to filter a userreference dropdown based on group affiliation, I made my own modified userreference module.

At first I tried to mimic being able to use a view as a filter like you can in nodereference, but ran into a lot of trouble. So instead I used good ole php filters.

My module is exactly the same as cck userreference only it allows you to enter in php code that returns an array in the format 'uid' => 'label' to be used in filtering users that can be referenced. If anyone wants to use my module let me know. I won't be writing any patches to do this for CCK core modules as I feel it should be done using views and my knowledge doesn't extend far enough to do that properly.

Anonymous’s picture

@Cpyle

I'd be interested in your module. Can you post it here?

TIA

txcrew

CPyle’s picture

StatusFileSize
new15.72 KB

@txcrew

Here it is. This is a replacement for the normal userreference module so you should disable that one before turning on this one. Do not place it under the cck folder.

Be aware that CCK expects at least one valid value to be returned in your code. Also expect this to be entirely unsupported, as it is basically a hack I made for my own uses until we can convince someone to add in views referencing for allowed values.

darrenmothersele’s picture

I too would like to see views referencing for allowed values on the userreference cck field type.

Now that Views 2 has user views shouldn't this be possible in the same way that nodereference fields can select from a view's nodes?

CPyle’s picture

I tried doing this using the new views and couldn't get it to work before switching to a php filter. At some point I may revisit this and try to make a patch for userreference, as there is now an open feature request for it ( #295556: Userreference with allowed values from View ).

CPyle’s picture

I have submitted a patch to add the ability to filter userreference using views. It is currently under review.

See the aforementioned request #295556: Userreference with allowed values from View.

CPyle’s picture

Status: Active » Closed (fixed)