0

I'm struggling to achieve something in Views that would be quite simple in PHP itself - hoping for some pointers.

I have created a view, displayed as a block, which appears at the bottom of each page with a content type of 'article'. This block shows the latest 4 articles added to the site based on publish date. It shows max 4 items (provided at least 4 articles exist and are published).

This works - however I've been struggling to extend this functionality to meet two new client requests:

  • The view should exclude the currently viewed article if loaded on the page of one of the 4 latest articles added; [SOLVED THANKS TO ANSWER BELOW]
  • The client wishes to 'override' the items shown in this view in some cases. Here I've added a field to the content type named 'field_related_articles' which is a entity reference field. The admin can now select up to 4 articles from the database which should show in this view. However it is optional, and the default behaviour is to show the 4 latest articles still. If 1 article is specified the 3 latest articles should then appear. If 2 then those 2 plus the latest 2 articles make up the view.

I've been advised to have two views then use an 'attached' display to stitch them together, then remove the extra results (if applicable) in code in a template file. But I'm struggling settling this up.

---------- EDIT : to add some clarity to second point ----------

The view is intended to always show 4 article teasers. In it's default state this should be the latest 4 articles added to the site. This is now working.

The complexity comes in that on each 'article' content page it should also be possible to specify up to 4 articles (using an entity reference field) to show in this view instead. I've added the field to the content type 'field_related_articles'.

So when the view is included on an article page it needs to check if any values are specified in 'field_related_articles' - if so they should be the results in the view. If less than 4 nodes have been specified, the view should then show the latest X articles to make up to 4 in total.

0

To avoid the article you're on :

Click on the "advanced" tab. Click on add under "contextual filters". Choose Content:nid. Under "when the filter variable is not available, choose "provide default value". From the drop down menu select "content id from url". Scroll all the way down to the bottom of the window and click on the "More" link. Click "Exclude"

https://www.drupal.org/node/131547

The second problem can be done by adding 2 Sort Criterias. The first will be on 'field_related_articles' - and then by date. I find displaying the SQL output in admin really helps to see what is happening backend.

  • Thanks so much - that contextual filter handles the article I'm on perfectly. – steve Oct 26 '14 at 21:51
  • i've edited the question to try and clarify the 2nd requirement. – steve Oct 26 '14 at 21:52
  • No worries, I've changed my answer accordingly – Deejay Oct 27 '14 at 10:03
  • Thanks @Deejay - it doesn't seem to work, perhaps because I'm outputing content as teasers? But I'll keep experimenting – steve Oct 27 '14 at 12:17

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.