Last active
September 24, 2017 22:08
-
-
Save laradevitt/4bef3fcab9d74300b81eb546b4147eab to your computer and use it in GitHub Desktop.
(Drupal 8.x) Example code: Add Drupal events to Google calendar with AddToCalendar. https://addtocalendar.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add-to-calendar: | |
css: | |
component: | |
'//addtocalendar.com/atc/1.5/atc-style-menu-wb.css': {type: external} | |
js: | |
'//addtocalendar.com/atc/1.5/atc.min.js': { type: external, minified: true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Preprocesses variables for node--event.html.twig. | |
* | |
* This is an untested, simplified version of production code that used | |
* paragraph entities to store date information, with multiple date ranges | |
* possible per node. This code assumes one date range consisting of two | |
* datetime fields. | |
*/ | |
function mytheme_preprocess_node__event(&$vars) { | |
$node = $vars['node']; | |
// Getting ready to populate properties for AddToCalendar. | |
$vars['#attached']['library'][] = 'mytheme/add-to-calendar'; | |
$atc = [ | |
'atc_date_start' => '', | |
'atc_date_end' => '', | |
'atc_timezone' => 'America/Edmonton', | |
'atc_title' => $node->getTitle(), | |
'atc_description' => '', | |
'atc_location' => '2 Sir Winston Churchill Square, Edmonton, Alberta T5J 2C1', | |
'atc_organizer' => 'Art Gallery of Alberta', | |
'atc_organizer_email' => '[email protected]', | |
]; | |
$atc['atc_description'] = $node->get('field_event_teaser')->view('teaser'); | |
$atc['atc_description'] = render($atc['atc_description']); | |
$atc['atc_description'] = trim(strip_tags($atc['atc_description'])); | |
$start = $node->field_event_start_date->getValue(); | |
$date = $start->getValue(); | |
$date = new DrupalDateTime($date[0]['value'], new DateTimeZone('UTC')); | |
$atc['atc_date_start'] = $date->getTimestamp(); | |
$end = $node->field_event_end_date->getValue(); | |
$date = $end->getValue(); | |
$date = new DrupalDateTime($date[0]['value'], new DateTimeZone('UTC')); | |
$atc['atc_date_end'] = $date->getTimestamp(); | |
foreach ($atc as $k => $v) { | |
$build[$k] = [ | |
'#type' => 'html_tag', | |
'#tag' => 'var', | |
'#value' => $v, | |
'#attributes' => [ | |
'class' => $k, | |
], | |
]; | |
} | |
$wrapper = [ | |
'#type' => 'html_tag', | |
'#tag' => 'span', | |
'#value' => '<a class="atcb-link">Add to Calendar</a><var class="atc_event">' . render($build) . '</var>', | |
'#attributes' => [ | |
'class' => [ | |
'addtocalendar', | |
'atc-style-menu-wb', | |
], | |
], | |
]; | |
// You can append this to any field. | |
$vars['content']['field_event_start_date'][$i]['#suffix'] = render($wrapper); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment