1

Even though I can see that my data is being saved it doesn't ever appear to be set when I go back to edit my entity via a form. Am I missing something in my setup?

Here's what I have:

      $fields['content_display'] = BaseFieldDefinition::create('list_integer')
          ->setDisplayOptions('form', [
              'type' => 'options_buttons',
              'weight' => -4,
          ])
          ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
          ->setSetting('allowed_values', $values)
          ->setDisplayConfigurable('form', TRUE);
0

You need to set a default value for the field. Drupal's field system by default assumes a field is empty, you will have to manually add a default value to the field definition.

This default value should be based on your entity's value, or be an empty string or NULL in case there is no value yet. Try something like this:

// Use the entity value if available, or NULL otherwise.
$default_value = !empty($entity->my_value) ? $entity->my_value : NULL;
$fields['content_display'] = BaseFieldDefinition::create('list_integer')
  ->setDisplayOptions('form', [
    'type' => 'options_buttons',
    'weight' => -4,
   ])
  ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  ->setSetting('allowed_values', $values)
  ->setDisplayConfigurable('form', TRUE);

  // Set the default value on the field.
  ->setDefaultValue($default_value);
  • Unfortunately I am unable to do this do to the function being public static – Mokugo May 14 '16 at 20:40
  • What function do you mean? I only added the setDefaultValue, which imho should work. If it doesn't, look for something similar. This is the Drupal way of pre-populating form elements. – Neograph734 May 14 '16 at 21:57
  • No, default value is for new entities. That's not what this question is about. Pre-populating based on existing values should just work. Are you sure that your value is actually saved correctly? What is the structure of your allowed values and how does the form look like? – Berdir May 15 '16 at 6:22

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.