Unable to Add new value to select list

Created on 28 March 2024, 9 months ago
Updated 10 June 2024, 6 months ago

Problem/Motivation

When there is a custom field already created and you want to add additional option to select list by "Add Option" it by default tries to create a new field instead and you will receive message "Required field".
Now you need to create some dummy field, save and go back to remove that newly created field.

-The same when you want to remove value option. Just click Remove button and it display message "Required field" so you again need to create a dummy one.

Steps to reproduce

-already described, please check attached images:

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΈπŸ‡°Slovakia coaston

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

Comments & Activities

  • Issue created by @coaston
  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    This is a known issue and being addressed in dev branch. It's functionally better in dev but still needs improvement to get to a release. I'll update this ticket when its fully addressed.

  • Status changed to Fixed 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    fIxed in 3.x dev.

  • πŸ‡ΈπŸ‡°Slovakia coaston

    Thank you,

    I can confirm it works for 3.x dev.

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    @coaston,

    If you have data saved in the db for this field, the storage form is locked from being able to remove existing fields and/or add new ones. All Drupal fields work like this. There is a service available ( https://www.drupal.org/docs/extending-drupal/contributed-modules/contrib... β†’ ) to add/update fields programmatically via update hooks although it hasn't been fully tested with the new reference fields. It will get refined more once we get closer to stable release.

  • Status changed to Fixed 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States apmsooner
  • πŸ‡ΈπŸ‡°Slovakia coaston

    Hi apmsooner,

    Can you help me with this a little bit.
    I do really need to add additional column to my existing custom field.

    Name of custom field is "field_circuit"
    and new column machine name should be "portx" of string (255)

    1. I have created custom_field_updates.info.yml
    2. I have created custom_field_updates.install as follow :

    <?php
    /**
     * @file
     * Install file for the custom_field_updates module.
     */
    
    use Drupal\Core\Entity\EntityTypeManagerInterface;
    use Drupal\field\Entity\FieldStorageConfig;
    use Drupal\field\Entity\FieldConfig;
    
    /**
     * Implements hook_module_install().
     */
    function custom_field_updates_module_install() {
      $entity_type_manager = \Drupal::service('entity_type.manager');
      $field_storage_config = FieldStorageConfig::load('<strong>field_circuit</strong>');
    
      // Check if the field storage configuration exists.
      if ($field_storage_config) {
        // Create an update hook to add the new column.
        $update_hook = 8001; 
        $entity_type_manager->getStorage('update')->create([
          'id' => $update_hook,
          'announce' => 'Add "port" column to "<strong>field_circuit</strong>" field',
          'execute' => 'custom_field_updates_update_' . $update_hook,
        ])->save();
      }
    }
    

    3. I have created custom_field_updates.update.php as follow:

    <?php
    /**
     * @file
     * Update hook for the custom_field_updates module.
     */
    
    use Drupal\Core\Database\Connection;
    
    /**
     * Adds the "port" column to the "field_circuit" field storage.
     */
    function custom_field_updates_update_8001(): void { 
    /** @var \Drupal\custom_field\CustomFieldUpdateManagerInterface $update_manager 
    */ 
    $update_manager = Drupal::service('custom_field.update_manager'); 
    $update_manager->addColumn('node', '<strong>field_circuit</strong>', '<strong>portx</strong>', 'string', ['length' => 255, 'machine_name' => TRUE]); } 
    
    

    When I enable module, i still cannot see new column in field_circuit and also in database. (I also tried with 9001 instead of 8001)

    What I am missing ?

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    I'm not sure what you are doing with the 2 files. I would just put your update in your custom module .install file like example:

    /**
     * Adds the "port" column to the "field_circuit" field storage.
     */
    function custom_field_updates_update_10001(): void {
      /** @var \Drupal\custom_field\CustomFieldUpdateManagerInterface $update_manager */
      $update_manager = Drupal::service('custom_field.update_manager');
      $update_manager->addColumn('node', 'field_circuit', 'portx', 'string');
    }
    

    Not sure why you're wrapping the field name with html... thats definitely wrong. You shouldn't need any other options in this case either. Example above should just work for you. The sequencing number doesn't really matter but if you're on Drupal 10, I'd probably suggest 10001 as example. The updates just need to stay in order so if you add another one later... it would be custom_field_updates_update_10002()

  • πŸ‡ΈπŸ‡°Slovakia coaston

    Thank you..

    Does it mean I just need 2 following files :
    custom_field_updates.info.yml

    and
    custom_field_updates.install

    with code:

    <?php
    /**
     * Adds the "port" column to the "field_circuit" field storage.
     */
    function custom_field_updates_update_10001(): void {
      /** @var \Drupal\custom_field\CustomFieldUpdateManagerInterface $update_manager */
      $update_manager = Drupal::service('custom_field.update_manager');
      $update_manager->addColumn('node', 'field_circuit', 'portx', 'string');
    }
    
    

    Because I just tried and enabled module ,run update.php and cleared cache but I don't see any difference. Still when I visit field_circuit I am unable to see portx field item there.

    I also tried to remove already existing "decom_id" item from field_circuit but also once I enable module (custom_field_updates.install) with following code of , nothing happens :

    /**
     * Remove a column from custom_field.
     */
    function my_module_update_10002(): void {
      /** @var \Drupal\custom_field\CustomFieldUpdateManagerInterface $update_manager */
      $update_manager = Drupal::service('custom_field.update_manager');
      $update_manager->removeColumn('node', 'field_circuit', 'decom_id');
    }
    
    
  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    Did you run update.php? These update hooks don't just run by enabling the module, you have to run update.php.

  • πŸ‡ΈπŸ‡°Slovakia coaston

    Yes i have also mentioned that in my previous answer. Not sure what i am missing here. I have spent hours to make it work already:(

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    Maybe try uninstalling the module, remove or comment out the update hooks from .install file, then enable the module. After module is enabled, add back in the update hooks and try running update.php again to see if they are picked up in pending updates. It sounds like maybe having them present before the module is installed may prevent them from being recognized.

  • πŸ‡ΈπŸ‡°Slovakia coaston

    Bingo!. Finally.

    It sounds like maybe having them present before the module is installed may prevent them from being recognized.

    That is exactly the issue. It seems every time when you want to add/remove column you have to comment hooks out - enable module and later uncomment and run update.php.

    Thank you

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    Glad you got it working! As for...

    It seems every time when you want to add/remove column you have to comment hooks out - enable module and later uncomment and run update.php.

    No, this is incorrect. The issue was that you just didnt have your custom module enabled prior to the update hook being added. Keep it enabled and do NOT remove the existing update hook, just add new ones to it with the next sequence number. It's important to retain the correct sequence order. If you're last one is: custom_field_updates_update_10002, then your next one is custom_field_updates_update_10003.

Production build 0.71.5 2024