3

I've built a custom field using the Field API. I've defined the following element:

  $element['tabcontent'] = array(
    '#title' => t('Tab content'),
    '#type' => 'text_format',
    '#required' => $required,
    // use #default_value to prepopulate the element
    // with the current saved value
    '#default_value' => isset($item['tabcontent']) ? $item['tabcontent'] : '',
    '#tree' => true,
    '#rows' => 10,
    '#format' => 'full_html',
    '#base_type' => 'textarea',
  );

I've got the following code in the install file:

function tabfield_field_schema($field) 
{
 
  $columns = array(
    'tabtitle' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
    'tabcontent' => array('type' => 'text','not null' => TRUE,'size' => 'normal','description' => 'Question prompt','length' => '1000'  ),
  );


  return array(
    'columns' => $columns,
    'indexes' => array(),
  );
}

However, I get the following error when saving a new node:

Warning: preg_match_all() expects parameter 2 to be string, array given in _media_generate_tagMap() (line 186 of C:\Workspace\RnD\drupal1\sites\all\modules\media\includes\media.filter.inc).

Warning: Invalid argument supplied for foreach() in _media_generate_tagMap() (line 187 of C:\Workspace\RnD\drupal1\sites\all\modules\media\includes\media.filter.inc).

Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1572 of C:\Workspace\RnD\drupal1\includes\bootstrap.inc).

PDOException: 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_tab} (entity_type, entity_id, revision_id, bundle, delta, language, field_tab_tabtitle, field_tab_tabcontent) 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, :db_insert_placeholder_7_value, :db_insert_placeholder_7_format); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 17 [:db_insert_placeholder_2] => 17 [:db_insert_placeholder_3] => tabbed_page [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => ss [:db_insert_placeholder_7_value] => ss [:db_insert_placeholder_7_format] => full_html ) in field_sql_storage_field_storage_write() (line 448 of C:\Workspace\RnD\drupal1\modules\field\modules\field_sql_storage\field_sql_storage.module).

1
  • I'm getting the same error from a save on a textarea field on a form that used to work :(
    – user1359
    Commented Oct 12, 2012 at 17:37

2 Answers 2

1

This is because your value suppose to be string, however you given array as per these errors:

Warning: preg_match_all() expects parameter 2 to be string, array given in _media_generate_tagMap()

Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain()

Since you're sending array, the column count doesn't match in your SQL query.

This is what you're sending:

INSERT INTO {field_data_field_tab} (entity_type, entity_id, revision_id, bundle, delta, language, field_tab_tabtitle, field_tab_tabcontent)
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, :db_insert_placeholder_7_value, :db_insert_placeholder_7_format);
Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 17 [:db_insert_placeholder_2] => 17 [:db_insert_placeholder_3] => tabbed_page [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => ss [:db_insert_placeholder_7_value] => ss [:db_insert_placeholder_7_format] => full_html )

which can be broke down into:

  • Fields: entity_type, entity_id, revision_id, bundle, delta, language, field_tab_tabtitle, field_tab_tabcontent
  • Placeholders: :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, :db_insert_placeholder_7_value, :db_insert_placeholder_7_format

So you've 8 fields, but 9 placeholders.

Fix

To fix it, you've to check passed value of $item['tabcontent'] and make it a string. For example:

'#default_value' => !empty($item['tabcontent']['value']) ? $item['tabcontent']['value'] : '',

See also:

0

I only got it to work by using a combination of this approach + the variable_get() method.

enter image description here

enter image description here

The final code looks like this. Notice the format coming from the field as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.