You are here

Attachment.php in jQuery World Calendars API 3.x

File

src/Attachment.php
View source
<?php

namespace Drupal\jquery_calendar;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * An implementation of PageAttachmentInterface for the library.
 */
class Attachment implements ElementAttachmentInterface {
  use StringTranslationTrait;

  /**
   * The service to determin if it should be activated.
   *
   * @var ActivationCheckInterface
   */
  protected $activation;

  /**
   * The module handler.
   *
   * @var ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $settings;

  /**
   * Create an instance of ColorboxAttachment.
   */
  public function __construct(ActivationCheckInterface $activation, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config) {
    $this->activation = $activation;
    $this->moduleHandler = $module_handler;
    $this->settings = $config
      ->get('jquery_calendar.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function isApplicable() {
    return !InstallerKernel::installationAttempted() && $this->activation
      ->isActive();
  }

  /**
   * @param array $page
   * @param $components
   *   Library components that should be loaded on the page.
   *   Either a string with a single filename or comma-separated
   *   filenames. Also it accepts an array of filenames.
   *   Allowed values are:
   *   - all: contains base, plus and datepicker components together.
   *   - core
   *   - plus
   *   - picker
   *   - picker.ext
   *   - validation
   * @param $types
   *   - taiwan
   *   - thai
   *   - julian
   *   - persian
   *   - islamic
   *   - hebrew
   *   - ethiopian
   *   - coptic
   *   - mayan
   *   - ...
   * @param $locales
   *   Library localization files that should be loaded on the page.
   *   This can be an array, a single string or a comma-separated one.
   *   Note that compressions for these components are not available.
   *   If passed empty automatically adds the languages that are enabled in site
   *   Allowed values are:
   *   - lang: contains all Gregorian calendar locales.
   *   - picker.lang: contains all datepicker locales.
   *   - taiwan-zh-TW
   *   - thai-th
   *   - persian-fa
   *   - islamic-ar
   *   - hebrew-he
   *   - ethiopian-am
   *   - gregorian-af
   *   - gregorian-sq
   *   - picker-af
   *   - picker-sq
   *   - picker-fa
   *   - ...
   * @param $theme
   *   Datepicker theme.
   * @param $compression
   *   Compression level:
   *   - normal
   *   - min
   *   - pack
   *
   * @see http://keith-wood.name/calendars.html
   * @see http://keith-wood.name/calendarsPicker.html
   *
   * @todo: Add validation API for component availability.
   */
  public function attach(array &$page, $components = [], $types = [], $locales = [], $theme = NULL, $compression = NULL) {

    // Add current lang to drupal js settings to be used by date pickers on runtime when necessary
    $language = \Drupal::languageManager()
      ->getCurrentLanguage();
    $lang = $language
      ->getId();
    $compression = !empty($compression) ? $compression : \Drupal::config('jquery_calendar.settings')
      ->get('compression_level');
    $theme = !empty($theme) ? $theme : \Drupal::config('jquery_calendar.settings')
      ->get('datepicker_theme');
    $js_settings = [
      'lang' => $lang,
    ];
    foreach ($components as $component) {
      $page['#attached']['library'][] = 'jquery_calendar/jquery_calendars.' . $component;
    }
    foreach ($types as $type) {
      if ($type === 'gregorian') {
        continue;
      }
      $page['#attached']['library'][] = 'jquery_calendar/jquery_calendars.type.' . $type;
    }
    foreach ($locales as $local) {
      if ($local === 'en') {
        continue;
      }
      foreach ($components as $component) {
        $page['#attached']['library'][] = 'jquery_calendar/jquery_calendars.lang.' . $component . '-' . $local;
      }
      foreach ($types as $type) {
        $page['#attached']['library'][] = 'jquery_calendar/jquery_calendars.lang.type.' . $type . '-' . $local;
      }
    }
    if ($theme) {
      $page['#attached']['library'][] = 'jquery_calendar/jquery_calendars.picker.theme.' . $theme;
    }

    // Give other modules the possibility to override settings
    $this->moduleHandler
      ->alter('jquery_calendar_settings', $js_settings, $style);

    // Add js settings.
    $page['#attached']['drupalSettings']['jquery_calendar'] = $js_settings;
  }

}

Classes

Namesort descending Description
Attachment An implementation of PageAttachmentInterface for the library.