This document is for an older version of

Calendar

.

View latest version →

Developer Events & Hooks

If you wish to extend the capabilities of Calendar, you can use any of the events and hooks below:

Events for Calendar Events

  • onBeforeSave #
    • Called before saving the event data
    • Contains these parameters:
      • event - the Calendar_EventModel
      • isNewEvent - boolean value
  • onAfterSave #
    • Called after saving the event data
    • Contains these parameters:
      • event - the Calendar_EventModel
      • isNewEvent - boolean value
  • onBeforeDelete #
    • Called before deleting an event
    • Contains these parameters:
      • event - the Calendar_EventModel
  • onAfterDelete #
    • Called after deleting an event
    • Contains these parameters:
      • event - the Calendar_EventModel

Usage Instructions

In your plugin's ::init() method, subscribe to any of these events by using the craft()->on() method. For example, to subscribe to the onBeforeSave you would have to have this code in place:

<?php

namespace Craft;

class YourPlugin extends BasePlugin
{
  public function init()
  {
    parent::init();

    craft()->on(
      "calendar_events.onBeforeSave",
      function(Event $event) {
        $calendarEvent = $event->params["event"];
        // Do something with this data
      }
    )
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Events for Calendars

  • onBeforeSave #
    • Called before saving the calendar data
    • Contains these parameters:
      • calendar - the Calendar_CalendarModel
      • isNewCalendar - boolean value
  • onAfterSave #
    • Called after saving the calendar data
    • Contains these parameters:
      • calendar - the Calendar_CalendarModel
      • isNewCalendar - boolean value
  • onBeforeDelete #
    • Called before deleting a calendar
    • Contains these parameters:
      • calendar - the Calendar_CalendarModel
  • onAfterDelete #
    • Called after deleting a calendar
    • Contains these parameters:
      • calendar - the Calendar_CalendarModel

Usage Instructions

In your plugin's ::init() method, subscribe to any of these events by using the craft()->on() method. For examples, to subscribe to the onAfterDelete you would have to have this code in place:

<?php

namespace Craft;

class YourPlugin extends BasePlugin
{
  public function init()
  {
    parent::init();

    craft()->on(
      "calendar_calendars.onAfterDelete",
      function(Event $event) {
        $calendar = $event->params["calendar"];
        // Do something with this data
      }
    )
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Examples

To listen to Craft Commerce order save event, you must make a new plugin (or use an existing one) and in it's ::init() method, listen to the commerce_orders.onSaveOrder event:

class TestPlugin extends BasePlugin
{
  public function init()
  {
    parent::init();

    craft()->on(
      "commerce_orders.onSaveOrder",
      function (Commerce_OrderModel $order) {
        // Create the event
        $event = new Calendar_EventModel();

        $event->getContent()->title = "New Event Title";
        $event->slug				= "new-event-title";

        // Any custom fields go through the ::getContent() model
        $event->getContent()->body = "This is a custom rich text field";

        // Do whatever you need to do with the order data
        // By using the $order OrderModel
        // For example:
        //
        // $event->getContent()->body = "Order total: " . $order->totalPrice;

        // Mandatory fields
        $event->calendarId = 1;
        $event->authorId   = craft()->getUser()->id;
        $event->startDate  = new DateTime();
        $event->endDate	= new DateTime();

        $event->allDay = true;

        // Save the event using Calendar Events service ::saveEvent() method
        craft()->calendar_events->saveEvent($event);
      }
    );
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38