-
Link to a View (with Contextual Filter value) If It Will Have Results (Drupal 8)
Returns a link render array.
1234567$nid = $this->entity()->id();$results = views_get_view_result('related_posts_by_category', 'page_1', $nid);if (!empty($results)) {$url = Url::fromRoute('view.related_posts_by_category.page_1', ['arg_0' => $nid]);$link = Link::fromTextAndUrl(t('Related Posts'), $url)->toRenderable();return $link;} -
Drupal Views – Show One (Most Recent) Item Per Group
Alright, the title is a bit misleading because I’m not using Groups, but it is the best explanation of what I’m achieving with this example. My goal is to show a list of the most recent blog post in each category, where clicking the category takes you to the associated post.
I have a content type called Blog Post which has the following fields:
- Date (field_blog_date – date field – single value)
- Category (field_blog_tr_category – taxonomy reference field – single value)
- Items (field_blog_cr_items – content reference field – unlimited values)