0

I have created an entity, contactdata, and am trying to expose this entity to views. I have created my own views_data handler class, contactdataEntityViewsData, and have annotated my custom entity to point the views_data hander to this class. I put a breakpoint in this class and I know that it is being invoked.

Here is the code in my contactdataEntityViewsData class:

<?php
/**
 * @file
 */

namespace Drupal\contactdata;

use Drupal\views\EntityViewsData;

class contactdataEntityViewsData  extends EntityViewsData  {
  /**
   * {@inheritdoc}
   */
public function getViewsData() {
  $data = parent::getViewsData();
  return $data;
}
}

Symptoms: The views UI can "see" my contactdata entity and will allow me to create a table with both base fields from the entity and non-base fields that I have added to it. However, when I run the view I get a table with the correct headers but with no data.

I am using the Creative Responsive theme on my site, but have also reset the default theme to Bartik, and the results are identical: a table with headers but no data.

I am running Drupal 9.2.4 with PHP 7.4.16 and mysql 5.7.30.

6
  • As your ViewsData class isn't doing anything, you don't even need to implement it, you can leave your annotation in the entity class as "views_data" = "Drupal\views\EntityViewsData",. But I can't say why you aren't getting any data, have you actually created any entities? Maybe analyze the Views query to see if something stands out.
    – Jaypan
    Jul 7 at 18:01
  • Thanks Jaypan. I had already tried this, and the results are the same. And yes, I have created many entities; I can see them when I look at my contactdata database, and when I run a query directly on that database I see results. The views query is a bit peculiar, because it looks like it is selecting just the id from the contactdata table. Here it is: SELECT "contactdata"."id" AS "id" FROM {contactdata} "contactdata" LIMIT 100 OFFSET 0 Jul 7 at 18:07
  • Further info: If I show contactdata entities instead of fields, I see the contactdata entities that I have created. If I show fields, and type "Nothing Found" in the No Results Behavior dropdown, I see Nothing Found. Jul 7 at 18:54
  • Finally, if I create a preprocess function creative_responsive_theme_preprocess-views_view(&$variables), put a breakpoint in this preprocess function and then examine $variables, I can see that in $variables[rows][0] I have all of the field values for the first entity, in $variables[rows][1] I have all of the field values for the second entity, etc. Jul 7 at 19:00

2 Answers 2

0

Since you don't have anything of your own in contactdataEntityViewsData class you can delete your contactdataEntityViewsData.php file.

In your entity definition class that extends ContentEntityBase you should have handlers in the annotations that point to "Drupal\views\EntityViewsData". That should be enough for views to pick up your entity. Here is an example:

/**
 * Defines the Contact Data entity.
 *
 * @ContentEntityType(
 *   id = "ws_contact_data",
 *   label = @Translation("Contact Data"),
 *   base_table = "ws_contact_data",
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "subscribers" = "subscribers",
 *     "hash" = "hash",
 *     "data" = "data",
 *   },
 *   handlers = {
 *    "views_data" = "Drupal\views\EntityViewsData",
 *     "form" = {
 *       "default" = "Drupal\Core\Entity\ContentEntityForm",
 *       "add" = "Drupal\Core\Entity\ContentEntityForm",
 *       "edit" = "Drupal\Core\Entity\ContentEntityForm",
 *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
 *     },
 *   },
 *   links = {
 *     "canonical" = "/contact_data/{contact_data}",
 *     "add-page" = "/contact_data/add",
 *     "edit-form" = "/contact_data/{contact_data}/edit",
 *     "delete-form" = "/contact_data/{contact_data}/delete",
 *     "collection" = "/admin/content/contact_data",
 *   },
 *   fieldable = TRUE,
 *   admin_permission = "administer site configuration",
 *   field_ui_base_route = "entity.contact_data.edit_form",
 * )
 */

class ContactData extends ContentEntityBase implements ContactDataInterface {
...
5
  • Thanks Dmitry. Tried all of these suggestions but the results are still the same: I get a table with headers but no data. Question: what is the ContactSearch class at the bottom of your comment? I haven't seen anything that indicates I need to create that? Jul 7 at 19:25
  • How did you create your custom entity? If you did it in code you must have this class. Go to core’s user or node module and search for “extends ContentEntityBase” and you will see an example how to do it. Or check this out: drupal.org/docs/drupal-apis/entity-api/… Jul 8 at 22:29
  • My entity is called contactdata and I have a class definition that looks like this: ` class contactdata extends ContentEntityBase implements ContactDataInterface ` Jul 9 at 17:36
  • I installed twig_xdebug and put a breakpoint in my theme's views-view-table.html.twig so that I could examine the rows variable that is passed in to that file. Sure enough, when I look at $context[rows][0][columns][field_ldname][content][0][field_output][#markup] it has no value for the field. Interestingly, I constructed another view, this time of the system-defined users entity, and this displays results just fine. So it looks like something is wrong with my contactdata entity. Jul 10 at 14:46
  • My contactdata entity is not translatable, and I have not provided a data_table annotation. I'm wondering if I should try that and specify a data_table that is the same as my base table? One more comment. I should add that I can do CRUD operations on my contactdata entity just fine. The only problem is that I cannot use views to display base and added fields of the entity. Jul 10 at 14:54
0

Finally got this. I had not realized that when Views displays an entity, it constructs an EntityViewDisplay object, uses that object to create a render array, and allows other modules to alter that render array. I had created a hook_entity_display_build_alter method in a completely different module, this method was being invoked by views, and hence my problem.

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.