Skip to content

Commit

Permalink
Added calendar link to breadcrumbs
Browse files Browse the repository at this point in the history
Updates #14
  • Loading branch information
inghamn committed Mar 10, 2021
1 parent 67de994 commit d3137a8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 62 deletions.
21 changes: 5 additions & 16 deletions calendar.services.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
# @copyright 2017 City of Bloomington, Indiana
# @copyright 2017-2021 City of Bloomington, Indiana
# @license https://www.gnu.org/licenses/old-licenses/gpl-2.0 GNU/GPL2, see LICENSE
#
# This file is part of the Google Calendar drupal module.
#
# The calendar module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# The calendar module is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the calendar module. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0/>.
---
services:
calendar.twig.TwigExtension:
class: '\Drupal\calendar\Twig\TwigExtension'
tags:
- { name: twig.extension }
calendar.breadcrumb:
class: '\Drupal\calendar\Breadcrumb\BreadcrumbBuilder'
tags:
- { name: 'breadcrumb_builder', priority: 100 }
...
50 changes: 50 additions & 0 deletions src/Breadcrumb/BreadcrumbBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @copyright 2021 City of Bloomington, Indiana
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0 GNU/GPL2, see LICENSE
*/
namespace Drupal\calendar\Breadcrumb;
use Drupal\calendar\GoogleGateway;

use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Link;
use Drupal\Core\Url;

class BreadcrumbBuilder implements BreadcrumbBuilderInterface
{
public function applies(RouteMatchInterface $route_match)
{
$name = explode('.', $route_match->getRouteName());
return $name[0] == 'calendar';
}

public function build(RouteMatchInterface $route_match): Breadcrumb
{
$route = $route_match->getRouteName();
$breadcrumb = new Breadcrumb();
$breadcrumb->addCacheContexts(['url']);
$breadcrumb->addLink(Link::createFromRoute('Home', '<front>'));

$config = \Drupal::config('calendar.settings');
$nids = explode(',', $config->get('calendar_breadcrumb'));
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);
foreach ($nodes as $n) {
$breadcrumb->addLink(Link::createFromRoute($n->title->value, 'entity.node.canonical', ['node'=>$n->nid->value]));
}
if ($route == 'calendar.event_view') {
try {
$id = $route_match->getParameter('calendar_id');
$cal = GoogleGateway::calendar($id);
$uri = "https://calendar.google.com/calendar/embed?src=$id&ctz=America/New_York";
$url = Url::fromUri($uri, ['absolute' => true, 'https' => true]);
$breadcrumb->addLink(Link::fromTextAndUrl($cal->summary, $url));

}
catch (\Exception $e) { }
}

return $breadcrumb;
}
}
38 changes: 9 additions & 29 deletions src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
<?php
/**
* @copyright 2017 City of Bloomington, Indiana
* @copyright 2017-2021 City of Bloomington, Indiana
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0 GNU/GPL2, see LICENSE
*
* This file is part of the Google Calendar drupal module.
*
* The calendar module is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* The calendar module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the calendar module. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0/>.
*/
namespace Drupal\calendar\Form;

Expand All @@ -25,25 +10,16 @@

class SettingsForm extends ConfigFormBase
{
/**
* {@inheritdoc}
*/
public function getFormId()
{
return 'calendar_settings';
}

/**
* {@inheritdoc}
*/
public function getEditableConfigNames()
{
return ['calendar.settings'];
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$config = $this->config('calendar.settings');
Expand All @@ -53,17 +29,21 @@ public function buildForm(array $form, FormStateInterface $form_state)
'#title' => 'Google User Email',
'#default_value' => $config->get('google_user_email')
];
$form['calendar_breadcrumb'] = [
'#type' => 'textfield',
'#title' => 'Breadcrumbs',
'#default_value' => $config->get('calendar_breadcrumb'),
'#description' => 'Enter node IDs, separated by commas, to be used as the base breadcrumb for calendar routes'
];

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$this->config('calendar.settings')
->set('google_user_email', $form_state->getValue('google_user_email'))
->set('google_user_email', $form_state->getValue('google_user_email'))
->set('calendar_breadcrumb', $form_state->getValue('calendar_breadcrumb'))
->save();

parent::submitForm($form, $form_state);
Expand Down
29 changes: 12 additions & 17 deletions src/GoogleGateway.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
<?php
/**
* @copyright 2017-2018 City of Bloomington, Indiana
* @copyright 2017-2021 City of Bloomington, Indiana
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0 GNU/GPL2, see LICENSE
*
* This file is part of the Google Calendar drupal module.
*
* The calendar module is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* The calendar module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the calendar module. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0/>.
*/
namespace Drupal\calendar;

Expand All @@ -40,9 +25,19 @@ private static function getClient()
return $client;
}

/**
* @see https://developers.google.com/calendar/v3/reference/calendars/get
* @throws Google_Service_Exception
*/
public static function calendar(string $calendar_id): \Google_Service_Calendar_Calendar
{
$service = new \Google_Service_Calendar(self::getClient());
return $service->calendars->get($calendar_id);
}

/**
* @see https://developers.google.com/calendar/v3/reference/events/get
* @throws \Google_Service_Exception
* @throws Google_Service_Exception
*/
public static function event(string $calendar_id, string $event_id): \Google_Service_Calendar_Event
{
Expand Down

0 comments on commit d3137a8

Please sign in to comment.