Using hook_form_alter, I've added a 'text_format' element to the system theme settings form. Submitting the form, I get the following error:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1543 of /srv/www/vhosts/default/includes/bootstrap.inc).

It looks like the submit handler for the system_theme_settings form does not properly handle the new 'text_format' fapi element.

For reference, here is the form alter function (defined in theme-settings.php):

function mytheme_form_system_theme_settings_alter(&$form, &$form_state) {
  $form['mytheme_settings']['frontpage_bigplay']['frontpage_body'] = array(
    '#type' => 'text_format',
    '#title' => t('Body'),
    '#default_value' => theme_get_setting('frontpage_body','mytheme'),
    '#format' => NULL,
  );
}

Comments

AmiGator’s picture

Got same error

jisuo’s picture

Same error here too on 7.12

I had a form in my custom module:

$form['mymodule_text'] = array(
  '#type' => 'text_format',
  '#format' => 'filtered_html',
  '#title' => t('Title'),
  '#default_value' => variable_get('mymodule_text', ''),
  '#description' => t('Description'),
);
manfredekblad’s picture

I'm getting the same error in bootstrap.inc, which I traced back to form.inc line 3769.

I think the variable $element is supposed to contain a key "#value" but it's missing the #... so it's just $element["value"], without the #. At least that's what I can see from doing a simple var_dump($element) inside the theme_textarea() function.

Update:

It does contain the key #value, just that it holds an array with the keys "value" and "format". check_plain() is expecting just a string, not an array, since it just pass that to htmlspecialchars().

Temporarily I fixed it by replacing theme_textarea() with this in includes/form.inc


function theme_textarea($variables) {
  $element = $variables['element'];
  element_set_attributes($element, array('id', 'name', 'cols', 'rows'));
  _form_set_class($element, array('form-textarea'));

  $wrapper_attributes = array(
    'class' => array('form-textarea-wrapper'),
  );

  // Add resizable behavior.
  if (!empty($element['#resizable'])) {
    drupal_add_library('system', 'drupal.textarea');
    $wrapper_attributes['class'][] = 'resizable';
  }

  if(is_array($element['#value']) && isset($element['#value']['value'])) $value = $element['#value']['value'];
  else $value = $element['#value'];

  $output = '<div' . drupal_attributes($wrapper_attributes) . '>';
  $output .= '<textarea' . drupal_attributes($element['#attributes']) . '>' . check_plain($value) . '</textarea>';
  $output .= '</div>';
  return $output;
}

Obviously that's just a quick n dirty fix.

jimmynash’s picture

I just ran into this using a text_format FAPI element on a custom modules system_settings_form.

Originally my form element was as such

$form['my_element'] = array(
  '#title' => t('Element Title'),
  '#type' => 'text_format',
  '#default_value' => variable_get('my_element', ''),
  '#format' => NULL,
  '#description' => t('Description of this form element.'),
);

Since I was getting back an array from the variable with both the value and the format, check_plain in theme_textarea was throwing the htmlspecialchars error.

I worked around it by getting the variable first and setting the parts to where they need to be in the form element array.

$tmp = variable_get('my_element', array('value' => '', 'format' => NULL));

$form['my_element'] = array(
  '#title' => t('Element Title'),
  '#type' => 'text_format',
  '#default_value' => $tmp['value'],
  '#format' => $tmp['format'],
  '#description' => t('Description of this form element.'),
);

Hope this helps someone.

hillaevium’s picture

Thanks jimmynash - that worked like a charm!

danny englander’s picture

Issue summary: View changes

Old issue here, but even with Drupal 7.27, I was still getting the Warning: htmlspecialchars() expects parameter 1 error. I can confirm that #4 above works great, exactly what I needed. This seems to be a work around though and not an actual fix.

PumaDias’s picture

This helped for me:

bootstrap.inc -> line 1566

function check_plain($text) {
  return is_string($text) ? htmlspecialchars($text, ENT_QUOTES, 'UTF-8') : '';
}
paulraj augustin’s picture

After working for an hour, I came up with the below solution to use text_format in theme-settings.php

function YOURTHEMENAME_form_system_theme_settings_alter(&$form, &$form_state, $form_id = NULL)  {
  // Work-around for a core bug affecting admin themes. See issue #943212.
  if (isset($form_id)) {
    return;
  }
  $footer = theme_get_setting('footer_text');

  $form['footer_text'] = array(
    '#type'          => 'text_format',
    '#title'         => t('Footer Text'),
    '#description'   => t('Enter the text to be appear in footer'),
    '#default_value' => $footer['value'],
    '#format' => $footer['format'],
  );
}
hessam61’s picture

#8 worked for me. Thanks!

kenorb’s picture

Workaround in case of custom field: If you changed #type from 'text' to 'text_format', you need to make sure your '#default_value' is also updated and it is a string, not an array.

Related: Field API field_schema for text field error

Version: 7.9 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.