You are here

beautytips.module in BeautyTips 8

Same filename and directory in other branches
  1. 6.2 beautytips.module
  2. 6 beautytips.module
  3. 7.2 beautytips.module

Provides API for adding beautytips to pages.

File

beautytips.module
View source
<?php

/**
 * @file
 * Provides API for adding beautytips to pages.
 */
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\CacheBackendInterface;

/**
 * Implements hook_page_attachments().
 */
function beautytips_page_attachments(&$page) {
  $config = \Drupal::config('beautytips.basic');
  if ($config
    ->get('beautytips_always_add')) {
    $options = [];
    $selectors = $config
      ->get('beautytips_added_selectors_array');
    if (count($selectors)) {
      foreach ($selectors as $selector) {
        if (!empty($selector)) {
          $options[$selector]['cssSelect'] = $selector;
        }
      }
    }
    $options['.beautytips']['cssSelect'] = '.beautytips';
    beautytips_add_beautytips($page, $options);
  }
}

/**
 * This is the API.  Call this function to add beautytips.
 *
 * @param array $options - See README.txt for details
 */
function beautytips_add_beautytips(&$element, $options = NULL) {
  $settings = [
    'beautytips' => [],
  ];
  if (is_array($options) && count($options)) {
    foreach ($options as $beautytip => $content) {

      // Setup the settings array for adding js
      if (isset($content) && is_array($content)) {
        $settings['beautytips'][$beautytip] = $content;
        $settings['beautytips'][$beautytip]['style'] = isset($content['style']) ? $content['style'] : 'default';
        $keys_no_add = [
          'cssSelect',
          'style',
          'list',
          'text',
          'preEval',
          'ajaxDisableLink',
          'animate',
        ];
        foreach ($settings['beautytips'][$beautytip] as $key => $value) {

          // Ensure that numeric options are not passed as strings.
          $settings['beautytips'][$beautytip][$key] = is_numeric($value) ? (int) $value : $value;
          if (!in_array($key, $keys_no_add)) {
            $settings['beautytips'][$beautytip]['list'][] = $key;
          }
        }
      }
    }
  }
  beautytips_add_js($element);
  if (!empty($settings['beautytips'])) {
    $element['#attached']['drupalSettings']['beautytips'] = $settings['beautytips'];
  }
}

/**
 * Add the basic beautytips javascript to the page.
 */
function beautytips_add_js(&$element) {
  $added =& drupal_static(__FUNCTION__, FALSE);
  if (!$added) {

    // Add beautytips jQuery plugin and module js.
    $element['#attached']['library'][] = 'beautytips/beautytips-module';
    $path = drupal_get_path('module', 'beautytips');
    if (file_exists($path . '/other_libs/excanvas_r3/excanvas.compiled.js')) {

      // Add this for ie compatibility
      $element['#attached']['library'][] = 'beautytips/excanvas_r3';
    }
    if (\Drupal::config('beautytips.basic')
      ->get('beautytips_ltr')) {
      $element['#attached']['library'][] = 'beautytips/beautytips-ltr';
    }

    // Add the styles info.
    $styles = beautytips_get_styles();
    $element['#attached']['drupalSettings']['beautytipStyles'] = $styles;

    // Mark as added so we don't do it again.
    $added = TRUE;
  }
}

/**
 * Get all the defined beautytips styles
 */
function beautytips_get_styles($reload = FALSE) {
  $cache = \Drupal::cache()
    ->get('beautytips:beautytips-styles');
  if (!$cache || $reload) {
    $styles = \Drupal::moduleHandler()
      ->invokeAll('define_beautytips_styles');
    \Drupal::moduleHandler()
      ->alter('define_beautytips_styles', $styles);

    // Save the beautytips style registry in the cache.
    \Drupal::cache()
      ->set('beautytips:beautytips-styles', $styles, CacheBackendInterface::CACHE_PERMANENT, [
      'beautytips',
    ]);
  }
  else {
    $styles = $cache->data;
  }
  return $styles;
}

/**
 * Get an array of options that defines a particular style
 */
