0

I have a custom entity that fieldable. I need to add a field to it, and I want to do it through the UI. The entity was created in a custom module, which I can modify.

How can I make the entity fieldable?

/**
 * Defines the Quiz entity.
 *
 * @ingroup qwizard
 *
 * @ContentEntityType(
 *   id = "qwiz",
 *   label = @Translation("Quiz"),
 *   bundle_label = @Translation("Quiz type"),
 *   handlers = {
 *     "storage" = "Drupal\qwizard\QwizStorage",
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\qwizard\QwizListBuilder",
 *     "views_data" = "Drupal\qwizard\Entity\QwizViewsData",
 *
 *     "form" = {
 *       "default" = "Drupal\qwizard\Form\QwizForm",
 *       "add" = "Drupal\qwizard\Form\QwizForm",
 *       "edit" = "Drupal\qwizard\Form\QwizForm",
 *       "delete" = "Drupal\qwizard\Form\QwizDeleteForm",
 *     },
 *     "access" = "Drupal\qwizard\QwizAccessControlHandler",
 *     "route_provider" = {
 *       "html" = "Drupal\qwizard\QwizHtmlRouteProvider",
 *     },
 *   },
 *   base_table = "qwiz",
 *   revision_table = "qwiz_revision",
 *   revision_data_table = "qwiz_field_revision",
 *   admin_permission = "administer quiz entities",
 *   entity_keys = {
 *     "id" = "id",
 *     "revision" = "vid",
 *     "bundle" = "type",
 *     "label" = "name",
 *     "uuid" = "uuid",
 *     "uid" = "user_id",
 *     "langcode" = "langcode",
 *     "status" = "status",
 *   },
 *   links = {
 *     "canonical" = "/admin/structure/qwizard/qwiz/{qwiz}",
 *     "add-page" = "/admin/structure/qwizard/qwiz/add",
 *     "add-form" = "/admin/structure/qwizard/qwiz/add/{qwiz_type}",
 *     "edit-form" = "/admin/structure/qwizard/qwiz/{qwiz}/edit",
 *     "delete-form" = "/admin/structure/qwizard/qwiz/{qwiz}/delete",
 *     "version-history" = "/admin/structure/qwizard/qwiz/{qwiz}/revisions",
 *     "revision" = "/admin/structure/qwizard/qwiz/{qwiz}/revisions/{qwiz_revision}/view",
 *     "revision_revert" = "/admin/structure/qwizard/qwiz/{qwiz}/revisions/{qwiz_revision}/revert",
 *     "revision_delete" = "/admin/structure/qwizard/qwiz/{qwiz}/revisions/{qwiz_revision}/delete",
 *     "collection" = "/admin/structure/qwizard/qwiz",
 *   },
 *   bundle_entity_type = "qwiz_type",
 *   field_ui_base_route = "entity.qwiz_type.edit_form"
 * )
 */
4
  • Do you have bundle_entity_type set in your entity annotation?
    – Clive
    May 13 at 18:28
  • 1
    Actually if you could edit the question and copy/paste the whole annotation that would be helpful
    – Clive
    May 13 at 18:30
  • If the answer to the question of @Clive is no and you want to make a simple entity field-able: drupal.stackexchange.com/questions/283826/…
    – 4k4
    May 13 at 20:04
  • The annotation is missing the value for data_table. The annotation isn't sufficient to make an entity fieldable; the entity class must also implement a specific interface. What's the exact issue you are having, though? The question just says what you want to achieve, not the problem you need to resolve. Is entity.qwiz_type.edit_form the correct route for the bundle edit form?
    – apaderno
    Jul 24 at 22:06
1

You just need to add a field_ui_base_route to the entity annotation. The value should be the route ID for the edit form of the config entity type used to provide the bundles.

So if your entity ID is foo and the bundle config entity ID is foo_type, that would be:

@ContentEntityType(
  id = "foo",
  ...
  field_ui_base_route = "entity.foo_type.edit_form"
)

Rebuild the cache, and you should see the relevant menu links and tabs when you manage your bundles in the UI.

1
  • Hi Clive, I added the annotation to the original post. It does have the field_ui_base_route set. Thanks. May 14 at 17:58

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