0

I searched for a solution but I didn't get it right. Given my setup:

  1. A content type Service, referring a content type Agency
  2. A content type Agency
  3. A content type Person, referring a content type Agency

Now I need to add an entity reference in Service referring some of the Persons. The list of persons needs to be limited to the persons in the same Agency (of the service). I'm using an entity reference view to generate the list of the widget. I tried fetching the list of the person, using contextual filter(nid) and relationship but i didn't manage to make it right.

Therefore I'm asking for help. How should I setup the view?

Thank you all

Small visual explanation

6
  • I also tried to use the token argument from the entity reference widget, but i wasn't able to filter using the token. How should I add the argument in the views?
    – Elgandoz
    Commented Jun 12, 2018 at 4:34
  • [node:field_reference_name] returns the title, i tried different option but none of them returns the id
    – Elgandoz
    Commented Jun 12, 2018 at 6:15
  • Is this new ER field supposed to work at the time when you're creating the Service node? Before the Service node is saved, you have only chosen the Agency in the node creation form, but it has not been saved anywhere, so it can't be used to filter out the Person CT that only belong to the Agency selected in Service.
    – prkos
    Commented Jun 12, 2018 at 15:21
  • Thanks @prkos, but I'm aware of that. The service nodes are already existing and rarely newly created. The contacts though needs to be updated often.
    – Elgandoz
    Commented Jun 13, 2018 at 1:21
  • OTOMH it doesn't seem you need a Relationship. You only need to create Person Views, with Contextual Filter of ER field (not NID). I haven't tested this case yet, is your Service->Agency field the same field as the one on Person->Agency? Was it created as a new field, or did you "Add existing field"? I think that might matter when configuring the Views. You basically need to create Person Views with a Contextual filter for Agency that when placed on nodes where Agency is in ER, that filter will reduce the list to only those People. If you have that, you can easily create Entity ref Display.
    – prkos
    Commented Jun 13, 2018 at 14:24

2 Answers 2

1

I couldn't test all the way with Entityref Views, I seem to have a bug in choosing Views on my Field settings. But I was able to create the Views and it works as expected in Views preview.

The goal is to create Views of sibling nodes. In your case we need to list Person nodes that are sibling nodes to the Service node you're currently viewing. They are sibling through the Agency Entity reference field (they both refer to the same Agency node).

When both References to Agency are the same field

This is the case when the references to Agency from both Service and Person are the same field, reused (this happens when you create one ref field on one content type, then on the other content type you choose from the second drop-down "Re-use an existing field").

  1. Create Content Views showing Person. You don't need Page nor Block, add Entity reference Display.
  2. Show Fields Title and Agency (to test filtering works as expected, you can remove Agency later)
  3. Under "Entity Reference inline fields" set Separator to from (with spaces on each side) so you get meaningful output Person: Name1 from Agency: Agency1
  4. Add a Relationship Content referenced from field_agency, you can require it. This is the Agency a Person listed in Views is referring to.
  5. Add another Relationship Content using field_agency, set it to use the Relationship from the previous step, you can require it. This is the reverse direction, pointing to all content that is pointing to this Agency.
  6. Add a Contextual Filter ID from Content Category and set it to use the second relationship (field_agency) and configure:
    When the filter value is NOT available
    Provide default value
    Content ID from URL
    When the filter value IS available or a default is provided
    Specify validation criteria
    Service
  7. Configure Entity ref field settings to use this View, and choose the widget on "Manage form display".

That's it! You can test Views through Preview by entering one of the Service NIDs. Don't forget to add some "No results" text.

The Views will list only Person that are in Agency that this Service is referring to.

When References to Agency are NOT the same field

This case is when you created two different Entity reference fields on Service and Person (they have different machine names). Configuration is a bit different even though they refer to the same Agency.

You create Views the same as above only with these differences:

  1. The first Relationship needs to be of the Entity reference to Agency that is on the Person CT.
  2. The second Relationship needs to be of the Entity reference to Agency that is on the Service CT.

    Again this second Relationship must use the Relationship from the previous step. This is where the magic happens, where Service and Person Agency fields are compared.

The Contextual ID Filter needs to be using the second Relationship, as in the first case. This Contextual is only passing on the Service ID of the page you're on to Views.

0

After lot of tries, I gave up and use custom code, which took me 15 mins. That's how I did it:

function custom_dataset_views_pre_view ( &$view, &$display_id, &$args) {
  if ($display_id === 'entityreference_1') {
    // Get the node
    $url = arg();
    if ($url[1]) {
      $node = node_load($url[1]);
      $agency_id = $node->field_theme_working_group['und'][0]['target_id'];
      // Get the views filter
      $filters = $view->display_handler->get_option('filters');
      if ($filters['field_agency_target_id']) {
        // Change the filter
        $filters['field_agency_target_id']['value']['value'] = $agency_id;
        // Push the filter back
        $view->display_handler->override_option('filters', $filters);
      }
    }
  }
}

Less elegant, but finally working. Despite that, I still would like to know how to achieve this in plain views, or using tokens.

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.