Drupal 7 Field Collection Views module

By shane
Tue, 2013-01-29 23:32
9 comments
Daily Dose of Drupal Episode #97

The Drupal 7 Field Collection Views module makes the Field Collection module even better by allowing you to harness the power of the Drupal Views module to make the display of a Field Collection extremely flexible.

In this episode you will learn:

  • How to use the Drupal Field Collection Views module to change the Field display formatter for a Field Collection to a custom view
  • How to correctly clone and implement a custom view to make use of the Drupal Field Collection views module

Hello everybody and welcome to another Daily Dose of Drupal. Today we’re on Episode Number 97 and as always I’m Shane Thomas, you can follow on Twitter at smthomas3, also go to codekarate.com and sign up for the newsletter on the left and find me on Google + as well.

In episode number 59 I believe I went over the Field Collection Module and what the Field Collection Module allows you to is it allows you to basically add a field to a content type that allows multiple field values to be attached to that and the best way to show you would be to … with an example.

So I have the Field Collection Module installed and I have a content type called the Playlist. In this content type called Playlist I had a field collection called the Songs. So if you take a look at one of the playlist I created it is called Classic Rock and it has a couple of songs. It has … you can see there’s song title and artist and there’s multiple of those so I have 1, 2, 3, 4 … it looks like about 6 songs in here and you notice that for every song title there’s an artist so I’ll walk this way with Aerosmith, We are the Champions, Queen and if you take a look at the edit form you’ll see how it works. Basically it has the two fields; the Song Title and the Artist and it allows me to of course rearrange these or to add another field here if I want to, if I want to add another song and artist and I can of course add these two fields.

I can add any number of fields I wanted just by simply going to the Field Collection’s page after I added it to the content type and manage the fields that go inside this Field Collection so I have a song title and an artist. And the only reason I’m showing you this is because it ties in to the module we’re going to be going over today. We’re going over the Field Collection Views Module.

As you can see on this display page it’s not necessarily the format that I would want this display to be in, it’s just kind of a list, it tells me the song title, the artist. I can make some minor changes to this format, if I come up over to the content type page for my playlist and I go to manage display I can have it show the Field Collection Items or links to the Field Collection Items.

So I’ll go ahead and have a Show Field Collection Items and I’ll click Save and you can see now it shows in a little bit different format, I can edit and delete, there’s an Add Link so I can add a new song title and artist to this collection and I can edit an existing one but this isn’t necessarily the format that I want but what I want here is I kind of want it to be on a table based format just so it’s easy to read and easy to see and this is where the Field Collection Views module comes in.

This basically allows you to use the power of the Views Module with your Field Collections. So I’m going to start by turning this module on and I’ll save this and now there’s a couple of steps that you’re going to need to follow; there are some instructions on the Field Collection Views page so you may need to take a look at that if you get lost.

It basically will allow you to change the display settings for this particular field collection so I’m going to come back to the content type page I’m going to go to manage display for my playlist and now I’m going to look and see that there’s a Views Field Collection Items option here so I’m going to select that and now what this allows you to do is it allows you to use the name of your view so this is the machine name of the view as well as which display ID and if you want to have an Add Link in the actual display itself.

So I’m going to use the default and show you how the default view works so I save this, I come back to the page and now you can see I’m already … you can tell it’s a View and I’m already using a table based format. Right now it just tells me the Field Collection ID gets me an Edit and Delete link and has this Add Link at the bottom. Of course I want to modify this but before you go and just modify this view directly, there’s a couple of things you should note; this default View probably shouldn’t be modified just in case you have other Field Collections you add later you want to be able to use this view as an example.

So what I recommend you do and I think it’s also recommended this way in the instructions here is I’m going to go ahead and clone this Field Collection View and you could of course just add another display to the Field Collection View but I like to … I guess just change it so it’s a completely separate view then I can export it with the Features Module or something else if I need to and I will go ahead and name this Playlist Field Collection.

And since this is a clone you’re already working with the exact items that you need so it already has all these different fields in it and you need to make sure you keep most of these fields here. So what I’m going to do is I’m going to go ahead and this Field Collection ID I’m going to just exclude this from the display so it’s not showing up and I’m going to then add my various fields that I want which are song title and artist.

