2

I created a custom field as in field_example described, using text_format as type and it shows up a multi line editor without any problems.

But I'm facing a mysql error when trying to save a node:

messagePDOException: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1: INSERT INTO {field_data_field_test3} (entity_type, entity_id, revision_id, bundle, delta, language, field_test3_additional_info) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6_value, :db_insert_placeholder_6_format); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 12 [:db_insert_placeholder_2] => 12 [:db_insert_placeholder_3] => article [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6_value] => test [:db_insert_placeholder_6_format] => filtered_html ) in field_sql_storage_field_storage_write() (line 451 of /var/www/demo/site/modules/field/modules/field_sql_storage/field_sql_storage.module).

I have the following files: si_media.install:

<?php

/**
 * Implements hook_field_schema().
 */
function si_media_field_schema($field) {
  $columns = array(
    'additional_info' => array(
      'type' => 'text',
      'not null' => FALSE
    ),
  );
  $indexes = array(
    //'additional_info' => array('additional_info'),
  );
  return array(
    'columns' => $columns,
    'indexes' => $indexes,
  );
}

and si_media.module:

<?php
/**
 * Implements hook_field_info().
 */
function si_media_field_info() {
  return array(
    // We name our field as the associative name of the array.
    'si_media_additional_info' => array(
      'label' => t('Additional Informations'),
      'description' => t('Adds more informations to a media field.'),
      'default_widget' => 'si_media_text',
      'default_formatter' => 'si_media_simple_text',
    ),
  );
}

/**
 * Implements hook_field_validate().
 */
function si_media_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if (!empty($item['additional_info'])) {
      /*if (! preg_match('@^#[0-9a-f]{6}$@', $item['additional_info'])) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'si_media_invalid',
          'message' => t('Color must be in the HTML format #abcdef.'),
        );
      }*/
    }
  }
}

/**
 * Implements hook_field_is_empty().
 */
function si_media_field_is_empty($item, $field) {
  return empty($item['additional_info']);
}

/**
 * Implements hook_field_formatter_info().
 */
function si_media_field_formatter_info() {
  return array(
    // This formatter just displays the hex value in the color indicated.
    'si_media_simple_text' => array(
      'label' => t('Default output'),
      'field types' => array('si_media_additional_info'),
    ),
  );
}

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

  switch ($display['type']) {
    // This formatter simply outputs the field as text and with a color.
    case 'si_media_simple_text':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          // We create a render array to produce the desired markup,
          // "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
          // See theme_html_tag().
          '#type' => 'html_tag',
          '#tag' => 'p',
          '#attributes' => array(
            'style' => 'color: ' . $item['additional_info'],
          ),
          '#value' => t('The color code in this field is @code', array('@code' => $item['additional_info'])),
        );
      }
      break;
  }

  return $element;
}

/**
 * Implements hook_field_widget_info().
 */
function si_media_field_widget_info() {
  return array(
    'si_media_text' => array(
      'label' => t('Text area (multiple rows)'),
      'field types' => array('si_media_additional_info'),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function si_media_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $value = isset($items[$delta]['additional_info']) ? $items[$delta]['additional_info'] : '';

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

  switch ($instance['widget']['type']) {
    case 'si_media_text':
      $widget += array(
        '#type' => 'text_format',
        '#default_value' => $value,
      );
      break;
  }

  $element['additional_info'] = $widget;
  return $element;
}

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

I don't get it, why there is this error coming.

1

The error message tells you that there is a mismatch between the insert value list (7 items) and the number of values you're trying to insert (8 items).

The error message comes from a core module, that somehow is fed garbage. I am unable to see from the code snippets posted where this garbage actually comes from.

0

The error usually happens when you're trying to pass array for the field value when Drupal expects the string (as per 'type' => 'text').

So I would check if the $value:

'#default_value' => $value,

is really in string format (as per $items[$delta]['additional_info'] assignment) or you need to specify exact element further down in the array.

Related:

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.