0

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

This example module/:

  • defines a "Contact" entity; it can have extra fields, displays can be configured (web/admin/structure/content_entity_example_contact_settings),
  • gives the user the form to add content (contact info) to it. (/web/content_entity_example_contact/add)
  • shows a list of the contacts (/web/content_entity_example_contact/list)

I want to make my list as a View, but this content entity type "Contact" is not there to select it. (I see only the node content types, users, taxonomy etcI

How can I use this content in a View ? What do I have to do to make this content available in the Views UI ? Where can I find the content entity types to create a view ? Or how can I add those ?

0
1

Add a views_data handler to the entity type. Either the core standard EntityViewsData

content_entity_example/src/Entity/Contact.php

  *   handlers = {
  *     "views_data" = "Drupal\views\EntityViewsData",

or a custom handler:

content_entity_example/src/Entity/Contact.php

  *   handlers = {
  *     "views_data" = "Drupal\content_entity_example\Entity\ContactViewsData",

content_entity_example/src/Entity/ContactViewsData.php

<?php

namespace Drupal\content_entity_example\Entity;

use Drupal\views\EntityViewsData;

/**
 * Makes the content_entity_example entity available to Views
 *
 * @ingroup content_entity_example
 */
class ContactViewsData extends EntityViewsData {

  /**
   * {@inheritdoc}
   */
  public function getViewsData() {
    $data = parent::getViewsData();

    // $data from the parent is enough to make it work but modify as required.

    // Give it a more description name in the dropdown when creating a new view.
    $data['contact']['table']['base']['title'] = $this->t('Content Entity Example Contact');

    // Make the Name field appear to Views as Last Name
    $data['contact']['name']['title'] = $this->t('Last Name');

    return $data;
  }

}

To apply this code to the example module use the patch from this issue https://www.drupal.org/project/examples/issues/2725833

1
  • I am now having trouble using a relationship and contextual filters (to show all contacts on a user/%/tab ) but I think that is another question. (using the core standard handler) – sigridt Apr 10 at 14:56

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.