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...