I am trying to make a custom field with one normal textarea and 1 text_format widget. The textarea functions perfect but if I try to use the text_format widget I get an primitve error when I am trying to save.

This is the error message:

This value should be of the correct primitive type

This is my formElement:

 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  $element['date_text'] = [
  '#type' => 'text_format',
  '#title' => 'text',
  '#format' => 'full_html',
  '#default_value' => isset($items[$delta]->date_text) ? $items[$delta]->date_text : NULL,
  '#placeholder' => t('Data Text'),
  '#element_validate' => array(
      array($this, 'validate'),
  $element['date_table'] = [
  '#type' => 'textarea',
  '#default_value' => isset($items[$delta]->date_table) ? $items[$delta]->date_table : NULL,
  '#placeholder' => t('Data Table'),
return $element;


and this is my schema:

  public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
  'columns' => [
   'date_text' => [
      'type' => 'text',
      'size' => 'medium',
    'date_table' => [
      'type' => 'text',
      'size' => 'medium',

return $schema;


It seems, that text_format returns an array instead of a single value and thus cant be saved in my text column. The array consists of the text and the format, i dont need the format, so I would need to replace the array with the text value.

How and where could I accomplish this?

Xdebug is not working on scripts that are executed when I click save, so I really have no clue what is really going on.


  • What is the error?
    – Kevin
    Commented Mar 24, 2017 at 14:33
  • I edited it in.
    – user73434
    Commented Mar 24, 2017 at 21:48

2 Answers 2


Here is what solved the issue:

I used messageFormValues in my Widget.php to override the Array with the text value, so it could be stored in my database.

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
  foreach($values as $key => $value) {
    $values[$key][date_text] = $value[date_text][value];
  return $values;

To unescape the html for my twig template(so it is displayed as html and not as text on the site) I used the raw attribute.

With this solution no Text-Format will be saved, but it was not necessary for my custom field anyway.


Thanks to Matt's solution I was able to save a text_format element that's part of a custom field, but I also wanted to store the chosen format so I did this:

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    foreach($values as $index => $value) {
        $values[$index]['body'] = json_encode($value['body']);
    return $values;

When re-populating the edit form, I can json_decode as applicable to get the value and text format out of the string.

  • 1
    N.b. json_decode gives you a stdClass object with two keys: "value" and "format".
    – Tim
    Commented Aug 21, 2021 at 16:53
  • @Tim or an associative array if you pass TRUE as the second parameter.
    – Geat
    Commented Aug 22, 2021 at 22:32

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.