Hi all.

I use Entity Reference module and now system has 2 content types: Book and Author. Book has a field 'authors' of type 'Entity Reference'. Book can be written by many authors. Now I can list all authors for selected book.

But also I need to list all books written by selected author. What's the best way to do this? Any advice is welcome, probably without using Entity Reference.

Thanks in advance.

Comments

nevets’s picture

You should be able to do that with views.

mechmsk’s picture

Thank you. But could you explain this in detail, please? I create a view, select Book as a content type, create a filter on book_authors field.

But then I need to choose an operator. Book should be selected when book_authors contain one certain author. So I need an operator like "contains". But there are only simple operators like 'Is Equal' , 'Is Greater', etc. in the drop-down list.

And if I had operator 'Contains', how could I pass an author to it, to fill a 'Value' of filter criterion?

grient’s picture

If I understand you correctly, you have a book content type which has an entity reference field that refers to (possibly) many authors. Now you want to query all books for a given author. This can be done with views and contextual filters:

1. create a new view (depending on your needs you may want to create a block or page)
2. Filter on content type books (after all, that is what we're aiming for)
3. Add a relationship: "Content entity referenced from field_author" (this gives us access to the authors in the entity reference field)
4. Now, instead of using a filter criterion, we use view's powerful contextual filters. You will probably need to filter on node id (NID).
5. From the relationship pull down, select "Content entity referenced from field_author".
6. Under "when the filter value is not available" choose "Provide default value" and under "Type" choose "Content ID from url"
7. Under "when the filter value is is available" choose "Specify validation criteria" and keep the default settings.

I hope this will set you in the right direction. You might need to tweak some of the values as I have not tested this with multiple authors per book.

tgshannon’s picture

I have the same content types: author and book, with book having an entity reference field pointing to author. However, what I am looking for is additional books by the author of the current context book. I can't figure out how to set a context to the author nid when the current context is a book?

Example: book1 has author1; When I am looking at the book1 content, I want my block to display all books by author1 (and perhaps leave book1 out of the list.)

tgshannon’s picture

As I was perusing the site, I thought I would follow-up with my solution.

1. Create view and filter on content type book and published true.
2. The book content type has a node relationship field, author - set this up as a relationship in the view
3. Add a contextual filter using the relationship author field - this will filter books by the author
4. Provide default value uing php:
         $node = menu_get_object();
         return $node->field_book_author['und'][0]['nid'];
5. Add a contextual filter on the content: nid 
6. Provide default value content id from url and specify exclude in the more options - this removes the current book