Score:0

Can't get field storage definition in custom entity

ni flag

I have a custom entity 'myentity' with some fields, and one of them is a taxonomy field with the next definition:

 $fields['colors'] = BaseFieldDefinition::create('entity_reference')
                ->setLabel(t('Colors'))
                ->setDescription(t(''))
                ->setRequired(FALSE)
                ->setSetting('target_type', 'taxonomy_term')
                ->setSetting('handler', 'default:taxonomy_term')
                ->setSetting('handler_settings',
                        array(
                            'target_bundles' => array(
                                'colors' => 'colors'
                    )))                
                ->setDisplayOptions('view', array(
                    'label' => 'above',
                    'type' => 'author',
                    'weight' => 0,
                ))
              ->setDisplayOptions('form', [
                    'type' => 'options_select',
                    'weight' => 0,
                ])
                ->setDisplayConfigurable('form', TRUE)
                ->setDisplayConfigurable('view', TRUE);

It works good and I can use my entity without any problem. But there is a problem:

FieldStorageConfig::loadByName('myentity','colors');

returns NULL.

I don't know why, but I think It should returns my field definition. Because It doesn't work, I have tried with "drush upe" (with this module) that let you ensure the field definition is right, but without success.

4uk4 avatar
cn flag
No, it shouldn't return your base field definition. Only configured fields.
ni flag
Thanks. But what do you mean with only configured fields? I don't understand you.
4uk4 avatar
cn flag
Instead of the base field definition in code you could configure the field in UI or provide the configuration in the module /config/install folder as YAML. Then the command would return a result.
ni flag
Ok. Thanks. I'll try to create the configuration file in install folder. I'm not sure if I could generate it from the current entity definition. Is there any way to do it?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.