This document is for an older version of

Calendar

.

View latest version →

Developer

Developer Hooks

Calendar provides hooks that give plugins the opportunity to insert their own template code into Calendar's templates. This allows you to add HTML, Javascript and Twig variables to extend the capabilities of Calendar.

Control Panel

When viewing calendars in the control panel, you'll have access to the following hooks:

cp.solspace.calendar.filters 4.0.3+

This hook can be used to customize the calendar filters. Using the option criteria, you can pass additional parameters when querying event data.

Usage Examples

Add Author Filtering to Month/Week/Day View

In your plugin's ::init() method, or by using the Event::on() static method:

<?php

namespace Your\Plugin\Namespace;

use Solspace\Calendar\Events\SaveElementEvent;

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

    Craft::$app->view->hook('cp.solspace.calendar.filters', function () {
      $calendarId = 5;

      $events = Calendar::getInstance()->events
        ->getEventQuery([
          'calendarId' => [$calendarId],
        ])
        ->all();

      $authors = [];

      foreach ($events as $event) {
        $authorsList = '';
        $authorsList .= '<nav>';
        $authorsList .= '   <ul class="calendar-list">';
        $authorsList .= '       <li class="heading">';
        $authorsList .= '           <a href="#" class="sel" style="display: none;">Authors</a>';
        $authorsList .= '           <span>Authors</span>';
        $authorsList .= '       </li>';

        $authors[] = $event->getAuthor();
        $authors = array_unique($authors);

        foreach ($authors as $author) {
          $authorsList .= '       <li class="item">';
          $authorsList .= '           <input type="checkbox" name="criteria[authorId][]" id="calendar-author-'.$author->id.'" value="'.$author->id.'" />';
          $authorsList .= '           <label for="calendar-author-'.$author->id.'">';
          $authorsList .= '               <span style="background: red; color: white; border-color: black;"></span>';
          $authorsList .= '               '.$author->firstName.' '.$author->lastName;
          $authorsList .= '           </label>';
          $authorsList .= '       </li>';
        }
      }

      $authorsList .= '   </ul>';
      $authorsList .= '</nav>';

      return $authorsList;
    });

    Event::on(
      UrlManager::class,
      UrlManager::EVENT_REGISTER_CP_URL_RULES,
      function (RegisterUrlRulesEvent $event) {
        $event->rules['cpActionTrigger1'] = 'events/create';
        $event->rules['cpActionTrigger2'] = 'submissions/create';
        $event->rules['cpActionTrigger3'] = 'spam/create';
      }
    );
  }
}
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63