Hi,
First of all I want to excuse my bad english and the fact that I didn't know in which Component I should post my request. So is there a way that I can make node references as checkboxes? There is only a select list, And I had to modify the nodereference.module to have checkboxes. That's not good in case of an update, anyway here is the code how i did it:
Greetings
/**
* Implementation of hook_widget_info().
*/
function nodereference_widget_info() {
return array(
'nodereference_select' => array(
'label' => t('Select List'),
'field types' => array('nodereference'),
),
'nodereference_autocomplete' => array(
'label' => t('Autocomplete Text Field'),
'field types' => array('nodereference'),
),
'nodereference_checkboxes' => array(
'label' => t('Checkboxes'),
'field types' => array('nodereference'),
),
);
}
/**
* Implementation of hook_widget().
*/
function nodereference_widget($op, &$node, $field, &$items) {
if ($field['widget']['type'] == 'nodereference_select') {
switch ($op) {
case 'prepare form values':
$items_transposed = content_transpose_array_rows_cols($items);
$items['default nids'] = $items_transposed['nid'];
break;
case 'form':
$form = array();
$options = _nodereference_potential_references($field, TRUE);
foreach ($options as $key => $value) {
$options[$key] = _nodereference_item($field, $value, FALSE);
}
if (!$field['required']) {
$options = array(0 => t('<none>')) + $options;
}
$form[$field['field_name']] = array('#tree' => TRUE);
$form[$field['field_name']]['nids'] = array(
'#type' => 'select',
'#title' => t($field['widget']['label']),
'#default_value' => $items['default nids'],
'#multiple' => $field['multiple'],
'#size' => $field['multiple'] ? min(count($options), 6) : 0,
'#options' => $options,
'#required' => $field['required'],
'#description' => t($field['widget']['description']),
);
return $form;
case 'process form values':
if ($field['multiple']) {
// if nothing selected, make it 'none'
if (empty($items['nids'])) {
$items['nids'] = array(0 => '0');
}
// drop the 'none' options if other items were also selected
elseif (count($items['nids']) > 1) {
unset($items['nids'][0]);
}
$items = array_values(content_transpose_array_rows_cols(array('nid' => $items['nids'])));
}
else {
$items[0]['nid'] = $items['nids'];
}
// Remove the widget's data representation so it isn't saved.
unset($items['nids']);
foreach ($items as $delta => $item) {
$items[$delta]['error_field'] = $field['field_name'] .'][nids';
}
}
}
elseif ($field['widget']['type'] == 'nodereference_checkboxes') {
switch ($op) {
case 'prepare form values':
$items_transposed = content_transpose_array_rows_cols($items);
$items['default nids'] = $items_transposed['nid'];
break;
case 'form':
$form = array();
$options = _nodereference_potential_references($field, TRUE);
foreach ($options as $key => $value) {
$options[$key] = _nodereference_item($field, $value, FALSE);
}
$form[$field['field_name']] = array('#tree' => TRUE);
$form[$field['field_name']]['nids'] = array(
'#type' => 'checkboxes',
'#title' => t($field['widget']['label']),
'#default_value' => $items['default nids'],
'#multiple' => $field['multiple'],
'#size' => $field['multiple'] ? min(count($options), 6) : 0,
'#options' => $options,
'#required' => $field['required'],
'#description' => t($field['widget']['description']),
);
return $form;
case 'process form values':
if ($field['multiple']) {
// if nothing selected, make it 'none'
if (empty($items['nids'])) {
$items['nids'] = array(0 => '0');
}
// drop the 'none' options if other items were also selected
elseif (count($items['nids']) > 1) {
unset($items['nids'][0]);
}
$items = array_values(content_transpose_array_rows_cols(array('nid' => $items['nids'])));
}
else {
$items[0]['nid'] = $items['nids'];
}
// Remove the widget's data representation so it isn't saved.
unset($items['nids']);
foreach ($items as $delta => $item) {
$items[$delta]['error_field'] = $field['field_name'] .'][nids';
}
}
}
else {
switch ($op) {
...
Comments
Comment #1
drewish commentedi guess this could be considered a back port for #282183: Allow checkboxes/radios for nodereference ?
Comment #2
g10 commentedtnx, needed this :)
Comment #3
g10 commentedand a patch for convenience
(is patch against 5.x-1.9)
Comment #4
generaltao commentedCan somebody please comment on why this was not rolled into the current version of node reference for 5.x? I really need this too and don't want to have a potential broken version if I need to upgrade later. Works perfectly fine for me.
Thank you for the contribution.
Comment #5
yched commentedCCK D5 is feature frozen and receives only absolutely critical bugfixes - CCK is a huge module, maintaining and enriching two code bases is just not doable, sorry.
Comment #6
karens commentedToo old now, way past the time when we will add features to D5 version.