You are here

class Attachment in jQuery World Calendars API 3.x

An implementation of PageAttachmentInterface for the library.

Hierarchy

Expanded class hierarchy of Attachment

1 string reference to 'Attachment'
jquery_calendar.services.yml in ./jquery_calendar.services.yml
jquery_calendar.services.yml
1 service uses Attachment
jquery_calendar.attachment in ./jquery_calendar.services.yml
Drupal\jquery_calendar\Attachment

File

src/Attachment.php, line 13

Namespace

Drupal\jquery_calendar
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Attachment::$activation protected property The service to determin if it should be activated.
Attachment::$moduleHandler protected property The module handler.
Attachment::$settings protected property The settings.
Attachment::attach public function @todo: Add validation API for component availability. Overrides ElementAttachmentInterface::attach
Attachment::isApplicable public function Check if the attachment should be added to the current page. Overrides ElementAttachmentInterface::isApplicable
Attachment::__construct public function Create an instance of ColorboxAttachment.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.