In this video, I will show you how to create a block display.

Dynamic blocks are commonly created by contributed modules, and static blocks can be created in the blocks administration area. But we’ll use Views to randomly select an ad from a list of upcoming sales and display that ad in a block.

Before I create the view, I'll create a content type for upcoming sale events with fields for the start and end dates of the event. Since it will contain date fields, I’ll quickly install and enable the date module:

Install Modules

Enable Modules

  • Go to "Modules" (admin/modules) and enable:
    • Date (This will make date fields available)
    • Date API (This will enable the date module to be used by other modules such as Views)
    • Date Views (This will integrate integrate views with date module)
  • (Save Configuration)

Let's create content type

Create Content Type

  • Go to "Structure => Content types => Add content type" (admin/structure/types/add)
  • Name: Event
  • Description: A sale event
  • Submission form settings
    • Title field label: Event Title
  • (Save and add fields)

Add new field

  • Label: Date
  • Field Type: Date
  • Widget: Select list
  • (Save)
  • Field Settings
    • Date attribute to collect
      • [x] Year
      • [x] Month
      • [x] Day
      • [ ] Hour
      • [ ] Minute
    • [x] Collect an end date
    • [x] Required
  • (Save field settings)
  • Event Settings
    • [x] Required
    • Help text: Select start and end dates
  • (Save settings)

Now that we have our content type, let's create some events.

Create Content

  • Go to "Content => Add content => Event"(node/add/event)
  • Event Title: Event #1
  • Description : (Lorem Ipsum)
  • Date
    • Month: Feb
    • Day: 1
    • Year: 2014
  • to:
    • Month: Feb
    • Day: 3
    • Year: 2014
  • (Save)

And another one

  • Event Title: Event #2
  • Description : (Lorem Ipsum)
  • Date
    • Month: Feb
    • Day: 14
    • Year: 2014
  • to:
    • Month: Feb
    • Day: 20
    • Year: 2014
  • (Save)

Now, let’s create a view with a block display that will randomly select and display an ad.

  • Go to "Structure => Views => Add new view" (admin/structure/views/add)
  • View name: Event Advertisement
  • Description: A randomized display of upcoming events
  • Show: Content
    • of type: Event
    • sorted by: Unsorted
  • [ ] Unchecked Create a page
  • [x] Create a Block
    • Block title: Upcoming Events
    • Display format: Unformatted list
      • of: fields
    • Items per page: 1 (We are just going to display one(1) item at a time)
  • (Continue & edit)

Fields

Next, let's add the Date field

  • Click "Add" next to Fields
  • [x] Content: Date
  • (Apply(all displays))
  • [ ] Uncheck Create a label
  • Choose how users view dates and times: Short
  • (Apply(all displays))

Filter Criteria

Now, we only want to show upcoming events, and not events that have already occurred, so let’s add a filter to only show current and upcoming events.

  • Click "Add" next to Filter Criteria
  • [x] Content: Date - end date (field_date:value2) (We’re using the end date instead of the start date so that events that are happening right now, will be included.)
  • (Apply & Continue)
  • (Apply & Continue)
  • Operator: Is greater than or equal to
    • Enter a relative date
    • Relative date: now
  • (Apply(all displays)

Sort Criteria

Finally, let's set the sort option to random, so that Views will randomly select an event to display.

  • Click "Add" next to Sort Criteria
  • Filter: Global
  • [x] Global: Random
  • (Apply(all displays))
  • (Apply(all displays))

To test this, I’ll scroll down to the live preview section and click “Update preview”. Since there are only two options, we have a 50/50 chance of displaying each. But, as I continue to click the update button you’ll see that it does switch between them, so it’s working.

  • (Save) (Save the view)

A block created by Views can be displayed in any region just like other blocks. Let's assign the "Event Advertisement" block to the sidebar second.

  • Go to "Structure => Blocks" (admin/structure/block)
  • Scroll down to Disabled
    • Block: View: Event Advertisement
      • Region: Choose "Sidebar second"
    • Block: View: Archive (I’ll also display the Archive block in the sidebar)
      • Region: Choose "Sidebar second"
  • (Save blocks)

Let's return to the homepage, and now we see the Archive and Event Advertisement views in the second sidebar.