2

My set up is similar to the setup in this question: How to integrate Field collection module in Views?

I have a content type with a field collection field (hereafter itineraries) that accepts unlimited entries. In the field collection is a (user) entity reference field (hereafter travelers) that accepts unlimited values. It also has another (user) entity reference field (hereafter approvers) separate from the field collection.

I've created a user view, with a contextual filter to select the current logged in user. This view has a relationship to bring in the nodes that reference the current user via the approvers field. I have another relationship pulling in the itineraries attached to the node.

What I would like to do is make a field in my view that has a comma separated list of travelers on the node. I can get the travelers from a single itinerary into a field, but nodes with multiple itineraries result in multiple rows.

I guess the solution must be to somehow embed a view to feed this view, but I'm not sure how to approach that, perhaps that would require an additional module?

Update: At @no-sssweat's recommendation I tried out the Views Field View module.

I tried a content view, passing the nid and using a relationship to get the field collection items.

With that I was able to get something like this for a node with the two itineraries, the first with two travelers and the second with one:

Travelers:

Person One, Person Two

Person Three

I also tried a field collection item view, with the intention of passing in the field collection item ids as contextual filters. Testing this in the view itself, I got one traveler per row, but in practice I couldn't figure out a good way to pass id1,id2,id3 to the view. The view with ids input manually looks like this:

Travelers:

Person One

Person Two

Person Three

So in both cases I would get all the travelers listed in one field as the result of the embedded view, but they are listed each in their own divs within the field. I'm trying to figure out how I can get them listed as a comma separated list of values. I want to get to a field that looks like this:

Travelers:

Person One, Person Two, Person Three

Can you help me get over that last hump?

1 Answer 1

2

I guess the solution must be to somehow embed a view to feed this view, but I'm not sure how to approach that, perhaps that would require an additional module?

You're on the right track, use the Views Field View module to achieve that.


You could also do this programmatically if you have a template file or a preprocess function

Ex:

<?php print views_embed_view('blog_posts','block_1', $contextual_filter_value); ?>

To make it as a list, use CSS:

.view-id-test.view-display-id-block .views-row {
  display: inline-block; /* If it doesn't work try display: inline; */
}

.view-id-test.view-display-id-block .views-row .field-content::after { 
    content: ",";  
}

.view-id-test.view-display-id-block .views-row-last .field-content::after { 
    content: "";  /* or content: normal; */
}

.view-id-test.view-display-id-block being the unique parent classes wrapper as you don't to affect every single view and only a specific view & view display. (Use inspect element with your browser, to find a good parent wrapper.)


If you don't want to use CSS, then you will have to do it programmatically in your view's fields template file.

// load view
$view = views_get_view('view_name');
// set arguments for view
$view->set_arguments($contextual_filter_value);
// execute view
$view->execute();
// get all travelers from the result 
foreach ($view->result as $result) {
   // capture the result of your field in an array. 
   $travelers[] = $result->field_traveler; 
}
//make array into comma list
$travelers_list = array(implode(", ", $travelers));
// print the list
print $travelers_list;
7
  • Thanks very much for this. Now I get all the travellers showing up in one field, but they are all row separated within the field. I probably should have specified in the question that I want to end up with a comma separated list. Your answer probably still works, I just haven't been able to figure out how to make it do what I need. I'll update my question with details of what I've tried.
    – UltraBob
    Oct 30, 2017 at 1:53
  • @UltraBob updated.
    – No Sssweat
    Oct 30, 2017 at 7:01
  • Thank you, inline-block didn't work, but inline did. I had to stick it on a few more divs than in your example, but I think I can probably address that with the style settings in views. I do wish there was a cleaner way to do this in drupal, I'd prefer not adding content in css, but this gets me there. Thanks!
    – UltraBob
    Oct 30, 2017 at 9:14
  • 1
    @UltraBob for your follow up, you would have to do it through a template file. See What is the template suggestion for node 'teaser' view mode?
    – No Sssweat
    Oct 30, 2017 at 9:32
  • 1
    Thanks! I think lack of successful experience with theming in drupal was showing. This time I'm going to make it happen.
    – UltraBob
    Oct 30, 2017 at 9:34

Your Answer

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

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