Expression Engine: Using Custom Fields In The Calendar
It seems like for years people have been asking for custom field availability within the calendar module in Expression Engine. Somehow the feature still languishes on the request list.
Here’s a quick and dirty guide on how to implement the "Mr Wilson" hack for adding custom field data into your weblog calendar so that you can do cool CSS rollover displays like those in the screenshot to the right.
Getting Started: Backup Your File
For starters you need to navigate to the weblog directory: core/modules/weblog and make a backup of the file mod.weblog_calendar.php. Once this is done you are ready to start editting the original file. The process will essentially see you add the desired fields to an array, which then then be string replaced when the web page is generated using the template you have created.
Pulling in the Data to the Calendar Module
In the file you will see, amongst others, a "calendar" function within which you will find a section "Build Data Array". This, as mentioned above contains a plethora of data/fields that are used in the generation of pages. In order to make our fields work we need to start by adding in the array variable that contains the data to the $data[$d][ ] array.
The code as you find it will look like this:
$data[$d][ ] = array(
$TYPE->parse_type($row['title'], array('text_format' => 'lite',
'html_format' => 'none', 'auto_links' => 'n', 'allow_img_url' => 'no')),
$row['url_title'],
$entry_date,
$permalink,
$title_permalink,
$author,
$profile_path,
$id_path,
$base_fields,
$comment_tb_total,
$day_path,
$comment_auto_path,
$comment_url_title_auto_path,
$comment_entry_id_auto_path
);
Once it is done, the array should look something akin to this:
$data[$d][ ] = array(
$TYPE->parse_type($row['title'], array('text_format' => 'lite',
'html_format' => 'none', 'auto_links' => 'n', 'allow_img_url' => 'no')),
$row['url_title'],
$entry_date,
$permalink,
$title_permalink,
$author,
$profile_path,
$id_path,
$base_fields,
$comment_tb_total,
$day_path,
$comment_auto_path,
$comment_url_title_auto_path,
$comment_entry_id_auto_path,
$row
);
Once this is done you are ready to start making changes to the string replacement function in the file. And so further down the page you will find a function called "var_replace" in a section headed "Replace Calendar Variables". At the top of the function you will see a FOR EACH loop that runs through the $data passed in as an argument of the function.
The portion of the code you need to edit within the loop will look something like this to begin with:
array($val['0'], $val['1'], $val['5'], $val['9'], $val['10'], $val['11'], $val['12'], $val['13']), $str);
And shoudl end up looking liek this with the number custom fields you wish to add, in this example 3, with IDs 40, 41 and 42:
array($val['0'], $val['1'], $val['5'], $val['9'], $val['10'], $val['11'], $val['12'], $val['13'], $val['14']['field_id_40'], $val['14']['field_id_41'], $val['14']['field_id_42']), $str);
So that is that for the mod.weblog_calendar.php file, now on to the template itself.
Setting Up The Custom Fields in the Template
The real catch with this hack is that you have to specify the field id variable name in the template rather than the easier to remember variable name specificed in the section custom field manager in admin. My "{eventtitle}" essentially becomes "{field_id_40}".
<h1>{field_id_40}</h1>
If you are wondering where you find these IDs, if you are not sure. When logged in under the control panel, click Admin > Weblog Administration > Custom Weblog Fields > Add/Edit Custom Fields. At this point you should see the ID on the left hand side of each row.
Anyhow, once you have titivated the template and saved it, and made sure you have data entered, you are ready to rock ‘n’ roll, as they say!
Conclusion: A Hack Nonetheless.
As you will see the code works, barring any errors/typos introduced into the code. Its a life saver for a feature that really shoudl already be in Expression Engine, but is not. That aside, this is a real hack and isn;t partcularly flexible. The only saving grace is perhaps that once it is set up initially you wont have to go back and keep on adding field_id_name values.
Thanks to Mr Wilson on the EE forums, and hopefully this will help a few others fix the itch they have from missing features on the calendar in EE.
















