I have used the content entity example coming from the example project.

This example define a Contact entity.

Next I have extended this Contact class in an Artist class with some added fields (let say a Bio and a Cv field).

class Artist extends Contact  {

My Idea was to share the list_builder defined in the Contact class in order to get an administrative list showing the Contacts and the Artists at the same place.

To do so, I have those annotations in the Artist class:

 * @ContentEntityType(
  * id = "mymodule_artist",
  * label = @Translation("Artist entity"),
  * handlers = {
  * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
  * "list_builder" = "Drupal\mymodule\Entity\Controller\ContactListBuilder",
  * ...

As you can see, ContactListBuilder is the list builder of the Contact entity... in other words, it is used in Contact and in Artist.

In the routing.yml I have the following

  path: '/mymodule_contact/list'
    _entity_list: 'mymodule_contact'
    _title: 'Contact List'
    _permission: 'view contact entity'

Unfortunately, this is not working at all... the ContactListBuilder shows only the Contact entities; not the Artists. (I couldn't understand why)

After many tries, I drop this idea... My new plan was now to create a view that will show all Contacts and all Artists.

Thus, I added this line in the annotations of the Contact and the Artist classes (in the "handlers" section):

 * "views_data" = "Drupal\mymodule\ContactViewsData",

And here is my ContactViewsData class definition

class ContactViewsData extends EntityViewsData  {

As you can see it is empty: it only extends EntityViewsData. In fact I really don't understand what to do in the getViewsData() function. I couldn't find any doc on it (But this is maybe something I should ask in another question)

Anyway, this does the job, and I could create a view (in the UI) for the Contacts and another view for the Artists. But I have no clue on how to create a view that are showing both.

To summarize, my questions are:

  1. Is extending the Contact class (for the Artists) a good idea? Maybe it is much simpler to just add the Artist's fields in the Contact Class.
  2. What is the best way to list a Mix of 2 entities that are sharing some common fields (Ex: Name, Surname, ID,...): Is it a view?
  3. Is it a good Idea to try to use the same ListBuilder for 2 entities (Of course, this Listbuilder will only list the common fields)
  4. Where to find some doc on the getViewsData()` function, or at least, what is the purpose of this function.

Your questions cover quite a few unrelated concepts, but here's an attempt at answering nonetheless.

  1. Create a 'Person' base class and extend that into 'Contact' and 'Artist' classes. This approach will isolate your Artist entity type form changes you need to make to the Contact class and vice versa. Adding a company-field would, as an example, probably not apply to your artists.
  2. I would use a content-view with a filter, only showing contacts and artists types. Re-using common fields on the 2 entities will make creating such a view a no-brainer.
  3. Afaik the parent list-builder class does not support multiple entity types. The constructor for the list-builder class, accepts $entity_type as one of its arguments, so unless you write your own list-builder, you are limited to 1 type per list.
  4. The getViewsData() method exposes properties from your entity type to the views api. Extending the EntityViewsData class, like you did, auto-generates the view-data through the inherited methods. Core provides some usage examples; e.g. UserViewsData. Use your IDE to search for "extends getViewsData()" for more examples.

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.