1

I have created a custom field type. When creating a piece of content with this field type I get a select list and a text area like the image below. However, I have stated that the '#title' for the select list is Label and the textarea is Value.

These '#title''s I've given them do not show up. From looking at other modules that provide a field type that has multiple elements on its widget form seem to be doing it the same way but their titles show up.

enter image description here

function civsearch_property_labels_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if (isset($items[$delta]['label'])) {
    $default_label = $items[$delta]['label'];
  }

  if (isset($items[$delta]['value'])) {
    $default_value = $items[$delta]['value'];
  }

  // We are unserializing the variable_get because the string that was
  // serialized with variable_set was a serialized array. So we need to run the
  // unserialize process one more time.
  $options = array();
  $list_items = unserialize(variable_get('civsearch_property_labels', ''));
  foreach ($list_items as $list_item) {
    $explode = explode('|', $list_item);
    $options[$explode[0]] = $explode[1];
  }

  $labels = $element;
  $labels += array(
    '#type' => 'select',
    '#title' => t('Label'),
    '#options' => $options,
    '#default_value' => isset($default_label) ? $default_label : '',
    '#suffix' => '<br \>',
  );

  $values = $element;
  $values += array(
    '#type' => 'textarea',
    '#title' => t('Value'),
    '#default_value' => isset($default_value) ? $default_value : '',
  );

  return array(
    'label' => $labels,
    'value' => $values,
  );
}
1

According to hook_field_widget_form() the $element parameter will already have a #title set. If you take a look at the docs for PHP array operators:

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

Emphasis mine.

So your new #title is currently being ignored.

Try this instead:

$labels = array(
  '#type' => 'select',
  '#title' => t('Label'),
  '#options' => $options,
  '#default_value' => isset($default_label) ? $default_label : '',
  '#suffix' => '<br \>',
) + $element;

The example in hook_field_widget_form() might be a bit confusing as it uses an approach similar to your original one, but if you look closely neither of the properties added exist in the list of defaults that $element will contain, so there's no chance of a collision.

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.