0

I have created a form that adds a subset of field types to an entity (does not support bundles). (the reason for the custom form is that end users find the Drupal standard 'Manage fields' workflow too difficult) The class extends FormBase. When the 'submitForm' function is executed, the field is saved but not added to the entity.

This is the code to save a date field (with text values filled in for explanation purposes):

$field_storage_values = [
  'field_name' => 'field_test_date',
  'entity_type' => 'my_entity_id',
  'type' => 'datetime',
  'translatable' => TRUE,
];

$field_values = [
  'field_name' => 'field_test_date',
  'entity_type' => 'my_entity_id',
  'bundle' => 'my_entity_id',
  'label' => 'Test Date',
  'translatable' => FALSE,
];

$this->entityTypeManager->getStorage('field_storage_config')->create($field_storage_values)->save();
$field = $this->entityTypeManager->getStorage('field_config')->create($field_values);
$field->save();

When the form is submitted, the field is created correctly but not added to the entity.

When I export the field configuration to YML, the following appears under dependencies:

dependencies:
  config:
    - field.storage.my_entity_id.field_test_date

Creating an identical Node field results in the following field configuration YML dependencies:

dependencies:
  config:
    - field.storage.node.field_test_date
    - node.type.basic_page

I suspect the field created with my custom form should also add the second configuration dependency like:

my_entity_id.type.my_entity_id

I tried adding the following before the field is saved but the 'addDependency' function is protected.

$field->addDependency('config', 'my_entity_id.type.my_entity_id');
0
1

You can't add this dependency, it's bundle configuration which you don't have and don't need in a bundleless entity. For testing better add a user instead of a node field or check the already pre-installed field.field.user.user.user_picture.yml

To create the field config you only need to specifiy the field storage and the bundle (for a bundleless entity this is the entity id). The other values are then extracted from the storage config:

$storage = $this->entityTypeManager->getStorage('field_storage_config')->create($field_storage_values);
$storage->save();

$field = $this->entityTypeManager->getStorage('field_config')->create([
  'field_storage' => $storage,
  'bundle' => 'my_entity_id',
]);
$field->save();

BTW part of the field UI is also the configuration of display modes. You need to add the new field as component at least to the default view and form mode, so that the new field is visible. See How to change the entity view display configuration programmatically?

2
  • Thanks again for helping me through this. The fields are almost fully functional. The only thing that doesn't work is that the added fields do not appear as options on the 'Add fields' when I try to add them to a view. The submit method includes $this->entityDisplayRepository->getViewDisplay('my_entity', 'my_entity')->setComponent($storage->getName(), ['label' => 'inline'])->save(). I looked at the link you included but I'm not sure what else needs to be done to get the added fields to appear in Views.
    – user320691
    Apr 27 at 17:03
  • This is for adding the field to a view mode. Not connected with Views. For Views you need a views_data handler. See drupal.stackexchange.com/questions/302392/…
    – 4k4
    Apr 27 at 19:46

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.