2

I've been following the Drupal example on how to create a new field. I can get their example to work fine, but when I try my own it does not seem to store any data, i.e. the field is always blank when I load the node back. I am ultimately attempting to create a field which can hold multiple pieces of information, a bit like the example three field color code. Although I would want a better storage then expanding a value into an array (I have an idea on this, but until I have the basic data storage working I cannot comment on this).

The example is here.

This is my code from the module; I have stripped the example down to the bare minimum, and I have checked it works on the color wheel code.

 function prodlist_field_schema($field) {
  if ($field['type'] == 'prodlist_code') { 
      $columns = array(
        'prodcode' => array(
          'type' => 'varchar',
          'length' => 7,
          'not null' => FALSE,
        ),
      );
  }
  return array('columns' => $columns);
}

function prodlist_field_info() {
  return array(
    'prodlist_code' => array( 
      'label' => t('Product Code'),
      'description' => t('Used to assign product codes'),
      'default_widget' => 'prodlist_text',
      'default_formatter' => 'field_example_simple_text',
    ),
  );
}

function prodlist_field_widget_info() {
  return array(
     'prodlist_text' => array(
      'label' => t('Prodcode entry fields'),
      'field types' => array('prodlist_code'), 
    ),

  );
}

function prodlist_field_is_empty($item, $field) {
  return empty($item['prodlist_code']); 
}

function prodlist_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $value = isset($items[$delta]['prodlist_code']) ? $items[$delta]['prodlist_code'] : 'emptyonload';

  $widget = $element;
  $widget['#delta'] = $delta;

    $widget += array(
          '#type' => 'textfield',
          '#title' => 'prodcode',
          '#size' => 23,
          '#default_value' => $value,
        );

  $element['prodcode'] = $widget; 
  return $element;
}
1

I found another great example in the book "Drupal 7 Module Development", having worked through this example I am now able to alter it to my own purposes and the data is persistent.

Below is a working 3 field example.

function prodlist_field_info() {
  return array(
    'productorder' => array( 
      'label' => t('Product Code'),
      'description' => t('Used to assign product codes'),
      'default_widget' => 'prodlist_text',
      'default_formatter' => 'field_example_simple_text',
    ),
  );
}


function prodlist_field_schema($field) {
  if ($field['type'] =='productorder') {
      $schema['columns']['code'] = array(
        'type' => 'varchar',
        'length' => 10,
        'not null' => FALSE,
      );
      $schema['columns']['description'] = array(
        'type' => 'varchar',
        'length' => 10,
        'not null' => FALSE,
      );
      $schema['columns']['amount'] = array(
        'type' => 'int',
        'not null' => FALSE,
      );


      $schema['indexes'] = array(
        'code' => array('code'),
        'description' => array('description'),
        'amount' => array('amount'),
      );

      return $schema;
  }

}

function prodlist_field_is_empty($item, $field) {
  if ($field['type'] == 'productorder') {
    if (empty($item['code']) && empty($item['description']) && empty($item['amount']) ) {
      return TRUE;
    }
  }
  return FALSE;

}


function prodlist_field_widget_info() {
  return array(
     'prodlist_text' => array(
      'label' => t('Prodcode entry fields'),
      'field types' => array('productorder'), 

    ),

  );
}

function prodlist_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $base = $element;

  if ($instance['widget']['type'] == 'prodlist_text') {
    $element['code'] = array(
      '#type' => 'textfield',
      '#title' => t('Code'),
      '#default_value' => isset($items[$delta]['code']) ? $items[$delta]['code'] : NULL) + $base;
    $element['description'] = array(
      '#type' => 'textfield',
      '#title' => t('Description'),
      '#default_value' => isset($items[$delta]['description']) ? $items[$delta]['description'] : NULL) + $base;
    $element['amount'] = array(
      '#type' => 'textfield',
      '#title' => t('Amount'),
      '#default_value' => isset($items[$delta]['amount']) ? $items[$delta]['amount'] : NULL) + $base;

  }
  return $element;

}

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.