Showing posts with label Views. Show all posts
Showing posts with label Views. Show all posts

Thursday, November 1, 2012

Creating an iCal Feed for a Single Node in Drupal 7

Here are updated instructions on how you can create an iCal Feed for a single node in Drupal 7 with the new Date iCal module. The functionality to create iCal Feeds has been separated from the Drupal Calendar module to the Date iCal module.
  1. Create a Block View for the content type(s) that you want to create the iCal Feed.  Keep in mind that this block needs to be put on your node page, so make sure that your block contains items that you would like to display on the node page.

    Here is a sample of the end result:

     
  2. Views will force you to display at least one field.  If you want an empty block with just the iCal feed, you can use the Global: Custom Text as your Field.  Hide the label and leave the text box empty.



     
  3. Add a Contextual Filter of Content: Nid to your Block view.


     
  4. Set up the Contextual Filter so that it gets the default value of Content ID from URL.  This way, your iCal Feed will only show information for the node that is loaded on the page.



     
  5. Now add a Feed to your View and configure it to display iCal Feeds.

  6. Don't forget to add the fields that you want included in your iCal feed...

  7. ... and add a Path to the Feed view.  Make sure it ends in .ics.
    (Edited: Dec 1, 2012 - thanks to @derekw from the Drupal.org for correcting this.) Add a wildcard placeholder for Views to use/insert the nid into.  For example, your path might be events/%/ical.ics.
    You will also need to attach the feed view to the Block that you created earlier.

  8. Once you save the view, you can place your empty block with the attached iCal feed on every node page.  The results should be like that illustrated in step 1.

Tuesday, October 2, 2012

Views Attachment in Drupal 7

I've seen answered a few questions on Stackoverflow and the Drupal StackExchange on creating a View with attachment.  Here's how to do it in Drupal 7....

When to use a Views Attachment


Perhaps you'd like to show a block of a particular type of content like News in a side bar of your webpage.  You already know that you can easily create a list of the top 5 News items (title only) using Views.  But what if you want the first News item to show the news article image in addition to the title?

Or perhaps you want to display a list of your blog posts and for the first 2 posts, you want to show the title and a summary.  But you also want to list the title of another 4 posts below that.  

This is where Views attachment comes in.  With Views attachment, you can create one View display and attach a another View display with different configuration (eg. different fields of the content type displayed).  And you can refer to both displays using the one View.

Breakdown of one block created using Views with an Attachment display.

Creating a View with an Attachment


I assume you know how to create a basic View so I'll skip the step-by-step of creating the initial View.  Here's what my initial block View might look like.  Note that I'm only showing the Title of my News article and I've set the view to skip the first News article (Pager options' Offset = 1) because I want to show the first article a little differently.


Now, when you add a new display to this View, notice that you have the option of creating an Attachment.  Let's add an Attachment.


In the Attachment display, I can now configure a different set of fields to display, not forgetting to apply the changes only to the Attachment display as not to affect the original display I created.

Add the image field ONLY to this Attachment display.

The Image field added.


Notice that the middle column of your Views display settings are the Attachment settings.  For the Attachment that I created above, I've set it to attach to the Block which I created initially and I've set the Attachment to be positioned before the block because I want the News item with the Image displayed first, on the top.

I've also set the the Pager settings to only display one item.  Remember in the Block display, I set the offset to 1 in order to exclude the first item from the Block. This is where I'm going to display the first item with an Image.


Now, if we save the View created above, you should get one Block that you can place in your site which contains both News listings with an Image and a title and News listings with just the title like this....

ONE block with two displays
one display with image and title
and  one display of title only.

Note that there are likely other ways of achieving the same thing.  For further reading, see:

Monday, July 9, 2012

List Related/Similar Nodes using Views 3

Want to show a list of related or similar nodes in a block on the current node's page?   All you need is Views.

These instructions will help you create a View of related nodes (based on shared taxonomy terms) in order of relevance.  Nodes that share more terms with the current node are shown at the top of the list.  For example, if you have a node IPhone 4S node with terms Gadget, Apple, and Phone, you can get similar nodes in the following order:

IPhone 3 (Apple, Gadget, Phone)
IPad (Apple, Gadget, Tablet)
Mac (Apple, Computer)
Bramley (Apple, Fruit, Tree)

To create the View:
  1. Create a block view
  2. Add Contextual filter -> Content: Nid -> Provide default value -> Content ID from URL
  3. Add Relationship -> Content: Taxonomy terms on node -> specify the appropriate vocabulary
  4. Add Relationship -> Taxonomy term: Content using vocabulary as specified above -> Check off Require this relationship
  5. Turn on Views aggregation
  6. Assuming you are listing title only, edit the title field to use the Relationship you set up in #4 above.
  7. Add new sort criteria of Content: Nid. In aggregation settings, select Count. Use relationship from #4 and sort descending
  8. Add Contextual filter -> Content: Nid -> Use relationship from #4 -> Provide default value - Content ID from URL -> Scroll down and expand "More" then check "Exclude" to remove current node from the view
Created with Drupal 7, Views 7.x-3.3 -- Click to enlarge

Credits: From my answer on Stackoverflow