Can not set text field format to a field with multiple items

Created on 12 October 2024, 2 months ago

Problem/Motivation

I needed a long text field that contain html. The field was assigned programmatically by node->set without setting format. The field was defined as full html. After assignment, in view mode, html is not shown. It shows html tags.
However, in edit mode, the field shows the html correctly. Saving edit mode cause the view mode to display the html correctly.

Steps to reproduce

Create a formatted text field and set it to multiple.
Create an array of values to set the field.
Assign the field content programmatically with html formatted text, using node->set
View the entity, it will not show the html.
Edit the entity and save it.
Now view mode will show the html.

Proposed resolution

The problem is that you cannot set the field format programmatically using:
$node->designated_field->format = 'full_html'
It will fail for string to array conversion

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

šŸ› Bug report
Status

Active

Version

10.3 āœØ

Component

field system

Created by

šŸ‡®šŸ‡±Israel taitai

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @taitai
  • I am unsure this is a bug. Show your code implementation.

  • šŸ‡®šŸ‡±Israel taitai

    Code:

    // Set the abstracts array
    foreach($abstracts['abstract'] as $key => $abstract){
    $abs_items [] = '

    Insight ' . $ins_items[$key] . '

    ' . $abstract;
    }
    // Set the field
    $Node->set('field_design_abstract',$abs_items);
    $Node->save();

  • Did you try?:

    foreach($abstracts['abstract'] as $key => $abstract){
           $abs_items [] = [
                'value' => '<p class="ins_t">Insight ' . $ins_items[$key] . '</p>' . $abstract,
                'format => 'full_html',
           ];
    }
    // Set the field
    $Node->set('field_design_abstract',$abs_items);
    $Node->save();
    
  • šŸ‡®šŸ‡±Israel taitai
    Your #4 works fine, thanks.
    However...2 questions:
    1. Why do you need to set the format explicitly as the field itself already defines the allowed format
    2.  I used the following which did not work..
    $Node->field_design_abstract = $abs_items;
    $Node->field_design_abstract->format = 'full_html';
    
    
  • Iā€™m glad I could help. You should examine the field values programmatically to see why this is the correct way.

Production build 0.71.5 2024