8

I've created an entity in a Drupal 8 custom module. Right now it's just an entity with base fields.

I want to expose these base fields to views.

I have the entity appearing on the views wizard page, but when I create the view, there are no base fields available for me to add, and the default field that views puts in there shows up as broken/missing.

I've tried referencing the comments module for examples, and have tried defining various views plugins, but I'm not having much luck.

I have baseFieldDefinitions($entity_type) defined in my entity controller. What else is required to expose this entity and its fields to views?

6 Answers 6

11

You need to implement hook_views_data() just like in Drupal 7. See comment.views.inc.

5
  • Not sure why this got a downvote, it's right...
    – Clive
    Sep 14, 2013 at 22:16
  • Me neither, worked great. Can't believe I missed that file. I was too focused on the new Classes. Thanks!
    – Coder1
    Sep 14, 2013 at 22:46
  • 1
    @Clive I can only guess. Maybe the fact it didn't have any link to the documentation could be the reason. If that is the case, what the user should have done is adding the links, as I did.
    – apaderno
    Sep 14, 2013 at 23:20
  • 3
    Yes, this is right now the correct answer. Drupal 8 does not yet contain the 7.x entity.module feature that automatically generates the views data definitions based on the entity schema/field definitions. The issues to add it is drupal.org/node/1740492 (and drupal.org/node/1792828 is a views/entity meta issue), so follow that if you're interested in it.
    – Berdir
    Sep 15, 2013 at 7:05
  • because you need implement \Drupal\views\EntityViewsData and add it in entity aanotation Feb 23, 2021 at 13:02
10

This is now possible via a class annotation; per the documentation for the hook mentioned in the other answer:

To provide views data for an entity, instead of implementing this hook, create a class implementing \Drupal\views\EntityViewsDataInterface and reference this in the "views" annotation in the entity class. The return value of the getViewsData() method on the interface is the same as this hook.

2
  • 2
    This is now the good way to go.
    – Kgaut
    Aug 5, 2015 at 14:49
  • Directly available if the custom entity is generated with drupal console
    – pbonnefoi
    Nov 4, 2020 at 15:33
3

You should extend EntityViewsData and you'll have the integration of your entity.

Example:

use Drupal\views\EntityViewsData;

class CustomEntityViewsData extends EntityViewsData {
  public function getViewsData() {
    $data = parent::getViewsData();
    return $data;
  }
}
2

If you use Drupal Console's Content entity generator it will automatically include Views integration.

2
  • 2
    Thanks for answering, Chris. It would be helpful if you enhance your answer with more detailed instructions, or references to documentation. Jan 26, 2016 at 8:34
  • As per Patricks example this is automatically generated in web/modules/custom/<yourmodule>/src/Entity/TeamsEntityViewsData.php It is then available from the views ui landing page admin/structure/views. When you create a new view your new entity should be selectable. Wow this question is old so the goto method might have changed.
    – Daniel
    Oct 4, 2019 at 21:35
1

This is an example of a custom entity with views integration:

/**
 * Defines the logbook entity.
 *
 * @ingroup logbook
 *
 * @ContentEntityType(
 *   id = "logbook",
 *   label = @Translation("Logbook"),
 *   base_table = "logbook",
 *   data_table = "logbook_field_data",
 *   fieldable = TRUE,
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "owner" = "uid",
 *     "uid" = "uid",
 *   },
 *   handlers = {
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "access" = "Drupal\logbook\LogbookAccessControlHandler",
 *   },
 *   field_ui_base_route="entity.logbook.settings"
 * )
 */
0

you need implement \Drupal\views\EntityViewsData and add it in entity aanotation

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.