Ensure require_on_publish setting is applied when FieldConfig is created programmatically

Created on 31 July 2025, 3 days ago

Problem/Motivation

Currently, the require_on_publish third-party setting is added in the require_on_publish_field_config_submit() form submit handler. However, this handler is only called when fields are created or edited via the UI.

When a FieldConfig entity is created programmatically (e.g., in test setup or install profiles), this submit handler is bypassed, and the setting is never added—resulting in inconsistent behavior.

Steps to reproduce

  1. In a test or custom module, programmatically create a FieldConfig with the require_on_publish setting:
    $field = FieldConfig::create([
      'entity_type' => 'node',
      'bundle' => 'article',
      'field_name' => 'field_tags',
      'label' => 'Tags',
      'required' => FALSE,
    ]);
    $field->setThirdPartySetting('require_on_publish', 'require_on_publish', TRUE);
    $field->save();
    
  2. Observe that the expected behavior (e.g., validation on publish) does not occur unless the setting is manually fetched through getThirdPartySetting() or added after load.
  3. Compare this to fields created through the UI with the "Require on publish" checkbox enabled—the setting is correctly saved and respected.

Proposed resolution

Refactor the logic in require_on_publish_field_config_submit() so that:

  • The actual saving of third-party settings lives in a separate internal function.
  • This function is invoked both:
    • In the form submit handler.
    • In hook_entity_presave() when the field config is being saved.

This ensures that if the require_on_publish form value is passed via $field->setThirdPartySetting() during programmatic creation, it is consistently processed and validated.

📌 Task
Status

Active

Version

2.0

Component

Code

Created by

🇺🇸United States jcandan

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

Comments & Activities

Production build 0.71.5 2024