You can see it’s just a basic view if you haven’t use the Views Module before you’ll obviously need to learn how to use that first but if you have it’s going to be very familiar so I’ll go ahead and leave the labels and walk through the next steps and I’ll rearrange this so I put the song title first then the artist then the edit link and the delete link. Let me see what this is; that’s the Add link that is there, it’s going to be shown at the bottom so I’m going to go ahead and save this and now when I come back to the playlist.

You’re going to notice nothing has change yet. I need to actually come back into my content type, go to Manage Display and change the new machine name of this view and in case you don’t know what the machine name is you can hover over or you can come in here and if you hover this view name you can see it says machine name, playlist_field_collection.

So I’m going to change that playlist_field_collection and as far as the machine name; if you hover over or you go into the advance section it’s going to give you the machine name of this view. In this case it’s a page, what I try want to do is I try want to not make this a page because … this is the wrong view actually so I want to click on the right view this time.

So it’s actually not a page, it’s just the master display so if you hover over it’s going to say and if you look at the URL down here right at the bottom when I hover over this it says … you’ll notice that there’s the word Default in there right at the end and that default is the name or the machine name of this display.

In most views if you added another View; for instance I added a block it would allow you to change this machine name if you wanted to but we’re just going to leave it at a default so the display ID which is just the Display ID not the machine name of the View is going to be default. If I added a different one that I could of course maybe change it but for the most part they’re should probably left at default. If you want to have the Add Link you leave that there, of course this was the actual machine name of the view, go ahead and click update, save it and things should begin looking the way I want it to now. And there you have it.

Now we have Song Title, Artist, Edit and Delete. This is a view so I can do a lot of different things here and control how this works and how this looks and it’s just that easy to have a content type with a Field Collection that you can build out and now you can also control very easily how that is presented on the page.

So that’s it for this time on the Daily Dose of Drupal and we’ll be back again next time with another episode. Thanks for watching and let me know if you have any questions or anything else you would like me to cover in future episodes. See you later.

5 Secrets to Becoming a Drupal 7 Ninja

Comments

Anyone facing the same issue, as No. 5. Functions given below worked for me. Paste this at field_collection_views.module

function field_collection_views_views_pre_render(&$view, &$display_id = 'default', &$args = array()) {
global $user;
if($view->name == 'tab_list_field_collection' && $display_id == 'default') {
if(user_access('edit own service_tabs_list content') || user_access('edit any service_tabs_list content'))
{
}
else
{
unset($view->field['nothing']);
unset($view->field['nothing_1']);
}
}
}

function field_collection_views_views_pre_view($view, $display_id, $args) {
global $user;
if($view->name == 'tab_list_field_collection' && $display_id == 'default') {
if(user_access('edit own service_tabs_list content') || user_access('edit any service_tabs_list content'))
{
}
else
{
$view->set_item_option('default', 'footer', 'area', 'content', '');
}
}
}

i need to build a design like that in kresge.org the grid having pics at the top in the website...please tell me how to build that .i am a beginner and not able to do it

Nice Tutorial.

But I cannot figure out how to hide edit/delete/add links for anonymous and non-authorized users. As it is added as custom text, it is not hidden viewing as anonymous users. Instead links to not authorized page.

Is there a way to hide links with this modules.

Thanks

Is there a way to use a field collection of date and text fields in a calendar view? I tried and a cloned calendar view did not recognize the collection as date type field? Thanks for the great and clear tutorials!

Hi i found this tutorial very helpful. but i am facing an issue. if i am trying to use views calc module with field collection it is not working and giving error. Please help me or suggest me a solution for this.

Thanks for the video. I am having a problem creating an exposed filter for a very similar field collection view. The collection consists of a composer name, a pdf file and an MP3 file. Everything comes up beautifully. If I add a filter on a composer name field collection item (or any of the fields included in the view) and provide a default value , the view opens with the expected filtered result. However, if I expose filter and enter or modify the value or don't do anything - clicking the Apply button takes me to the front page. Also, none of the words in the collection are searchable in a regular drupal search. What is missing? Why does the Apply button behave this way? I created a similar filter for a calendar view and it works fine.
Many thanks in advance to anybody who will respond to this!

Post new comment