Support setting default value

Created on 25 June 2025, 17 days ago

Problem/Motivation

AFAICT FieldCreateManager doesn't support setting the default value for a field instance.

Proposed resolution

Attached patch adds this support (via 'default_value' key for a field instance/bundle's settings).

Remaining tasks

Review & test.

User interface changes

None.

API changes

None.

Data model changes

'default_value' supported in field instance/bundle settings.

💬 Support request
Status

Needs review

Version

1.0

Component

Code

Created by

🇨🇦Canada bgilhome Victoria

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

Comments & Activities

  • Issue created by @bgilhome
  • 🇫🇷France pcharrat La rue, la vraie.

    The patch works because when I get the following code:

    /**
     * Implements hook_field_create_definitions().
     */
    function mymodule_list_field_create_definitions_alter(array &$definitions) {
      // Define your fields here by entity type.
      $definitions['node'] = [
        'demo_field_2' => [
          'name'    => 'demo_field_2',
          'label'   => 'This is my field',
          'type'    => 'string',
          'force'   => FALSE, 
          'bundles' => [
            'page' => [
          'default_value' => ['Lorem Ipsum'],
            ],
          ],
        ],
      ];
    }
    
    

    I see that I have my default value

  • 🇫🇷France pcharrat La rue, la vraie.

    I think we can add a default value for the field info.
    So, I propose the following patch.
    A test code :

    
    /**
     * Implements hook_field_create_definitions().
     */
    function contrib_todo_list_field_create_definitions_alter(array &$definitions) {
      // Define your fields here by entity type.
      $definitions['node'] = [
        'demo_field' => [
          'name'    => 'demo_field',
          'label'   => 'This is my field',
          'type'    => 'string',
          'default_value' => ['This is my default value'],
          'force'   => FALSE, 
          'bundles' => [
            'page' => [
              'default_value' => ['This is a more specific value'],
            ],
            'article' => []
          ],
        ],
      ];
    }
    

    This is my result :
    Default value article :

    Default value page :

Production build 0.71.5 2024