function userreference_widget in Content Construction Kit (CCK) 5
Same name and namespace in other branches
- 6.3 modules/userreference/userreference.module \userreference_widget()
- 6 modules/userreference/userreference.module \userreference_widget()
- 6.2 modules/userreference/userreference.module \userreference_widget()
Implementation of hook_widget().
File
- ./
userreference.module, line 171 - Defines a field type for referencing a user from a node.
Code
function userreference_widget($op, &$node, $field, &$items) {
if ($field['widget']['type'] == 'userreference_select') {
switch ($op) {
case 'prepare form values':
$items_transposed = content_transpose_array_rows_cols($items);
// get rid of null values
$items['default uids'] = array_filter((array) $items_transposed['uid']);
break;
case 'form':
$form = array();
$options = _userreference_potential_references($field);
if (!$field['required']) {
$options = array(
'none' => t('<none>'),
) + $options;
}
if (empty($items['default uids'])) {
$items['default uids'][] = 'none';
}
$form[$field['field_name']] = array(
'#tree' => TRUE,
);
$form[$field['field_name']]['uids'] = array(
'#type' => 'select',
'#title' => t($field['widget']['label']),
'#default_value' => $items['default uids'],
'#multiple' => $field['multiple'],
'#size' => $field['multiple'] ? min(count($options), 6) : 0,
'#options' => $options,
'#required' => $field['required'],
'#description' => content_filter_xss(t($field['widget']['description'])),
);
return $form;
case 'process form values':
if ($field['multiple']) {
// drop the 'none' option
unset($items['uids']['none']);
if (!empty($items['uids'])) {
$items = array_values(content_transpose_array_rows_cols(array(
'uid' => $items['uids'],
)));
}
else {
$items[0]['uid'] = '';
}
}
else {
$items[0]['uid'] = $items['uids'] != 'none' ? $items['uids'] : '';
}
// Remove the widget's data representation so it isn't saved.
unset($items['uids']);
foreach ($items as $delta => $item) {
$items[$delta]['error_field'] = $field['field_name'] . '][uids';
}
}
}
else {
switch ($op) {
case 'prepare form values':
foreach ($items as $delta => $item) {
if (!empty($items[$delta]['uid'])) {
$items[$delta]['default user_name'] = db_result(db_query("SELECT name FROM {users} WHERE uid = '%d'", $items[$delta]['uid']));
}
}
break;
case 'form':
$form = array();
$form[$field['field_name']] = array(
'#tree' => TRUE,
);
if ($field['multiple']) {
$form[$field['field_name']]['#type'] = 'fieldset';
$form[$field['field_name']]['#description'] = content_filter_xss(t($field['widget']['description']));
$delta = 0;
foreach ($items as $item) {
if ($item['uid']) {
$form[$field['field_name']][$delta]['user_name'] = array(
'#type' => 'textfield',
'#title' => $delta == 0 ? t($field['widget']['label']) : '',
'#autocomplete_path' => 'userreference/autocomplete/' . $field['field_name'],
'#default_value' => $item['default user_name'],
'#required' => $delta == 0 ? $field['required'] : FALSE,
);
$delta++;
}
}
foreach (range($delta, $delta + 2) as $delta) {
$form[$field['field_name']][$delta]['user_name'] = array(
'#type' => 'textfield',
'#title' => $delta == 0 ? t($field['widget']['label']) : '',
'#autocomplete_path' => 'userreference/autocomplete/' . $field['field_name'],
'#default_value' => '',
'#required' => $delta == 0 ? $field['required'] : FALSE,
);
}
}
else {
$form[$field['field_name']][0]['user_name'] = array(
'#type' => 'textfield',
'#title' => t($field['widget']['label']),
'#autocomplete_path' => 'userreference/autocomplete/' . $field['field_name'],
'#default_value' => $items[0]['default user_name'],
'#required' => $field['required'],
'#description' => content_filter_xss(t($field['widget']['description'])),
);
}
return $form;
case 'process form values':
foreach ($items as $delta => $item) {
$uid = '';
if (!empty($items[$delta]['user_name'])) {
$uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $items[$delta]['user_name']));
}
// Remove the widget's data representation so it isn't saved.
unset($items[$delta]['user_name']);
$items[$delta]['uid'] = $uid;
$items[$delta]['error_field'] = $field['field_name'] . '][' . $delta . '][user_name';
// Don't save empty fields except the first value
if (empty($uid) && $delta > 0) {
unset($items[$delta]);
}
}
}
}
}