0

How to change an address field that says "There is data for this field in the database. The field settings can no longer be changed."?

I want to add a "Company" field as part of the address. I don't want to ditch all existing address data. I don't want to create a separate field.

the problem

1
  • as @GisleHannemyr says you can make a new field. If you want to Edit this field to be Company you must first remove all submitted content using this field.
    – tenken
    Commented Feb 5, 2014 at 19:43

2 Answers 2

0

The message you see is for the settings of an existing field name "Adresse". It tells you that you can no longer change its field settings.

There is nothing that prevents you from adding a new field named "Company" to the content type. Just go to the "Manage fields" tab for the content type, fill in the "Label" (i.e. Company) and other settings for the new field, click "Add new field" and then "Save".

0

This is an case where you know that the change won't destroy existing data, but Drupal doesn't have the ability to figure that out, so it blanket blocks all such requests.

Here's an example that manipulates radio options, but the technique for changing an address field will be fundamentally the same:

/**
 * Updates allowed values for field_custom_radio_buttons.
 */
function mymodule_update_8001(&$sandbox) {
  // Queries for relevant nodes already using that field.
  $nids = \Drupal::entityQuery('node')
    ->condition('type', 'my_custom_content_type')
    ->condition('field_custom_radio_buttons', 'green')
    ->execute();
  // Loads queried nodes.
  // NOTE: If you have a non-trivial number of nodes to modify,
  // you should use the Batch API to drive your update function instead.
  // @see https://api.drupal.org/api/examples/batch_example%21batch_example.install/function/batch_example_update_8001/8.x-1.x
  $nodes = Node::loadMultiple($nids);
  // Updates field_custom_radio_buttons values.
  // Replaces any “green” values with “blue”.
  $updated_value_map = [
    'green' => 'blue',
  ];
  /** @var \Drupal\node\Entity\Node $node */
  foreach ($nodes as $node) {
    $node
      ->set('field_custom_radio_buttons', $updated_value_map[$node->field_custom_radio_buttons->value])
      ->save();
  }

  // Updates field storage config for field_custom_radio_buttons.
  // @see https://www.drupal.org/node/2012896
  // @see https://www.drupal.org/docs/8/api/update-api/updating-entities-and-fields-in-drupal-8#example-updating-a-field-from-an-obsolete-type-to-a-new-type
  $old_config = FieldStorageConfig::loadByName('node', 'field_custom_radio_buttons');
  $new_config = $old_config->createDuplicate();
  $new_config->original = $new_config;
  $new_config->enforceIsNew(FALSE);
  $new_config
    ->setSetting('allowed_values', [
      'red' => 'red',
      'blue' => 'blue',
    ])
    ->save();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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