Blog sorting by event date

Permalink
Hi,
I'm making site where I'm using blog post as event.
There is only 1-5 events in year so lets not make this too complicated.

I want to make in right sidebar nag for upcoming and past events.
In blog post I have date field. (event_date)
But what is the easiest way to sort and filter pages?

OlliSavolainen
View Replies:
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi OlliUotila,

I had the same question when trying to create events and dealing with custom attributes. Here is an example approach to handle sorting and filtering pages using a custom date/time attribute in 5.7.

In this example, I am using a custom attribute for date AND time. This allows me to resolve issues with multiple events that share the same date.

1. Create a custom attribute to store the event's date and time.
Dashboard > Pages & Themes > Attributes
- Add Attribute
- select Date/time
- Go
- Handle:
event_date_time (handles are lowercase and no spaces)
- Name:
Event Date and Time (names can have spaces and are case-insensitive)
- Searchable:
checked - Content included in sitewide page search index.
checked - Field available in Dashboard Page Search.
- Date/Time Options: Ask User For:
select Both Date and Time
- save

2. Create a page type for your event pages.
Dashboard > Pages & Themes > Page Types
- click Add Page type
- Page Type Name:
Event Entry (names can have spaces and are case-insensitive)
- Page Type Handle:
event_entry (handles are lowercase and no spaces)
- select the Default Page Template you want to use
- Launch in Composer:
yes - the page is created in a form style dialog
no - the page is displayed like a regular web page where you edit each section manually
- Is this page type frequently added? - orders the new page closer to the top in Add Page
- Allowed Page Templates - allows you to restrict which page templates are used
- Publish Method: (this is based on having an Events page)
Always publish below a certain page
- Publish Beneath Page:
Events
- save

3. Create the form for your new Event Entry page type
Dashboard > Pages & Themes > Page Types

This is project specific, so I will just include a few basic attributes.

- click Edit Form for Event Entry
- in the new Composer Form window you can create a single group or multiple groupings of attributes that will show up in your Composer dialog for Event Entry
- Add Set - allows you to create a named set and a description which shows up as a line of text above the set group in the Composer dialog
- Add Set
- Set Name:
Event Info
- Set Description - is optional
- click Add Set
- after adding a set, you can start adding attributes
- click or hover over the title bar of your new set
- on the right side you will see a plus sign, four sided arrow (for reordering), a pencil (for editing a set or attribute), and a trashcan for deleting a set or attribute
- click on the plus sign +
- Add Form Control pops up with options to include blocks, properties, and custom attributes
- under Built-In Properties click on Page Name
- click on the plus sign + again
- under Built-In Properties click on URL Slug
- click on the plus sign + again
- under Built-In Properties click on Description
- click on the plus sign + again
- under Custom Attributes click on Event Date and Time

3. Set up how page templates will display page blocks, attributes, and custom properties.
Dashboard > Pages & Themes > Page Types
- click Output for Event Entry
- on the Defaults and Output page you will see all the available page templates for the page type (enabled or disabled in the page type Basic Details)
- clicking on Edit Defaults will bring up a blank version of that page template
- on this blank page template, you decide on what will be displayed and where
*** the page display is on a per template basis, so if you allow right sidebar, left sidebar, and full templates, then you need to edit the defaults for all three (or they will be blank after exiting Composer)
- click on Edit Defaults for the Full page template
- click on + Add on the toolbar
- drag the Page Attribute Display block onto the desired area of the page
- in the dialog popup, Property to Display
Page Name
- click Add
- click on + Add on the toolbar
- drag the Page Attribute Display block onto the desired area of the page
- in the dialog popup, Property to Display
Page Description
- click Add
- click on + Add on the toolbar
- drag the Page Attribute Display block onto the desired area of the page
- in the dialog popup, Property to Display
Page Name
- click Add Event Date and Time
- click Add
- click Exit Edit Mode on the toolbar and Publish Changes
*** remember, this is repeated for all allowed templates

4. Create your Events page
- click Add Page on the toolbar
- under New Page, select Empty Page
- click Settings in the toolbar
- Page Name:
Events
- Description: is optional
- Page Location:
click on Home

