Skip to content
Snippets Groups Projects
Commit c40bf34f authored by Daniel Rodriguez's avatar Daniel Rodriguez
Browse files

Issue # 3375651: Add event url to entity

parent 0364fee8
Branches 5.x
No related tags found
No related merge requests found
......@@ -32,6 +32,29 @@ function google_calendar_service_help(
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function google_calendar_service_gcs_calendar_event_update(EntityInterface $entity) {
$calendar = \Drupal::entityTypeManager()->getStorage('gcs_calendar')->load($entity->get('calendar')->target_id);
\Drupal::service('google_calendar_service.edit_events')->setEventUrl($entity, $calendar);
}
use Drupal\Core\StringTranslation\TranslatableMarkup;
function google_calendar_service_update_8201() {
$field_storage_definition = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Event URL'))
->setDescription(new TranslatableMarkup("The url for the calendar event"))
->setSettings(["max_length" => 1024, "text_processing" => 0])
->setDefaultValue("")
->setDisplayOptions("view", ["label" => "above", "type" => "string", "weight" => 0])
->setDisplayOptions("form", ["type" => "string_textfield", "weight" => 0]);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('event_url', 'gcs_calendar_event', 'google_calendar_service', $field_storage_definition);
}
/**
* Implements hook_cron().
*
......
......@@ -186,6 +186,47 @@ class CalendarEditEvents {
}
}
/**
* Get google calendar event.
*
* @param string $calendarId
* The calendar id.
* @param string $eventId
* The event id.
*
* @return object
* Return calendar event.
*/
public function getGoogleCalendarEvent($calendarId, $eventId) {
try {
return $this->service->events->get($calendarId, $eventId);
}
catch (Google_Service_Exception $e) {
// Catch non-authorized exception.
if ($e->getCode() == 401) {
return FALSE;
}
}
}
/**
* Set event url.
*
* @param object $event
* The event.
* @param object $calendar
* The calendar.
*/
public function setEventUrl($event, $calendar) {
$calendarId = $calendar->get('calendar_id')->value;
$eventId = $event->get('event_id')->value;
$google_event = $this->getGoogleCalendarEvent($calendarId, $eventId);
if ($google_event['htmlLink'] != $event->get('event_url')->value) {
$event->set('event_url', ['value' => $google_event['htmlLink']]);
$event->save();
}
}
/**
* Delete google calendar event.
*
......
......@@ -245,6 +245,7 @@ class CalendarImport {
$query = $this->entityTypeManager
->getStorage('gcs_calendar_event')
->getQuery()
->condition('calendar', $calendar->id(), 'IN')
->condition('event_id', $event_ids, 'IN');
$existent_event_ids = $query->execute();
......@@ -262,6 +263,7 @@ class CalendarImport {
// Delete events if are not in the $events.
if (!empty($existent_event_ids)) {
$this->database->delete('gcs_calendar_event')
->condition('calendar', $calendar->id(), 'IN')
->condition('id', array_values($existent_event_ids), 'NOT IN')
->execute();
}
......@@ -310,7 +312,7 @@ class CalendarImport {
],
'description' => [
'value' => $event['description'],
'format' => 'basic_html',
'format' => 'plain_text',
],
'location' => [
'value' => $event['location'],
......@@ -323,6 +325,9 @@ class CalendarImport {
'value' => $end_date->setTimezone(new DateTimeZone('UTC'))
->getTimestamp(),
],
'event_url' => [
'value' => $event['htmlLink'],
],
];
if (!$event_entity) {
......
......@@ -8,6 +8,7 @@ use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Defines the Calendar Event entity.
......@@ -330,6 +331,14 @@ class CalendarEvent extends ContentEntityBase implements
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['event_url'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Event URL'))
->setDescription(new TranslatableMarkup("The url for the calendar event"))
->setSettings(["max_length" => 1024, "text_processing" => 0])
->setDefaultValue("")
->setDisplayOptions("view", ["label" => "above", "type" => "string", "weight" => 0])
->setDisplayOptions("form", ["type" => "string_textfield", "weight" => 0]);
return $fields;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment