1

The website has two content types: Magazine and issue.

I'm using the Entity Reference module to create a magazine archive website.

enter image description here

I have a views that list all magazines. Each magazine is with an image of issue that related to this magazine. There is a relationship (entity reference: referencing entity) and the aggregation setting is active (use aggregation).

Everything is OK but there is a problem I couldn't solve.

The image for each magazine is the image of the first issue of this magazine. But not every "first issue" has an image and it shows the default image.

What I want to do is to show the first issue's image that's image field is non-empty or show any related issue's image on this views. How can I do that?

Note: Some magazine's any issues doesn't have any image and if I filter out these issues, then the related magazine will not be shown. I think what I want to do is if any of the issue has an image show it or show the default image.

enter image description here

  • 1
    If you don't show the issues, only the magazine, then aggregate on the referenced magazine id, MIN() on the date of the issue and filer out fids that are empty. So you might need to pull in the file entity as well. – J. Reynolds Feb 1 '16 at 11:52
  • @J.Reynolds thanks for your comment. Yes, I aggregated it but did use "Group results together". And another thing I forgot to mention, some magazine's any issues doesn't have any image and if I filter out the issues, then the related magazine will not be shown. I think what I want to do is if any of the issue has an image show it or show the default image. – herci Feb 1 '16 at 12:31
2

From Everything is OK but there is a problem I couldn't solve I supposed you successfully create a view of issues and their related magazine and I focus on the problem.

There are two cases,

  1. at least one of the issues has an image
  2. non of them has an image

In order to create a view for case 1, you need to modify your existing view to have sort criteria on image_field:fid (Issue image field, using relationships you can have this)

enter image description here

and sort it descending

and set the Pager to:

Use pager: Display a specified number of items| 1 item

in this way, if there is any issue with an image, the first one that has image is displayed.

Lets handle the case where there is no issue with image. The image field is displayed in here, it can be in a <div views-field views-field-field-image></div>, but it is empty. We have to insert a default image in it. Simply add css like this to your theme

.views-field-field-image{
       width: ...px;// you standard display of width
       height: ...px;// you standard display of height
       display: inline-block;
       background-image: url('');

Now if there is an image, the <div views-field views-field-field-image></div> layer will go behind the image and it will not be shown and if there is no image, we have set the <div> dimension and also an image for it to be displayed.

  • Thanks for the answer. Your answer gives a good alternative for some cases but mine (views) was a bit more complex and didn't work this method. – herci Feb 6 '16 at 6:14
  • 1
    In which it case didn't work for you? I tried this solution for case 1 that described above – M a m a D Feb 6 '16 at 6:16
  • The views was not designed per magazine. It was showing all the magazines with an image so 'Use pager: Display a specified number of items| 1 item' didn't work. Anyway, thanks for your answer. It gave me another idea for the issues with no image. – herci Feb 6 '16 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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