How to Update Configuration Entity in Drupal 9+?

Last updated on
17 February 2024

This page has not yet been reviewed by Configuration API maintainer(s) and added to the menu.

This documentation needs review. See "Help improve this page" in the sidebar.

In Drupal 9+, updating the existing Configuration Entity is a bit different than in Drupal 8. Drupal 8 has drush entity:updates, drush entup, drush entity-updates the command to update the existing entity schema but Drupal 9+ doesn't have these commands. See the overview of Configuration API.

How to update Configuration Entity in Drupal 9+?

Steps to update Configuration Entity in Drupal 9+:

1. Suppose you have custom_module with project_config_entity Configuration Entity.

2. You want to add two new fields configuration and json_configuration in the custom Project Config Entity.

3. Default configuration when generating by drush generate configuration-entity of Project Config Entity

File: custom_module/config/schema/custom_module.schema.yml

custom_module.project_config_entity.*:
  type: config_entity
  label: Project Configuration Entity
  mapping:
    id:
      type: string
      label: ID
    label:
      type: label
      label: Label
    uuid:
      type: string
    description:
      type: string

4. Add new fields in the custom_module/config/schema/custom_module.schema.yml file as below:

custom_module.project_config_entity.*:
  type: config_entity
  label: Project Configuration Entity
  mapping:
    id:
      type: string
      label: ID
    label:
      type: label
      label: Label
    uuid:
      type: string
    description:
      type: string
    configuration:
      type: string
    json_configuration:
      type: string

5. Add fields in the custom_module/src/Entity/ProjectConfigEntity.php file under the config_export key as below:

 *   config_export = {
 *     "id",
 *     "label",
 *     "description",
 *     "configuration",
 *     "json_configuration"
 *   }

6. Add new fields in the class ProjectConfigEntityForm of custom_module/src/Form/ProjectConfigEntityForm.php file as below:

    $form['configuration'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Configuration'),
      '#default_value' => $this->entity->getConfiguration(),
      '#description' => $this->t('Configuration in text format.'),
    ];

    $form['json_configuration'] = [
      '#type' => 'textarea',
      '#title' => $this->t('JSON Configuration'),
      '#default_value' => $this->entity->getJsonConfiguration(),
      '#description' => $this->t('Configuration in the JSON format.'),
    ];

7. Declare methods in the class ProjectConfigEntityInterface of custom_module/src/ProjectConfigEntityInterface.php file as below:

namespace Drupal\custom_module;

use Drupal\Core\Config\Entity\ConfigEntityInterface;

/**
 * Provides an interface defining a project configuration entity type.
 */
interface ProjectConfigEntityInterface extends ConfigEntityInterface {

  /**
   * Declare function to get the configuration.
   */
  public function getConfiguration();

  /**
   * Declare function to get JSON configuration.
   */
  public function getJsonConfiguration();
}

8. Add members variables & methods in the class ProjectConfigEntity of custom_module/src/Entity/ProjectConfigEntity.php file as below:

  /**
   * The project_config_entity configuration field.
   *
   * @var string
   */
  protected $configuration;

  /**
   * The project_config_entity json_configuration field.
   *
   * @var string
   */
  protected $json_configuration;

  /**
   * Get Configuration value.
   *
   * @return string
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * Get Configuration in JSON format.
   *
   * @return JSON
   */
  public function getJsonConfiguration() {
    return $this->json_configuration;
  }

9. Do drush cr and open the Form /admin/structure/project_config_entity/add and see the new fields.

10: Verify your changes in the Edit mode as below:

Help improve this page

Page status: Needs review

You can: