You are here

fullcalendar_view.module in Fullcalendar View 5.x

Full Canlendar Views module help and theme functions.

File

fullcalendar_view.module
View source
<?php

/**
 * @file
 * Full Canlendar Views module help and theme functions.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Datetime\DrupalDateTime;

// Store the preprocess theme functions in a separate .inc file.
\Drupal::moduleHandler()
  ->loadInclude('fullcalendar_view', 'inc', 'fullcalendar_view.theme');

/**
 * Implements hook_theme().
 */
function fullcalendar_view_theme($existing, $type, $theme, $path) {
  return [
    'fullcalendar' => [
      'file' => 'fullcalendar_view.theme.inc',
    ],
  ];
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * Prepopulate the datetime field with the date passed from query parameter.
 */
function fullcalendar_view_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Event start date from query parameter.
  $start = \Drupal::request()->query
    ->get('start');

  // Field name of the start date from query parameter.
  $start_field = \Drupal::request()->query
    ->get('start_field');
  if (!empty($start) && !empty($start_field)) {
    $node = $form_state
      ->getFormObject()
      ->getEntity();

    // Only handle new node with the start field.
    if ($node
      ->isNew() && isset($form[$start_field]['widget'][0]['value'])) {

      // Only handle datetime field.
      if ($form[$start_field]['widget'][0]['value']['#type'] === 'datetime') {

        // Prepopulate the start date field as the event date.
        $form[$start_field]['widget'][0]['value']['#default_value'] = new DrupalDateTime($start, 'UTC');
      }
    }
  }
}

/**
 * Implements hook_library_info_alter().
 */
function fullcalendar_view_library_info_alter(array &$libraries, $module) {
  if ('fullcalendar_view' == $module) {

    // Use CDN instead of all local missing libraries.
    // JSFrame.js
    $cdn_library = _fullcalendar_view_use_cdn_full_path($libraries, 'libraries.jsframe', 'js');
    if ($cdn_library) {
      $libraries['libraries.jsframe']['js'] = $cdn_library;
    }

    // Moment.js
    $cdn_library = _fullcalendar_view_use_cdn_full_path($libraries, 'libraries.moment', 'js');
    if ($cdn_library) {
      $libraries['libraries.moment']['js'] = $cdn_library;
    }

    // RRule js.
    $cdn_library = _fullcalendar_view_use_cdn_full_path($libraries, 'libraries.rrule', 'js');
    if ($cdn_library) {
      $libraries['libraries.rrule']['js'] = $cdn_library;
    }

    // Fullcalendar default theme.
    $cdn_library = _fullcalendar_view_use_cdn($libraries, 'libraries.fullcalendar-theme', 'css');
    if ($cdn_library) {
      $libraries['libraries.fullcalendar-theme']['css']['component'] = $cdn_library;
    }

    // Fullcalendar js.
    $cdn_library = _fullcalendar_view_use_cdn($libraries, 'libraries.fullcalendar', 'js');
    if ($cdn_library) {
      $libraries['libraries.fullcalendar']['js'] = $cdn_library;
    }
  }
}

/**
 * Replace local library with CDN.
 *
 * @param array $libraries
 *   The libraries array.
 * @param string $library_name
 *   The library name.
 * @param string $type
 *   The library type.
 * @param bool $replace_local
 *   Force to replace local libraries with CDN.
 *
 * @return array
 *   The new library array (CDN)
 */
function _fullcalendar_view_use_cdn(array $libraries, $library_name, $type, $replace_local = FALSE) {
  if (isset($libraries[$library_name])) {
    if (isset($libraries[$library_name][$type]) && isset($libraries[$library_name]['cdn'])) {
      $library_array = [];
      $updated = FALSE;

      // CSS library has a sub-array called component.
      if ($type === 'css') {
        if (isset($libraries[$library_name][$type]['component'])) {
          $local_library = $libraries[$library_name][$type]['component'];
        }
        else {
          return FALSE;
        }
      }
      else {

        // Local js library.
        $local_library = $libraries[$library_name][$type];
      }
      foreach ($local_library as $key => $value) {
        if (!file_exists(DRUPAL_ROOT . $key) || $replace_local) {

          // The js file doesn't exist.
          // Replace it with remote cdn.
          $path = explode('/', $key);
          $end = count($path);
          $plugin_url = $path[$end - 2] . '@' . $libraries[$library_name]['version'] . '/' . $path[$end - 1];
          $library_array[$libraries[$library_name]['cdn'] . $plugin_url] = $value;
          $updated = TRUE;
        }
        else {
          $library_array[$key] = $value;
        }
      }
    }
  }
  return empty($updated) ? FALSE : $library_array;
}

/**
 * Replace local rrule library with CDN.
 *
 * @param array $libraries
 *   The libraries array.
 * @param string $library_name
 *   The library name.
 * @param string $type
 *   The library type.
 * @param bool $replace_local
 *   Force to replace local libraries with CDN.
 *
 * @return array
 *   The new library array (CDN)
 */
function _fullcalendar_view_use_cdn_full_path(array $libraries, $library_name, $type, $replace_local = FALSE) {
  if (isset($libraries[$library_name])) {
    if (isset($libraries[$library_name][$type]) && isset($libraries[$library_name]['cdn'])) {
      $library_array = [];
      $updated = FALSE;

      // CSS library has a sub-array called component.
      if ($type === 'css') {
        if (isset($libraries[$library_name][$type]['component'])) {
          $local_library = $libraries[$library_name][$type]['component'];
        }
        else {
          return FALSE;
        }
      }
      else {

        // Local js library.
        $local_library = $libraries[$library_name][$type];
      }
      foreach ($local_library as $key => $value) {
        if (!file_exists(DRUPAL_ROOT . $key) || $replace_local) {

          // The js file doesn't exist.
          // Replace it with remote cdn.
          $library_array[$libraries[$library_name]['cdn']] = $value;
          $updated = TRUE;
        }
        else {
          $library_array[$key] = $value;
        }
      }
    }
  }
  return empty($updated) ? FALSE : $library_array;
}