1

I have the following case: I have a field (Long text) with unlimited values. Now I have the following values:

Value 1
- empty value -
Value 3

By default, the second value would get removed. How can I prevent that? I don't really need a remove button, but that would be the cleanest way.

Update:
Okay, I' now trying with a custom field type. Thanks to Clive. The field is available, the fields are getting generated, but the values are not saved. Do I need a custom validation and submit function?

My code so far: si_media.install

<?php

/**
 * Implements hook_field_scheme().
 */
function si_media_field_scheme($field) {
  $schema = array(
    'columns' => array(
      'info_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'file_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
      'node_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
      'text_field' => array(
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
      )
    ),
    'indexes' => array(
      'info_id' => array('info_id'),
    ),
    'foreign keys' => array(),
  );

  return $schema;
}

si_media.module

<?php

/**
 * Implements hook_field_info().
 */
function si_media_field_info() {
  // Define new field type.
  return array(
    'si_media_additional_info' => array(
      'label'             => t('Additional informations'),
      'description'       => t('Field to provide additional informations for images'),
      'default_widget'    => 'si_media_field_widget',
      'default_formatter' => 'si_media_field_formatter',
      'settings'          => array(),
      'instance_settings' => array(),
    ),
  );
}

/**
 * Implements hook_field_formatter_info().
 */
function si_media_field_formatter_info() {
  // Define new display formatter.
  return array(
    'si_media_field_formatter' => array(
      'label'       => t('SI Media Formatter'),
      'field types' => array('si_media_additional_info'),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function si_media_field_formatter_view($entity_type, $entity, $field, $instance, $language, $items, $display) {
  $element = array();

  switch ($display['type']) {
    case 'si_media_field_formatter':
      $element[]['#markup'] = var_export($items, true);
      break;
  }
}

/**
 * Implements hook_field_widget_form().
 */
function si_media_field_widget_form(&$form, &$form_state, $field, $instance, $language, $items, $delta, $element) {
  switch ($instance['widget']['type']) {
    case 'si_media_field_widget':
      $element['text_field'] = array(
        '#type'           => 'text_format',
        '#maxlength'      => 500,
        '#title'          => $instance['label'],
        '#default_value'  => isset($items[$delta]['text']) ? $items[$delta]['text'] : NULL,
        '#required'       => $element['#required'],
        '#weight'         => $element['#weight'],
        '#size'           => 'big',
      );

      $element['si_media_additional_info_removed'] = array(
        '#type'           => 'hidden',
        '#value'          => '0',
      );

      break;
  }

  return $element;
}

/**
 * Implements hook_widget_error().
 */
function si_media_widget_error($element, $error, $form, &$form_state) {
  switch ($error['error']) {
    case 'si_media_field_invalid':
      form_error($element, $error['message']);
      break;
  }
}

/**
 * Implements hook_field_is_empty().
 */
function si_media_field_is_empty($item, $field) {
  return FALSE;
}

/**
 * Implements hook_field_widget_info().
 */
function si_media_field_widget_info() {
  return array(
    'si_media_field_widget' => array(
      'label' => t('Long Text'),
      'field types' => array('si_media_additional_info'),
      'settings' => array(),
      'behaviors' => array(
        'default value' => FIELD_BEHAVIOR_NONE,
      ),
    ),
  );
}

What am I missing? Thanks for any help!

  • 2
    You'll need to define your own custom field type and use that instead of the core text field - text_field_is_empty() decides what an 'empty' text field is, essentially anything where value is an empty string. I don't think there's any way to override that function without hacking core, so a new field type would probably be the easiest way – Clive Apr 30 '13 at 11:00
  • Thanks Clive! Can you provide me some information how to create a new field type? – lumio Apr 30 '13 at 11:03
  • 2
    There's an excellent example in the Examples module, and this question/answer should shed some light too. If you get stuck at any point just pop back here with your code and I'm sure someone will point you in the right direction :) – Clive Apr 30 '13 at 11:08
  • Thanks again, Clive. I'm facing one last problem (I think). If you want, you can create an answer to my question (as you already did in the comments) – lumio Apr 30 '13 at 12:57

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.