function beautytips_get_style($style = 'default') {
  $styles = beautytips_get_styles();
  return isset($styles[$style]) ? $styles[$style] : [];
}

/**
 * Implements hook_define_beautytips_styles().
 */
function beautytips_define_beautytips_styles() {
  $styles['default'] = \Drupal::config('beautytips.basic')
    ->get('beautytips_defaults');

  // Cleanup any problems with defaults.
  if (count((array) $styles['default'])) {
    foreach ($styles['default'] as &$value) {
      if (!is_array($value)) {
        $value = ctype_digit($value) || is_int($value) ? (int) $value : Html::escape($value);
      }
      else {
        if (count($value)) {
          foreach ($value as &$sub_value) {
            $sub_value = Html::escape($sub_value);
          }
        }
      }
    }
  }
  $styles['plain'] = [];
  $styles['netflix'] = [
    'positions' => [
      'right',
      'left',
    ],
    'fill' => '#FFF',
    'padding' => 5,
    'shadow' => TRUE,
    'shadowBlur' => 12,
    'strokeStyle' => '#B9090B',
    'spikeLength' => 50,
    'spikeGirth' => 60,
    'cornerRadius' => 10,
    'centerPointY' => 0.1,
    'overlap' => -8,
    'cssStyles' => [
      'fontSize' => '12px',
      'fontFamily' => 'arial,helvetica,sans-serif',
    ],
  ];
  $styles['facebook'] = [
    'fill' => '#F7F7F7',
    'padding' => 8,
    'strokeStyle' => '#B7B7B7',
    'cornerRadius' => 0,
    'cssStyles' => [
      'fontFamily' => '"lucida grande",tahoma,verdana,arial,sans-serif',
      'fontSize' => '11px',
    ],
  ];
  $styles['transparent'] = [
    'fill' => 'rgba(0, 0, 0, .8)',
    'padding' => 20,
    'strokeStyle' => '#CC0',
    'strokeWidth' => 3,
    'spikeLength' => 40,
    'spikeGirth' => 40,
    'cornerRadius' => 40,
    'cssStyles' => [
      'color' => '#FFF',
      'fontWeight' => 'bold',
    ],
  ];
  $styles['big-green'] = [
    'fill' => '#00FF4E',
    'padding' => 20,
    'strokeWidth' => 0,
    'spikeLength' => 40,
    'spikeGirth' => 40,
    'cornerRadius' => 15,
    'cssStyles' => [
      'fontFamily' => '"lucida grande",tahoma,verdana,arial,sans-serif',
      'fontSize' => '14px',
    ],
  ];
  $styles['google-maps'] = [
    'positions' => [
      'top',
      'bottom',
    ],
    'fill' => '#FFF',
    'padding' => 15,
    'strokeStyle' => '#ABABAB',
    'strokeWidth' => 1,
    'spikeLength' => 65,
    'spikeGirth' => 40,
    'cornerRadius' => 25,
    'centerPointX' => 0.9,
    'cssStyles' => [],
  ];
  $styles['hulu'] = [
    'fill' => '#F4F4F4',
    'strokeStyle' => '#666666',
    'spikeLength' => 20,
    'spikeGirth' => 10,
    'width' => 350,
    'overlap' => 0,
    'centerPointY' => 1,
    'cornerRadius' => 0,
    'cssStyles' => [
      'fontFamily' => '"Lucida Grande",Helvetica,Arial,Verdana,sans-serif',
      'fontSize' => '12px',
      'padding' => '10px 14px',
    ],
    'shadow' => TRUE,
    'shadowColor' => 'rgba(0,0,0,.5)',
    'shadowBlur' => 8,
    'shadowOffsetX' => 4,
    'shadowOffsetY' => 4,
  ];
  return $styles;
}

Functions

Namesort descending Description
beautytips_add_beautytips This is the API. Call this function to add beautytips.
beautytips_add_js Add the basic beautytips javascript to the page.
beautytips_define_beautytips_styles Implements hook_define_beautytips_styles().
beautytips_get_style Get an array of options that defines a particular style
beautytips_get_styles Get all the defined beautytips styles
beautytips_page_attachments Implements hook_page_attachments().