Google Calendar Plugin

Hey everybody,

I’m running into an issue which I can’t fix and can’t even understand: I’m using the Google Calendar Plugin for Kirby and it breaks when it tries to get the DateTime of an event. Pretty strange since I can access every other key in the JSON except this one.

The author of the plugin seems to be gone so my hope is this forum. :smiley:

The JSON looks like this:

and the part where the dateTime key seem to be parsed looks like this:

$events[$i] += ["datestart" =>  new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['start']['dateTime'])->format(option('benediktengel.google-calendar-plugin.formatDate')))];

Pretty normal to me but it won’t access the array. I tried everything, even creating a new JSON Parse to just access this array but it still won’t work.

If you have any hint for me, I would be super happy!

It’s a syntax error. There need to be parenthesis around the new object, before calling the format() method:

$events[$i] += ["datestart" =>  (new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['start']['dateTime'])))->format(option('benediktengel.google-calendar-plugin.formatDate'))];

I don’t know. I tried to access another key written in camelcase and got the same error.
Your snippet now throws this:

ParseError thrown with message “syntax error, unexpected ‘->’ (T_OBJECT_OPERATOR)”

Stacktrace:
#14 ParseError in /Applications/MAMP/htdocs/chiemgauermuenchen2021/site/plugins/google-calendar-plugin/snippets/calendar.php:58
#13 Kirby\Toolkit\F:loadIsolated in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/F.php:380
#12 Kirby\Toolkit\F:load in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/Tpl.php:35
#11 Kirby\Toolkit\Tpl:load in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/config/components.php:288
#10 Kirby\Cms\App:{closure} in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/App.php:1390
#9 Kirby\Cms\App:snippet in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/config/helpers.php:674
#8 snippet in /Applications/MAMP/htdocs/chiemgauermuenchen2021/site/templates/termine.php:7
#7 include in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/F.php:403
#6 Kirby\Toolkit\F:loadIsolated in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/F.php:380
#5 Kirby\Toolkit\F:load in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/Tpl.php:35
#4 Kirby\Toolkit\Tpl:load in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/Template.php:167
#3 Kirby\Cms\Template:render in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/Page.php:1172
#2 Kirby\Cms\Page:render in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/App.php:686
#1 Kirby\Cms\App:io in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/App.php:1039
#0 Kirby\Cms\App:render in /Applications/MAMP/htdocs/chiemgauermuenchen2021/index.php:5

I corrected my code, I’d anyway write this whole snippet differently:

$events[] = [
  "title"       => $event['summary'],
  "datestart"   => (new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['start']['dateTime'])))->format(option('benediktengel.google-calendar-plugin.formatDate')),
  "timestart"   => (new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['start']['dateTime'])))->format(option('benediktengel.google-calendar-plugin.formatTime')),
  "dateEnd"     => (new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['end']['dateTime'])))->format(option('benediktengel.google-calendar-plugin.formatDate')),
  "timeEnd"     => (new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['end']['dateTime'])))->format(option('benediktengel.google-calendar-plugin.formatTime')),
  "url"         => $event['htmlLink'] ?? '',
  "description" => $event['description'] ?? '',
  "location"    => $event['location'] ?? '',
];
1 Like

Oh wow, you’re so quick! Thanks.
Looks way better, but something is still wrong. I don’t know if it can access the dateTime from JSON now. Because:

TypeError thrown with message "DateTime::__construct() expects parameter 1 to be string, object given"

Stacktrace:
#16 TypeError in /Applications/MAMP/htdocs/chiemgauermuenchen2021/site/plugins/google-calendar-plugin/snippets/calendar.php:50
#15 DateTime:__construct in /Applications/MAMP/htdocs/chiemgauermuenchen2021/site/plugins/google-calendar-plugin/snippets/calendar.php:50
#14 include in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/F.php:403
#13 Kirby\Toolkit\F:loadIsolated in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/F.php:380
#12 Kirby\Toolkit\F:load in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/Tpl.php:35
#11 Kirby\Toolkit\Tpl:load in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/config/components.php:288
#10 Kirby\Cms\App:{closure} in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/App.php:1390
#9 Kirby\Cms\App:snippet in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/config/helpers.php:674
#8 snippet in /Applications/MAMP/htdocs/chiemgauermuenchen2021/site/templates/termine.php:7
#7 include in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/F.php:403
#6 Kirby\Toolkit\F:loadIsolated in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/F.php:380
#5 Kirby\Toolkit\F:load in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Toolkit/Tpl.php:35
#4 Kirby\Toolkit\Tpl:load in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/Template.php:167
#3 Kirby\Cms\Template:render in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/Page.php:1172
#2 Kirby\Cms\Page:render in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/App.php:686
#1 Kirby\Cms\App:io in /Applications/MAMP/htdocs/chiemgauermuenchen2021/kirby/src/Cms/App.php:1039
#0 Kirby\Cms\App:render in /Applications/MAMP/htdocs/chiemgauermuenchen2021/index.php:5

