I created a custom content entity, called member, with some fields of which one, called status, is a list appearing in form as a select:

$fields['status'] = BaseFieldDefinition::create('list_integer')
  ->setLabel(t('Statut'))
  ->setRequired(TRUE)
  ->setSettings(array(
    'allowed_values' => array(
      0 => 'Ancien',
      1 => 'Actif',
      2 => 'Non renouvelée',
      3 => 'Renouvelée',
      4 => 'En attente',
    ),
  ))
  ->setDefaultValue(4)
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'weight' => 7,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'options_select',
    'weight' => 7,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

I'd like to put the definition of the allowed values 'somewhere else' (meaning not directly in the definition of the member entity).
I know I could create another content entity (e.g. possible_status) that I would reference this way:

$fields['status'] = BaseFieldDefinition::create('entity_reference')
  ->setSetting('target_type', 'possible_status)')

But it seems a bit 'rich' to handle a couple of values...
Is there another way to store the values 'outside' the code defining the member entity?
An include file, a text file, a JSON file I could import?
What would be the best practice for that?

[EDIT]
I changed for this:

$fields['status'] = BaseFieldDefinition::create('list_integer')
  ->setLabel(t('Statut'))
  ->setRequired(TRUE)
  ->setSettings(['allowed_values_function' => 'association_allowed_values_function'])
  ->setDefaultValue(4)

with this in my module:

function association_allowed_values_function(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {

  if ($definition['entity_type'] == 'member' && $definition'field_name' == 'status') {
    $values = [
      0 => 'Ancien',
      1 => 'Actif',
      2 => 'Non renouvelée',
      3 => 'Renouvelée',
      4 => 'En attente',
    ];
  }
  return $values;
}

and it works!

Now, I'd like to move association_allowed_values_function to an include file. And I don't know how...

up vote 2 down vote accepted

Instead of allowed_values you can add a callback to set the allowed values:

  ->setSettings(['allowed_values_function' => 'callback_allowed_values_function'])

The callback:

function callback_allowed_values_function(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
  if (isset($entity) && ($entity->bundle() == 'not_a_programmer')) {
    $values = [
      1 => 'One',
      2 => 'Two',
    ];
  }
  else {
    $values = [
      'Group 1' => [
        0 => 'Zero',
        1 => 'One',
      ],
      'Group 2' => [
        2 => 'Two',
      ],
    ];
  }

  return $values;
}

You can get the allowed values from a text file or put the callback in an include file.

For more information see options.api.php


A different approach might be to store the allowed values in config, for which there is a bundle specific base field override, see How to easily alter an entity's base field definition per bundle. Then you can manage the allowed values list in a yaml file of an exported config.

  • I changed my code following your help but I still have something to deal with... (see my updated question) – gbmapo Oct 11 at 12:20
  • \Drupal::moduleHandler()->loadInclude('mymodule', 'inc', 'mymodule.allowed_values'); place this in the callback and then get the values from another function inside of the include file. – 4k4 Oct 11 at 12:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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