You are here

animations.module in Animations (JS/CSS) 8

File

animations.module
View source
<?php

// helper function to check if a string ends with a certain extension
function animations_endsWith($haystack, $needle) {
  $length = strlen($needle);
  if ($length == 0) {
    return true;
  }
  return substr($haystack, -$length) === $needle;
}

/**
* Implements hook_library_info_build(). For creating dynamic libraries.
*/
function animations_library_info_build() {
  $libs = [];

  // create dynamic libraries from the module config
  $config = \Drupal::config('animations.config');
  foreach ($config
    ->get("animations_dependencies") as $key => $library) {

    // create css library
    if (animations_endsWith($library["file"], "css")) {
      $libs['animations.' . $key] = [
        'css' => [
          'base' => [
            '/libraries/' . $key . '/' . $library["file"] => [],
          ],
        ],
      ];
    }
    else {
      if (animations_endsWith($library["file"], "js")) {
        $libs['animations.' . $key] = [
          'js' => [
            '/libraries/' . $key . '/' . $library["file"] => [],
          ],
        ];
      }
    }
  }
  return $libs;
}

/**
 * Implements hook_page_attachments(). Adds the library to every or selected pages.
 */
function animations_page_attachments(array &$page) {

  // attach the dynamic libraries and the main library to all pages
  $config = \Drupal::config('animations.config');
  foreach ($config
    ->get("animations_dependencies") as $key => $library) {
    $page['#attached']['library'][] = 'animations/animations.' . $key;
  }
  $page['#attached']['library'][] = 'animations/animations';

  // pass the configuration file to the js
  $config = \Drupal::config('animations.config');
  $page['#attached']['drupalSettings']['animations'] = $config
    ->get("animations");
}

/* TODO: make a good help page */
function animations_help($path, $arg) {
  $output = "";
  switch ($path) {
    case "help.page.animations":
      $output = t('The module provides an easy way to include common animations into your website. <br />In order to add a new animation:<br />1. Go to "Configure".<br />2. Expand the desired animation tab.<br />3. Fill in your css selectors (each one on a new line). The animation will be applied to the filled css selectors. ');
      break;
  }
  return $output;
}

Functions

Namesort descending Description
animations_endsWith
animations_help
animations_library_info_build Implements hook_library_info_build(). For creating dynamic libraries.
animations_page_attachments Implements hook_page_attachments(). Adds the library to every or selected pages.