6

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?

10

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

  • Not sure why this got a downvote, it's right... – Clive Sep 14 '13 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 '13 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. – kiamlaluno Sep 14 '13 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 '13 at 7:05
7

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.

  • This is now the good way to go. – Kgaut Aug 5 '15 at 14:49
1

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

  • 2
    Thanks for answering, Chris. It would be helpful if you enhance your answer with more detailed instructions, or references to documentation. – marcvangend Jan 26 '16 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 at 21:35
1

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;
  }
}

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.