What is in line 50 now?

Line 50 is the first line with DateTime:

Hm, ok, then that didn’t work, back to the drawing board. Could you quickly provide a bit of sample data to copy paste? A json string…

1 Like

Sure! This should work:

{
  "kind": "calendar#events",
  "etag": "isThisDataSensitive?",
  "summary": "Zusammenfassung",
  "description": "Beschreibung",
  "updated": "2021-07-22T01:13:17.507Z",
  "timeZone": "Europe/Berlin",
  "accessRole": "reader",
  "defaultReminders": [],
  "nextPageToken": "tokenMacTokface",
  "items": [
    {
      "kind": "calendar#event",
      "etag": "xx",
      "id": "xxx",
      "status": "confirmed",
      "htmlLink": "https://google.com",
      "created": "2014-04-05T09:35:14.000Z",
      "updated": "2014-04-05T10:12:00.455Z",
      "summary": "Zusammenfassung",
      "location": "München, Deutschland",
      "creator": {
        "email": "elon@musk.com",
        "displayName": "Elon Musk"
      },
      "organizer": {
        "email": "Jeffrey@bezos.com",
        "displayName": "Jeff",
        "self": true
      },
      "start": {
        "dateTime": "2014-04-07T18:00:00+02:00"
      },
      "end": {
        "dateTime": "2014-04-07T19:00:00+02:00"
      },
      "iCalUID": "anotherID",
      "sequence": 0,
      "eventType": "default"
    }
  ]
}

I’m sorry, the original code was correct after all, too late :see_no_evil:

$events[] = [
  "title"       => $event['summary'],
  "datestart"   => new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['start']['dateTime'])->format(option('benediktengel.google-calendar-plugin.formatDate'))),
  "timestart"   => new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['start']['dateTime'])->format(option('benediktengel.google-calendar-plugin.formatTime'))),
  "dateEnd"     => new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['end']['dateTime'])->format(option('benediktengel.google-calendar-plugin.formatDate'))),
  "timeEnd"     => new DateTime(DateTime::createFromFormat("Y-m-d?H:i:sP", $event['end']['dateTime'])->format(option('benediktengel.google-calendar-plugin.formatTime'))),
  "url"         => $event['htmlLink'],
  "description" => $event['description'] ?? '',
  "location"    => $event['location'] ?? '',
];

There seems to be another issue.

Did you get an error message or just no result?

haha, no problem. I’m back at: Undefined index: dateTime
This also happens, when I’m trying to get the displayName for example

Oh I forgot: This happens at line 50

Does really every item in your json have a dateTime key?

Where is that called in the code?

Or to ask the question the other way round: Which keys do work?

Somehow all keys work except those written in camelCase :smiley:
So if I try to get the email in creator it works. But if I try to get displayName it breaks.
This is just an example, I never used it inside my code.

And could you insert a dump($event) after line 42?

But the camelCase keys are definitely there, so that’s weird.

Here is the dump of the first array:

Array
(
    [kind] => calendar#event
    [etag] => "2xxx"
    [id] => xxx
    [status] => confirmed
    [htmlLink] => https://google.com
    [created] => 2014-04-05T09:35:14.000Z
    [updated] => 2014-04-05T10:12:00.455Z
    [summary] => xx
    [location] => xx
    [creator] => Array
        (
            [email] => email@mail.com
            [displayName] => email
        )

    [organizer] => Array
        (
            [email] => xxx
            [displayName] => Verein
            [self] => 1
        )

    [start] => Array
        (
            [dateTime] => 2014-04-07T18:00:00+02:00
        )

    [end] => Array
        (
            [dateTime] => 2014-04-07T19:00:00+02:00
        )

    [iCalUID] => xxx
    [sequence] => 0
    [eventType] => default
)

But I call it a day now before I write more nonsense… :sleeping:

It doesn’t make sense.

1 Like

Oh, if I limit the results to 30, it seems to work. It’s not displaying anything since all the events are in the past and the plugin only displays upcoming stuff but maybe there is something empty.

Me too! Time for bed :smiley: Thanks so much for your help! Good night!

Alright, I think I got it:

If you decide to create an event on Google Calendar which is without a time, so just the date, the key is date and if you edit the event and decide to let it start at 6AM, it converts the date to dateTime :weary: :exploding_head: