2

I've got a custom entity with bundles that I want to be able to use in an entity reference field in a different entity. So, I've got an the content entity for my entity type:

/**
 * Defines the Redacted teaser entity.
 *
 * @ingroup redacted
 *
 * @ContentEntityType(
 *   id = "redacted_teaser",
 *   label = @Translation("Redacted teaser"),
 *   bundle_entity_type = "redacted_teaser_bundle",
 *   bundle_label = @Translation("Content type"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\redacted\RedactedTeaserListBuilder",
 *     "views_data" = "Drupal\redacted\Entity\RedactedTeaserViewsData",
 *   
 *     "form" = {
 *       "default" = "Drupal\redacted\Entity\Form\RedactedTeaserForm",
 *       "add" = "Drupal\redacted\Entity\Form\RedactedTeaserForm",
 *       "edit" = "Drupal\redacted\Entity\Form\RedactedTeaserForm",
 *       "delete" = "Drupal\redacted\Entity\Form\RedactedTeaserDeleteForm",
 *     },
 *     "access" = "Drupal\redacted\RedactedTeaserAccessControlHandler",
 *     "translation" = "Drupal\node\NodeTranslationHandler",
 *   },  
 *   base_table = "redacted_teaser",
 *   admin_permission = "administer RedactedTeaser entity",
 *   translatable = FALSE,
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid", 
 *     "bundle" = "type",
 *     "langcode" = "langcode",
 *     "user_id" = "user_id",
 *     "created" = "created",
 *     "changed" = "changed",
 *   },
 *   links = {
 *     "canonical" = "/admin/redacted_teaser/{redacted_teaser}",
 *     "edit-form" = "/admin/redacted_teaser/{redacted_teaser}/edit",
 *     "delete-form" = "/admin/redacted_teaser/{redacted_teaser}/delete"
 *   },
 *   field_ui_base_route = "entity.redacted_teaser_bundle.edit_form",
 * )
 */

And the config entity for my bundles:

/**
 * Defines the Redacted teaser bundle entity.
 *
 * @ConfigEntityType(
 *   id = "redacted_teaser_bundle",
 *   label = @Translation("Redacted teaser bundle"),
 *   bundle_of = "redacted_teaser",
 *   handlers = {
 *     "list_builder" = "Drupal\redacted\RedactedTeaserBundleListBuilder",
 *     "form" = {
 *       "add" = "Drupal\redacted\Form\RedactedTeaserBundleForm",
 *       "edit" = "Drupal\redacted\Form\RedactedTeaserBundleForm",
 *       "delete" = "Drupal\redacted\Form\RedactedTeaserBundleDeleteForm",
 *       "default" = "Drupal\redacted\Entity\Form\RedactedTeaserForm"
 *     }
 *   },
 *   config_prefix = "redacted_teaser_bundle",
 *   admin_permission = "administer site configuration",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid"
 *   },
 *   links = {
 *     "canonical" = "/admin/structure/redacted_teaser_bundle/{redacted_teaser_bundle}",
 *     "edit-form" = "/admin/structure/redacted_teaser_bundle/{redacted_teaser_bundle}/edit",
 *     "delete-form" = "/admin/structure/redacted_teaser_bundle/{redacted_teaser_bundle}/delete",
 *     "collection" = "/admin/structure/visibility_group"
 *   }
 * )
 */

I then try to use that in an entity reference field in another entity:

->setSetting('target_type', 'redacted_teaser')

But when I go to create an instance of this entity, the reference field isn't getting any of my bundle instances.

I've also try adding an entity reference field to a content type in the UI. The field settings recognize my defined bundles when I'm setting up the field, but when I go to create a node of that content type the entity reference field doesn't pick up any of my bundles instances there, either. I've compared my annotations to those in Node and NodeType and don't see that I'm missing anything. I've also stepped through with Xdebug, and I can see that when my create form loads, it's getting "redacted_teaser" as the expected type for the reference field.

Everything else about my entity and bundles works just as expected.

3
  • can you clarify "isn't getting any bundle instances"? what exactly doesn't it show and where? Can you include the classname and parent class in the code example?
    – Berdir
    Aug 5, 2016 at 21:21
  • In the edit form for the entity where I want to reference my custom entity, nothing shows up when I start entering characters in the autocomplete field. Ditto for a regular content type entity reference field (created in /admin/structure/types) that tries to use my custom entity. In both cases, I can use node as the target type and things work fine. Aug 6, 2016 at 12:42
  • I used drupal console to generate both classes, the only mods to that are in the annotations. Aug 6, 2016 at 12:47

1 Answer 1

1

You have no label key, so autocomplete doesn't know what to search for.

1
  • Thank you! Especially for pointing out why autocomplete wasn't working. Aug 7, 2016 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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