0

I have a module that implements all necessary hooks for a custom field api. I can see hook_field_validate() and hook_field_is_empty() working properly and values are also available on hook_pre_save() but with incorrect depth.

For example on hook_field_validate my $items array have:

161 (Array, 2 elements)
  0 (Array, 3 elements)
    token (String, 15 characters ) test_parameters
    target_id (String, 3 characters ) 161
    value (String, 2 characters ) X1

And on hook_pre_save() my field have:

field_parameters_values (Array, 1 element)
  und (Array, 1 element)
    0 (Array, 2 elements)
      0 (Array, 3 elements)
        token (String, 15 characters ) test_parameters
        target_id (String, 3 characters ) 161
        value (String, 2 characters ) X1

so there is a level more on the resulting array, it can be easily manipulated here but I want to understand the reason for that.

My hook_widget_form() consists in something like:

(...)
foreach ($tokens as $token) {
  (...)

  $element[$custom_id][$delta]['token'] = array(
    '#type' => 'hidden',
    '#value' => $token,
  );
  $element[$custom_id][$delta]['target_id'] = array(
    '#type' => 'hidden',
    '#value' => $custom_id,
  );
  $element[$custom_id][$delta]['value'] = array(
    '#type' => 'textfield',
    '#title' => $token,
    '#required' => $instance['required'],
    '#default_value' => '',
  );
  $delta++;
}
  • Why $element[$custom_id][$delta]['target_id'] instead of the usual $element['target_id']? That's definitely what's causing the problem, but I'm not sure why you're trying to wrap the widgets in extra array levels in the first place? – Clive Apr 15 '14 at 12:12
  • @Clive Hi Clive, thank you for your comment, I'm using the $delta for differentiating from multiple elements, without it will only have one element. – Paulo Gomes Apr 15 '14 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.