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

Jaypan’s picture

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:

function my_test_module_field_is_empty($item, $field) {
  foreach($item as $item)
  {
    if($item)
    {
      // A value has been selected - so the field is not empty.
      return FALSE;
    }
  }
  // No boxes are checked, so the field is empty
  return TRUE;
}


Checkout my Japan podcasts.
shresthao’s picture

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

function hook_field_is_empty($item, $field) {
  if (empty($item['value']) && (string) $item['value'] !== '0') {
    return TRUE;
  }
  return FALSE;
}

But for my case if you see my $item variable, it is an array without any key.

Array
(
    [1] => 1
    [2] => 2
    [3] => 0
    [4] => 0
)

What my understanding was is if I define an form element in field_widget_form hook as

$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'),
					),
			);

I should get output something like

Array
(
    [mymodulefield] => Array[1] => 1
                     [2] => 2
                     [3] => 0
                     [4] => 0
)

(where 1 and 2 is checked) so I can use something like you've showed but with the key like

function my_test_module_field_is_empty($item, $field) {
  foreach($item['mymodulefield'] as $itm)
  {
    if($itm)
    {
      // A value has been selected - so the field is not empty.
      return FALSE;
    }
  }
  // No boxes are checked, so the field is empty
  return TRUE;
}

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

Jaypan’s picture

What do you see if you add this to the top of the _is_empty() function:

drupal_set_message('<pre>' . print_r($item, TRUE) . '</pre>');

Checkout my Japan podcasts.
shresthao’s picture

Hi Jaypan. Below is the code I see

    Array
    (
        [2] => 2
        [1] => 1
        [3] => 0
        [4] => 0
    )

    Array
    (
        [2] => 2
        [1] => 1
        [3] => 0
        [4] => 0
    )

    Content AA > RA > RA has been updated.

Regards,
Shresthao

Jaypan’s picture

Then what I showed you was correct.


Checkout my Japan podcasts.