1

I'm using Drupal 7 and I have a problem with custom field type generated by field type generator.

I attached this field to a content type, and set his value to unlimited. When I create content type and add values it works fine. So, when I modify the page, values are deleted from database and I must add again. Here attach the functions generated by field type generator.

/**
 * @file
 * mediatrack module.
 * Media Track field.
 */

/**
 * Implements hook_field_info().
 */
function mediatrack_field_info() {
  return array(
    'mediatrack' => array(
      'label' => t('Mediatrack'),
      'description' => t('The media track'),
      'default_widget' => 'mediatrack_widget_default',
      'default_formatter' => 'mediatrack_formatter_default',
    ),
  );
}

/**
 * Implements hook_field_widget_info().
 */
function mediatrack_field_widget_info() {
  return array(
    'mediatrack_widget_default' => array(
      'label' => t('Default widget'),
      'field types' => array('mediatrack'),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function mediatrack_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  switch ($instance['widget']['type']) {
    case 'mediatrack_widget_default':
      $element['mediatrack']['trackid'] = array(
        '#type' => 'textfield',
        '#title' => t('Trackid'),
        '#default_value' => strval(next_trackid($field)), 
      );
      $element['mediatrack']['tracktype'] = array(
        '#type' => 'select',
        '#options' => array("---","audio","video"),
        '#title' => t('Tipo traccia'),
      );
      $element['mediatrack']['tracktitle'] = array(
        '#type' => 'textfield',
        '#title' => t('titolo'),
        '#maxlength' => 255,
        '#default_value' => isset($items[$delta]['tracktitle']) ? $items[$delta]['tracktitle'] : NULL,
      );
      $element['mediatrack']['trackdesc'] = array(
        '#type' => 'text_format',
        '#title' => t('Descrizione'),
        '#format' => isset($items[$delta]['trackdesc_format']) ? $items[$delta]['trackdesc_format'] : NULL,
        '#default_value' => isset($items[$delta]['trackdesc']) ? $items[$delta]['trackdesc'] : '',
      );
      $element['mediatrack']['tracksource'] = array(
        '#type' => 'textfield',
        '#title' => t('file'),
        '#maxlength' => 255,
        '#default_value' => isset($items[$delta]['tracksource']) ? $items[$delta]['tracksource'] : NULL,
      );
    // Collapse existing entries, for some UX.
    if (($delta < sizeof($items))) {
      $fieldset_title = t("{$instance['label']} #{$delta}");
    }
    else {
      $fieldset_title = t("New entry for {$instance['label']}");
    }
    $element += array('#type' => 'fieldset');
    $element['#title'] = t('@fieldset_title', array('@fieldset_title' => $fieldset_title));
    $element += array(
      '#collapsible' => TRUE,
      '#collapsed' => ($delta < sizeof($items)),
    );
    break;
  }
  return $element;
}


function next_trackid($field) {
  $fieldname = array_keys($field['storage']['details']['sql']['FIELD_LOAD_CURRENT'])[0];
  $name = $field['field_name']; 
  $id = db_query("select max(" . $name . "_trackid) from {" . $fieldname . "};")->fetchfield();
  if ($id == NULL) {
    return 1;
  }
  else {
    return $id+1;
  }
}

/**
 * Implements hook_field_validate().
 */
function mediatrack_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  if ($field['type'] == 'mediatrack') {
    foreach ($items as $delta => $item) {
      // If field is not considered empty, validate its data.
      if (!mediatrack_field_is_empty($item, $field)) {
      }
    }
  }
}

/**
 * Implements hook_field_presave().
 */
function mediatrack_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  // Structure the field data as expected by the database. Only act when the
  // elements come nested as 'publisher_reference'. Otherwise, no change needed.
  if (!empty($items) && $field['type'] == 'mediatrack' && array_key_exists('mediatrack', $items[0])) {
    foreach ($items as $delta => $item) {
      $items[$delta]['trackid'] = $item['mediatrack']['trackid'];
      $items[$delta]['tracktype'] = $item['mediatrack']['tracktype'];
      $items[$delta]['tracktitle'] = $item['mediatrack']['tracktitle'];
      $items[$delta]['trackdesc'] = $item['mediatrack']['trackdesc']['value'];
      $items[$delta]['trackdesc_format'] = $item['mediatrack']['trackdesc']['format'];
      $items[$delta]['tracksource'] = $item['mediatrack']['tracksource'];
    }
  }
}

/**
 * Implements hook_field_formatter_info().
 */
function mediatrack_field_formatter_info() {
  return array(
    'mediatrack_formatter_default' => array(
      'label' => t('Default formatter'),
      'field types' => array('mediatrack'),
    ),
  );
}

This is a mediatrack field. Trackid is set to trackid +1 by default; I have a textfield for test only. When I modify, I can see table with previously added field collapsed, but, when I save, I don't see these fields, and the table is empty, if I don't add anything.

Do I need to set something?

0

I solved setting 'not null' to FALSE in my field_schema function, to trackid. Now it works!

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.