5. Create your custom page template for Page List (which will do the sorting and filtering)
- in your concrete5 install root folder, navigate to applications\blocks
- in the blocks folder, create a new folder called "page_list"
application\blocks\page_list\
- in the page_list folder, create a new folder called "templates
application\blocks\page_list\templates\
- in the templates folder, create a new folder for your new Page List template - "event_entry"
application\blocks\page_list\templates\event_entry
- copy view.php and view.css from this folder into the event_entry folder you just created
concrete\blocks\page_list\
- open the copied view.php
- on line 7, paste this into the file
<?php
// these lines make sorting by the custom attribute (event_date_time) work
$pl = new PageList();
// prevents blog_entry page types from showing up when this custom template is used on the Events parent page
$pl->filterByCollectionTypeHandle('event_entry');
// gets the current time minus 1 day as a unix timestamp
$datetime = strtotime('-1 day');
// takes the $datetime (unix timestamp format) and converts it to the mysql date time format - "Y-m-d H:i:s"
$mysqldate = date("Y-m-d H:i:s", $datetime);
// filters the database request for pages by the attribute "event_date_time" and that are no more than a day old
// "event_date_time" is a custom attribute for the time of the event, the value is a time
// $mysqldate is the current time minus one day
// the comparison is: event_date_time > $mysqldate
// if the event_date_time is greater (upcoming date time), then it will not be filtered
// $pl->filterbyAttribute($column, $value, '=') - the third argument is the comparison

- on line 64, paste this into the file
// get the event_date_time attribute
$event_date_time = $page->getAttribute('event_date_time');
// convert the $event_date_time from MySQL format to a unix timestamp
// format the unix timestamp - "l F jS g:ia" - e.g. Thursday October 2nd 10:22pm
$event_date_time = date("l F jS g:ia", strtotime($event_date_time));

- on line 178, paste this into the file
<div><?php echo $event_date_time ?></div>


The entire file for reference:
https://gist.github.com/anonymous/bc0e51006a12bedbdad7...

6. Create your Page List
- navigate to your Events page
- click +Add in the toolbar
- drag the Page List block onto the page
- the Add Page List dialog pops up
*** because we are using a custom template, some of the settings can be set, but won't actually control the block's behavior (they are set in the template instead)
- Number of Page to Display:
can be left empty
- Page Type:
Event Entry
- the other settings can be set if needed, but Page Type is the most important
- click Save
- the Page List will all your Event Entry pages using the default template
- click on the Page List block and select Design & Custom Template
- click the gear on the toolbar
- on the Custom Template dropdown select Event Entry
- click Save

Now your Page List should be sorting and filtering based on a custom attribute and expiring by a set time window. I commented the important parts of the code that is inserted into view.php.

To change the expiration date of an event, refer to the PHP relative formats.
- $datetime = strtotime('-1 day');
http://us2.php.net/manual/en/datetime.formats.relative.php...

To customize the date formating, refer to the date() parameters.
- $event_date_time = date("l F jS g:ia", strtotime($event_date_time));
http://us2.php.net/manual/en/function.date.php...


I will see about cleaning this up a bit and making a proper How To.
OlliSavolainen replied on at Permalink Reply
OlliSavolainen
I didn´t try yet but I think this is just what I was looking for.
Thankyou so much!
MrKDilkington replied on at Permalink Reply
MrKDilkington
The Page List in the example is on an Events page. You can also easily add the Page List block into your right sidebar and use the custom template.
DreamMedia replied on at Permalink Reply
DreamMedia
Hi,

maybe it's worth for you to have a look at the News & Blog Feed add-on. It uses a different approach to posts, detached from page types and gives you a lot of freedom on how to build your entries. In the list/feed you can then order and/or restrict the shown entries by date and from which pages you actually want to pick the list entries. You wouldn't need to have page attributes and extra programming. You could just build your event pages like you would with any other page and place a feed entry on it. The add-on is doing the rest. Maybe this makes it a little easier ...

http://www.concrete5.org/marketplace/addons/blog-anchor/...
MrKDilkington replied on at Permalink Reply
MrKDilkington
I totally agree, most non-developers would be best served by getting an add-on block like yours.

The long explanation was partly to serve as a record for other people when they search. If they are like me and find it very useful to see how things work and want to eventually make their own stuff (and help contribute to the concrete5 core).
jasteele12 replied on at Permalink Reply
jasteele12
@DreamMedia - Seeing as how this is in 5.7 Discussion, I don't see how that helps ;)