0

In a custom module, I have a custom entity for which a view shows the following values:

screenshot

Contact is the ID of another entity (Person) containing some fields, such as Email. Adresse courriel is a Custom text field defined in the view.

What hook should I use to achieve the following task?

  1. Query the Person entity with the Contact value to retrieve the Email field value
  2. Update Adresse courriel in the view output
3
  • 3
    Please refer UserViewsData or other subclass of EntityViewsData class for a reference how to achieve such.
    – Codium
    Commented Dec 16, 2019 at 12:32
  • Why don't you use relationship in views? Commented Dec 16, 2019 at 13:50
  • @usmanjutt84 I tried. But I couldn't find how to link my content to Person entity. The entity on which my view is based (ContractSubscription) is linked to Member entity. So I could add Member entity to the View with 'Relation' feature and then I can dispaly Désignation and Contact fields. But from here, I could'n( find a way to add Person entity: it doesn't appear in the entities list of 'Relation'.
    – gbmapo
    Commented Dec 16, 2019 at 13:58

2 Answers 2

1

1- Firstly you need to add a relationship to the Person entity to have the fields available.

2- then use hook_views_query_alter() to change the fields values.

1

I added this to MemberViewsData.php:

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

    // Additional information for Views integration, such as table joins, can be
    // put here.
    $data['member']['contact_id'] = array(
      'title' => t('Contact Id'),
      'relationship' => array(
        'base' => 'person',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('Member to Person (Contact)'),
        'title' => t('Person (Contact)'),
        'id' => 'standard',
      ),
    );
    return $data;
  }
}

Then I could use Views UI, adding a 'Relation' from Member (main entity of my view) to Person. And here it is: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.