0

I've created a custom widget for a textarea (to display the contents in a table as well). So the textarea should appear and work just like a normal textarea widget, but with an additional HTML table of its contents.

I can make it appear correctly, but when I save the form the values do not get saved.

In fact, my "data_source_field_is_empty()" function is never called (the dpm() text never appears).

function data_source_field_widget_form(&$form,
                                         &$form_state,
                                         $field,
                                         $instance,
                                         $langcode,
                                         $items,
                                         $delta,
                                         $element) {
  switch ($instance['widget']['type']) {
    case 'data_source_data_widget':
      $thisItem = isset($items[$delta]) ? $items[$delta]['value'] : '';

      $widget['#type'] = 'textarea';
      $widget['#default_value'] = $thisItem;

      // Only do this if there is already content in the field
      if (isset($items[$delta])) {
        $rows = _data_source_parse_csv($thisItem);

        $widget['#prefix'] = '<button type="button" id="show-hide-raw-table-data">Show raw data</button>';
        $widget['#suffix'] = _data_source_dataTable($rows, 0);

        $form['#attached']['js'][] = array(
          'data' => drupal_get_path('module', 'data_source') . '/js/data_source.js',
          'type' => 'file',
        );
        $form['#attached']['css'][] = array(
          'data' => drupal_get_path('module', 'data_source') . '/css/data_source.css',
          'type' => 'file',
        );
      }

      $element[$field['field_name']] = $widget;
      break;

  }

  return ($element);
}

function data_source_field_is_empty($item, $field) {
  dpm("Got here");
  return empty($item[$field['field_name']]);
}

Any ideas? I'm looking at the $element[$field['field_name']] = $widget; part, but none of the variations I've tried have worked.

0

After studying the code in modules/field/text/text.module, I found the missing item: I had to assign my new widget to $element['value'], so:

$element['value'] = $widget;
return ($element);
| improve this answer | |
  • I notice that the dpm() still never gets called in data_source_field_is_empty(). I guess that debugging functions don't always work here. – Graham May 19 '17 at 18:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.