Hi all,
I am fairly new to Drupal environment. I am tring to create a custom field in custom module that allows me to add and stores multiple values (Checkboxes). Below is the widget_form and field_is_empty hooks that I used for testing if it works or not.
function my_test_module_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$avalue = isset($items[$delta]['mymodulefield']) ? $items[$delta]['mymodulefield'] : '';
$element += array(
'#delta' => $delta,
);
$element['mymodulefield'] = array(
);
switch ($instance['widget']['type'])
{
case 'my_test_module_widget':
$element['mymodulefield'] = array(
'#title' => t('Some text'),
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#default_value' => $avalue,
'#options' => array(
'1' => t('Requirement 1'),
'2' => t('Requirement 2'),
'3' => t('Requirement 3'),
'4' => t('Requirement 4'),
),
);
break;
default:
break;
}
return $element;
}
function my_test_module_field_is_empty($item, $field) {
if(empty($item['mymodulefield']))
return true;
else
return false;
}
After creating a custom field using this widget I get the checkbox on the page but when saved, the values were not saving. Upon doing some testing I found the issue. What I did for testing is put a print and exit inside field_is_empty hook.
function my_test_module_field_is_empty($item, $field) {
echo "ISEMPTY<pre>";
print_r($item);exit;
}
The output I got from this was
ISEMPTY
Array
(
[1] => 1
[2] => 2
[3] => 0
[4] => 0
)
This explained why the value were not getting saved (as field_is_empty returns true). Though it explained why values are not getting stored but raised another question why I am not getting the element name "mymodulefield" in my item.
If anyone can point me to right direction on this would be greatly appreciated!
Regards,
shresthao
Comments
Checkboxes will always return
Checkboxes will always return an array, with one element for each checkbox. If the checkbox has been checked, the value of the array element will be the same as the key. For example, in the values you gave above, the checkboxes with a value of 1 and 2 have been checked. If the value is zero, it means the checkbox has not been checked.
So determine if a value has been entered, you can use this:
Thanks Jaypan for your reply
Thanks Jaypan for your reply and explanation. This makes sense. However just wanted to make sure one last thing. From the example codes I have seen for field_is_empty hook, the $item is "array" with some kind of key eg. "value" in the api.drupal.org example
But for my case if you see my $item variable, it is an array without any key.
What my understanding was is if I define an form element in field_widget_form hook as
I should get output something like
(where 1 and 2 is checked) so I can use something like you've showed but with the key like
Is what I think is not right? Hope you can shed some light on this and help me understand the process correctly.
Once again thank you for your help and look forward to hear from you soon!
Regards,
Shresthao
What do you see if you add
What do you see if you add this to the top of the _is_empty() function:
Hi Jaypan. Below is the code
Hi Jaypan. Below is the code I see
Regards,
Shresthao
Then what I showed you was
Then what I showed you was correct.