2

I have a list field in the content type and in the config table , the field storage details saved as follows :

collection:
name: field.storage.node.field_ip_range_sec_zone
data: a:17:{s:4:"uuid";s:36:"9d20e561-e39c-4c11-b100-ff3a29a75267";s:8:"langcode";
                                            s:2:"en";s:6:"status";b:1;s:12:"dependencies";a:1:{s:6:"module";a:3{i:0;s:17:
                                        "field_permissions";i:1;s:4:"node";i:2;s:7:"options";}}s:20:"third_party_settings";
                                    a:1:{s:17:"field_permissions";a:1:{s:15:"permission_type";s:6:"public";}}s:2:"id";
                                s:28:"node.field_ip_range_sec_zone";s:10:"field_name";s:23:"field_ip_range_sec_zone";
                            s:11:"entity_type";s:4:"node";s:4:"type";s:11:"list_string";s:8:"settings";a:2:
                        {s:14:"allowed_values";a:3:{i:0;a:2:{s:5:"value";s:2:"E1";s:5:"label";s:2:"E1";}
                i:1;a:2{s:5:"value";s:2:"C2";s:5:"label";s:2:"C2";}i:2;a:2{s:5:"value";s:2:"D1";s:5:"label";s:2:"D1";}}
            s:23:"allowed_values_function";s:0:"";}s:6:"module";s:7:"options";s:6:"locked";b:0;s:11:
        "cardinality";i:1;s:12:"translatable";b:1;s:7:"indexes";a:0:{}s:22:"persist_with_no_fields";
    b:0;s:14:"custom_storage";b:0;}

As you can see there is Allowed values (E1,C2,D1) , and now i want to add an additional value to the allowed values , How can i do that ?

WHAT I HAVE TRIED


I could get the allowed values using the following :

$entity_type = 'node';
$bundle = 'ip_range';
$field_name = 'field_ip_range_sec_zone';
$ip_sec_zone_storage_values = \Drupal\field\Entity\FieldStorageConfig::loadByName($entity_type, $field_name);

But dont know how to update it , Any idea ??

3

Those are settings, you get them using $ip_sec_zone_storage_values->getSetting('allowed_values'),, in there you'll find those settings, you can add another one, set the settings back with setSetting('allowed_values', $allowed_values) and then ->save() to save the field storage config:

$allowed_values = $ip_sec_zone_storage_values->getSetting('allowed_values');
// The new value.
$allowed_values[$ipseczone] = $ipseczone;
$ip_sec_zone_storage_values->setSetting('allowed_values', $allowed_values);
$ip_sec_zone_storage_values->save();
2
  • Not clear on the save part.. Is it ip_sec_zone_storage_values->setSettings("custom values") ?
    – KTM
    Oct 26 '16 at 3:25
  • place it as an answer , i will accept it , iused it like : $allowed_values = $ip_sec_zone_storage_values->getSetting('allowed_values'); $allowed_values[$ipseczone] = $ipseczone;//the new value i want to add $ip_sec_zone_storage_values->setSetting('allowed_values', $allowed_values); $ip_sec_zone_storage_values->save();
    – KTM
    Oct 26 '16 at 8:04
0

After many hours i finally found solution. I dynamically created options for multi select field and got error message on submit that choosen select in wrong option.

It requires add new options to allowed_values for that field. Here is the solution:

 //load your field
 $field_purchasers = \Drupal\field\EntityFieldStorageConfig::loadByName('user', 'field_purchaseres');
 //subscribe new options to this field
$field_purchasers->setSetting('allowed_values_function','module_name_allowed_values_function' );
//save configuration
$field_purchasers->save();

In your .module file write in callback function :

function module_name_allowed_values_function(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
  $options = [
    1 => 'My new option 1',
    2 => 'My new option 2',
    3 => 'My new option 3',
   ];
  return $options;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.