communico_plus-1.0.0-beta3/communico_plus.module
communico_plus.module
<?php
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* @param array $suggestions
* @param array $variables
* @param $hook
* Implements hook_theme_suggestions_alter().
*/
function communico_plus_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if ($hook == 'form' & !empty($variables['element']['#theme'])) {
$suggestions[] = 'form__' . $variables['element']['#theme'][0];
}
}
/**
* @param array $suggestions
* @param array $variables
* Implements hook_theme_suggestons_block_alter()
*/
function communico_plus_theme_suggestions_block_alter(array &$suggestions, array $variables) {
$content = $variables['elements']['content'];
if (isset($content['#block_content']) && $content['#block_content'] instanceof \Drupal\block_content\BlockContentInterface) {
$suggestions[] = 'block__' . $content['#block_content']->bundle();
}
}
/**
* @param $form
* @param FormStateInterface $form_state
* @param $form_id
* Implements hook_form_alter()
*/
function communico_plus_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'communico_plus_filter_form') {
$form['#attached']['library'][] = 'communico_plus/communico_plus.library';
}
}
/**
* Implements hook_theme().
*
*/
function communico_plus_theme($existing, $type, $theme, $path) {
return [
'communico_plus_block' => [
'variables' => [
'events' => NULL,
],
'template' => 'communico_plus_block',
],
'communico_plus_item' => [
'variables' => [
'title_link' => NULL,
'start_date' => NULL,
'end_date' => NULL,
'location' => NULL,
'room' => NULL,
],
'template' => 'communico_plus_item',
],
'communico_plus_filter_block' => [
'variables' => [
'events' => NULL,
],
'template' => 'communico_plus_filter_block',
],
'communico_plus_filter_item' => [
'variables' => [
'filter_form' => NULL,
'details' => NULL,
],
'template' => 'communico_plus_filter_item',
],
'communico_plus_filter_form' => [
'render element' => 'children',
'template' => 'form__communico_plus_filter_form',
],
'communico_plus_event_page' => [
'variables' => [
'event_data' => [],
'expire_date' => NULL,
'branch_link' => NULL,
'calendar_image_path' => NULL,
'map_pin_image_path' => NULL,
'reg_url' => NULL,
'expired_text' => NULL,
'description' => NULL,
'one_image' => NULL,
],
'template' => 'communico_plus_event_page',
],
'communico_plus_reservation_page' => [
'variables' => [
'reservation_data' => [],
'expire_date' => NULL,
'branch_link' => NULL,
'expired_text' => NULL,
'description' => NULL,
],
'template' => 'communico_plus_reservation_page',
],
];
}
/**
* Implements HOOK_cron.
*
* @return void
*/
function communico_plus_cron() {
$importConfig = \Drupal::config('communico_plus.import.settings');
$autoRemove = $importConfig->get('delete_unpublished');
$autoSave = $importConfig->get('auto_update_events');
$entityTypeManager = \Drupal::service('entity_type.manager')->getStorage('node');
$utilityService = \Drupal::service('communico_plus.utilities');
$queueFactory = \Drupal::service('queue');
// Unpublish expired events.
$eventNids = $entityTypeManager->getQuery()
->accessCheck(FALSE)
->condition('type', 'event_page')
->condition('status', '1')
->execute();
foreach($eventNids as $id) {
$node = $entityTypeManager->load($id);
if ($utilityService->checkIsEventExpired($node->field_communico_start_date->value)) {
$node->setUnpublished();
$node->save();
}
}
if ($autoSave == 1) {
$eventSyncQueue = $queueFactory->get('communico_event_sync_queue');
// get the libraries we are storing
foreach ($utilityService->locationDropdown() as $locationId => $nameString) {
// If we are storing nodes with this location already, create queue item.
if ($utilityService->checkLocationExists($locationId)) {
// Create queue item.
$item = new \stdClass();
$item->startDate = date('Y-m-d');
$item->endDate = date('Y-m-d', strtotime('last day of +1 month'));
$item->locationId = $locationId;
$eventSyncQueue->createItem($item);
}
}
}
if ($autoRemove == 1) {
$eventDeleteQueue = $queueFactory->get('communico_event_delete_queue');
$expiredNids = $entityTypeManager->getQuery()
->accessCheck(FALSE)
->condition('type', 'event_page')
->condition('status', '0')
->execute();
foreach ($expiredNids as $id) {
$item = new \stdClass();
$item->id = $id;
$eventDeleteQueue->createItem($item);
}
}
}
/**
* @return array
*
*/
function communicoPlusGetTypesArray() {
$dropdownArray = [];
$connector = \Drupal::service('communico_plus.connector');
$return = $connector->getEventTypes();
foreach($return['data']['entries'] as $index => $name) {
$dropdownArray[$index] = $name['name'];
}
return $dropdownArray;
}
/**
* @return array
*/
function communicoPlusGetLocationArray() {
$dropdownArray = [];
$connector = \Drupal::service('communico_plus.connector');
$return = $connector->getLibraryLocations();
foreach($return['data']['entries'] as $name) {
$dropdownArray[$name['id']] = $name['name'];
}
return $dropdownArray;
}
/**
* @return array
*/
function communicoPlusGetAgegroupsArray() {
$dropdownArray = [];
$connector = \Drupal::service('communico_plus.connector');
$return = $connector->getEventAgeGroups();
foreach($return['data']['entries'] as $index => $group) {
$dropdownArray[$index] = $group;
}
return $dropdownArray;
}
/**
* @param $valArray
* @return true
* @throws InvalidPluginDefinitionException
* @throws PluginNotFoundException
* @throws EntityStorageException
*/
function createEventPageNode($valArray) {
$entityTypeManager = \Drupal::service('entity_type.manager');
$utilityService = \Drupal::service('communico_plus.utilities');
$newEventPage = $entityTypeManager->getStorage('node')->create(['type' => 'event_page']);
$start_date = $utilityService->findDateFromDatestring($valArray['eventStart']);
$end_date = $utilityService->findDateFromDatestring($valArray['eventEnd']);
$agesArray = [];
$typesArray = [];
foreach($valArray['ages'] as $age) {
$agesArray['value'] = $age;
}
foreach($valArray['types'] as $type) {
$typesArray['value'] = $type;
}
$newEventPage->set('title', $valArray['title']);
$newEventPage->set('field_communico_subtitle', ['value' => $valArray['subTitle']]);
$newEventPage->set('field_communico_shortdescription', ['value' => $valArray['shortDescription']]);
$newEventPage->set('body', ['value' => $valArray['description'], 'format' => 'basic_html']);
$newEventPage->set('field_communico_age_group', $agesArray);
$newEventPage->set('field_communico_event_id', ['value' => $valArray['eventId']]);
$newEventPage->set('field_communico_event_type', $typesArray);
$newEventPage->set('field_communico_start_date', ['value' => $start_date]);
$newEventPage->set('field_communico_end_date', ['value' => $end_date]);
$newEventPage->set('field_communico_library_location', ['value' => $valArray['locationName']]);
$newEventPage->set('field_communico_location_id', ['value' => $valArray['locationId']]);
$newEventPage->enforceIsNew();
$newEventPage->save();
return true;
}
/**
* @param $success
* @param $results
* @param $operations
* @return void
*
*/
function communicoPlusFinished($success, $results, $operations) {
if ($success) {
$message = t('All operations completed successfully.');
}
else {
$message = t('Finished with an error.');
}
\Drupal::messenger()->addStatus($message);
}
/**
* Implements hook_help().
*/
function communico_plus_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.communico_plus':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<div>' . t('This module allows content from Communico to be integrated into Drupal.') . '</div>';
$output .= '<div>' . t('Go to the admin page <a href=":aliases">"/admin/config/communico_plus/config"</a> and input the correct information for your environment and save the config.', [':aliases' => Url::fromRoute('communico_plus.config')->toString()]) . '</div>';
$output .= '<div>' . t('Check the " Rebuild the filter block select element values" checkbox and hit save. This is necessary to build the library locations dropdown.') . '</div>';
$output .= '<div>' . t('Go to the import admin page <a href=":aliases">"/admin/config/communico_plus/import"</a> to import Communico events as node content.', [':aliases' => Url::fromRoute('communico_plus.import.config')->toString()]) . '</div>';
return $output;
}
}
