o365-2.0.3/modules/o365_outlook_calendar/o365_outlook_calendar.module
modules/o365_outlook_calendar/o365_outlook_calendar.module
<?php
/**
* @file
* Primary module hooks for o365_contacts module.
*/
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Implements hook_theme().
*/
function o365_outlook_calendar_theme($existing, $type, $theme, $path) {
return [
'o365_calendar_list' => [
'variables' => [
'list' => NULL,
'no_items_text' => NULL,
'readmore_class' => NULL,
],
'render element' => 'children',
],
'o365_calendar' => [
'variables' => [
'fromTime' => NULL,
'endTime' => NULL,
'fromDate' => NULL,
'endDate' => NULL,
'fromTs' => NULL,
'endTs' => NULL,
'attendees' => NULL,
'imageData' => NULL,
'subject' => NULL,
'remainingAttendees' => NULL,
'webLink' => NULL,
'onlineMeeting' => NULL,
'onlineMeeting_class' => NULL,
],
'render element' => 'children',
],
];
}
/**
* Implements hook_o365_auth_scopes().
*/
function o365_outlook_calendar_o365_auth_scopes(array &$scopes) {
$scopes[] = 'Calendars.Read';
$scopes[] = 'Calendars.Read.Shared';
$scopes[] = 'Calendars.ReadWrite';
}
/**
* Implements hook_form_alter().
*/
function o365_outlook_calendar_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\Core\Config\ImmutableConfig $moduleConfig */
$moduleConfig = Drupal::config('o365_outlook_calendar.settings');
if (!empty($moduleConfig->get('enabled'))) {
$contentType = $moduleConfig->get('content_type');
$contentTypeFormId = $moduleConfig->get('content_entity_form_id');
$checkFormId = [$contentType . '_form'];
if (!empty($contentTypeFormId)) {
$checkFormId = explode(',', $contentTypeFormId);
}
// Only show our field on new node form and for nodes of the selected
// type.
if (in_array($form_id, $checkFormId, TRUE)) {
// Add out own fields.
$form['fs_o365_calendar'] = [
'#type' => 'container',
'#weight' => 99,
];
$form['fs_o365_calendar']['o365_calendar_save'] = [
'#type' => 'checkbox',
'#title' => new TranslatableMarkup('Save this event in my Outlook Calendar'),
];
$form['fs_o365_calendar']['o365_calendar_attendees'] = [
'#type' => 'textfield',
'#title' => new TranslatableMarkup('Add a comma seperated list of attendee email addresses'),
'#states' => [
'visible' => [
':input[name="o365_calendar_save"]' => ['checked' => TRUE],
],
],
];
// Add a extra submit handler.
$form['actions']['submit']['#submit'][] = '_o365_outlook_calendar_save_submit';
}
}
}
/**
* Our custom submit handler to save the event in the Outlook Calendar.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*/
function _o365_outlook_calendar_save_submit(array $form, FormStateInterface $form_state) {
if (!empty($form_state->getValue('o365_calendar_save'))) {
$form_object = $form_state->getFormObject();
if ($form_object instanceof EntityFormInterface) {
$entity = $form_object->getEntity();
if ($entity) {
/** @var \Drupal\o365_outlook_calendar\OutlookCalendarSaveEventService $eventService */
$eventService = Drupal::service('o365_outlook_calendar.save_event');
$eventService->addEvent($entity, $form_state->getValue('o365_calendar_attendees'));
}
}
}
}
