beautytips_manager.module in BeautyTips 8
Code related to defining and displaying custom beautytips and styles.
File
beautytips_manager/beautytips_manager.moduleView source
<?php
use Drupal\Component\Render\FormattableMarkup;
/**
* @file
* Code related to defining and displaying custom beautytips and styles.
*/
use Drupal\Core\Url;
use Drupal\Component\Utility\Xss;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\CacheBackendInterface;
/**
* Implements hook_page_attachments().
*/
function beautytips_manager_page_attachments(&$page) {
$options = [];
$tips = beautytips_manager_get_custom_tips();
if (count($tips)) {
foreach ($tips as $tip) {
if (!$tip->enabled) {
continue;
}
// Match path if necessary.
if ($tip->pages) {
$pathCarrent = Url::fromRoute('<current>');
$path = Drupal::service('path.alias_manager.cached')
->getPathAlias($pathCarrent);
// Compare with the internal and path alias (if any).
$page_match = \Drupal::service('path.matcher')
->matchPath($path, $tip->pages);
if ($path != $pathCarrent) {
$page_match = $page_match || \Drupal::service('path.matcher')
->matchPath($pathCarrent, $tip->pages);
}
// When $tip->visibility has a value of 0, the beautytip is displayed on
// all pages except those listed in $tip->pages. When set to 1, it
// is displayed only on those pages listed in $tip->pages.
$page_match = !($tip->visibility xor $page_match);
}
else {
if (!$tip->visibility) {
$page_match = TRUE;
}
else {
$page_match = FALSE;
}
}
if ($page_match) {
$options['beautytips_manager_custom_' . $tip->id] = beautytips_manager_build_beautytip($tip);
}
}
}
if (count($options)) {
beautytips_add_beautytips($page, $options);
}
}
/**
* Implements hook_library_info_build().
*/
function beautytips_manager_library_info_build() {
// ColorPicker plugin.
$libraries['colorpicker'] = [
'title' => 'ColorPicker',
'website' => 'http://www.eyecon.ro/colorpicker/',
'version' => '',
'js' => [
'/' . drupal_get_path('module', 'beautytips') . '/other_libs/colorpicker/js/colorpicker.js' => [],
],
'css' => [
'theme' => [
'/' . drupal_get_path('module', 'beautytips') . '/other_libs/colorpicker/css/colorpicker.css' => [],
],
],
];
return $libraries;
}
/**
* Implements hook_permission().
*/
function beautytips_manager_permission() {
return [
'use Javascript for custom beautytip display' => [
'title' => t('Use Javascript for custom beautytip display'),
],
];
}
/**
* Delete a singular custom beautytip.
*/
function beautytips_manager_delete_custom_tip($id) {
\Drupal::database()
->delete('beautytips_custom_tips')
->condition('id', $id)
->execute();
}
/**
* Save a singular beautytip.
*/
function beautytips_manager_save_custom_tip($tip) {
$tip = (array) $tip;
$tip_id = isset($tip['id']) && $tip['id'] ? $tip['id'] : NULL;
\Drupal::database()
->merge('beautytips_custom_tips')
->key([
'id' => $tip_id,
])
->fields($tip)
->execute();
return $tip;
}
/**
* Retrieve a list of all possible triggers.
*
* TODO: Don't include all of these.
*/
function beautytips_manager_get_triggers() {
return [
'hover' => 'hover',
'hoverIntent' => 'hoverIntent',
'click' => 'click',
'dblclick' => 'dblclick',
'blur' => 'blur',
'focus' => 'focus',
'mouseover' => 'mouseover',
'mouseout' => 'mouseout',
'mousedown' => 'mousedown',
'mousemove' => 'mousemove',
'mouseenter' => 'mouseenter',
'mouseleave' => 'mouseleave',
'change' => 'change',
'select' => 'select',
'submit' => 'submit',
'keydown' => 'keydown',
'keypress' => 'keypress',
'keyup' => 'keyup',
'error' => 'error',
'load' => 'load',
'unload' => 'unload',
'resize' => 'resize',
'scroll' => 'scroll',
];
}
/**
* Retrieve all custom beautytips.
*/
function beautytips_manager_get_custom_tips() {
$tips = [];
$cache = \Drupal::cache()
->get('beautytips:beautytips-ui-custom-tips');
if (!$cache) {
$results = \Drupal::database()
->query("SELECT * FROM {beautytips_custom_tips}");
foreach ($results as $result) {
$tips[$result->id] = $result;
}
\Drupal::cache()
->set('beautytips:beautytips-ui-custom-tips', $tips, CacheBackendInterface::CACHE_PERMANENT, [
'beautytips',
]);
}
else {
$tips = $cache->data;
}
return $tips;
}
/**
* Retrieves a single custom beautytip.
*/
function beautytips_manager_get_custom_tip($id) {
$sql = "SELECT * FROM {beautytips_custom_tips} WHERE id = :id";
$result = \Drupal::database()
->query($sql, [
':id' => $id,
]);
return $result
->fetchObject();
}
/**
* Given a custom tip.
*
* Given a custom tip, build an array of options
* that can be passed to beautytips_add_beautytips().
*/
function beautytips_manager_build_beautytip($tip) {
$single_triggers = [
'hover',
'hoverIntent',
];
$trigger = in_array($tip->trigger_on, $single_triggers) ? $tip->trigger_on : [
$tip->trigger_on,
$tip->trigger_off,
];
$options = [
'cssSelect' => Html::escape($tip->element),
'style' => $tip->style,
'trigger' => $trigger,
'shrinkToFit' => (bool) $tip->shrink,
'ajaxDisableLink' => (bool) $tip->disable_link,
];
if ($tip->animation_on) {
$options['animate']['on'] = $tip->animation_on;
}
if ($tip->animation_off) {
$options['animate']['off'] = $tip->animation_off;
}
if ($tip->positions) {
$options['positions'] = explode(',', $tip->positions);
}
switch ($tip->content_type) {
case 'attribute':
if ($tip->content) {
$options['contentSelector'] = "\$(this).attr('" . Html::escape($tip->content) . "')";
}
break;
case 'text':
$options['text'] = new FormattableMarkup($tip->content);
break;
case 'ajax':
$options['ajaxPath'] = !$tip->content ? "\$(this).attr('href')" : [
"\$(this).attr('href')",
Xss::filter($tip->content),
];
break;
case 'js':
$options['contentSelector'] = Xss::filter($tip->content);
break;
}
return $options;
}
/**
* Delete a singular custom beautytip.
*/
function beautytips_manager_delete_custom_style($id) {
\Drupal::database()
->delete('beautytips_custom_styles')
->condition('id', $id)
->execute();
}
/**
* Save a singular beautytip style.
*/
function beautytips_manager_save_custom_style($style) {
$style = (array) $style;
$style_id = isset($style['id']) && $style['id'] ? $style['id'] : NULL;
\Drupal::database()
->merge('beautytips_custom_styles')
->key([
'id' => $style_id,
])
->fields($style)
->execute();
return $style;
}
/**
* Contains an array of beautytip style options
* for mapping the names between php and javascript.
*/
function beautytips_manager_style_mapping() {
return [
'options' => [
'fill' => 'fill',
'strokeWidth' => 'stroke_width',
'strokeStyle' => 'stroke_style',
'width' => 'width',
'padding' => 'padding',
'cornerRadius' => 'corner_radius',
'spikeGirth' => 'spike_girth',
'spikeLength' => 'spike_length',
'shadow' => 'shadow',
'shadowBlur' => 'shadow_blur',
'shadowColor' => 'shadow_color',
'cssClass' => 'css_class',
],
'css_options' => [
'color' => 'css_color',
'fontFamily' => 'css_font_family',
'fontWeight' => 'css_font_weight',
'fontSize' => 'css_font_size',
],
];
}
/**
* Get all defined custom styles.
*/
function beautytips_manager_get_custom_styles() {
$styles = [];
$results = \Drupal::database()
->query("SELECT * FROM {beautytips_custom_styles}");
foreach ($results as $result) {
$styles[$result->id] = $result;
}
return $styles;
}
/**
* Grab a custom style from the database.
*/
function beautytips_manager_get_custom_style($id) {
$sql = "SELECT * FROM {beautytips_custom_styles} WHERE id = :id";
$result = \Drupal::database()
->query($sql, [
':id' => $id,
]);
return $result
->fetchObject();
}
/**
* Implements hook_define_beautytips_styles().
*/
function beautytips_manager_define_beautytips_styles() {
$styles = [];
$custom_styles = beautytips_manager_get_custom_styles();
$style_map = beautytips_manager_style_mapping();
$style_options = array_flip($style_map['options']);
$css_style_options = array_flip($style_map['css_options']);
if (count($custom_styles)) {
foreach ($custom_styles as $style) {
$options = [];
foreach ($style as $key => $value) {
if (isset($style_options[$key])) {
if ($key == 'shadow') {
if ($value != 'default') {
$options['shadow'] = $value == 'shadow' ? TRUE : FALSE;
}
}
else {
if (!is_null($value) && $value != '') {
// Add the setting and make sure integers stay as integers.
$options[$style_options[$key]] = ctype_digit($value) || is_int($value) ? (int) $value : Html::escape($value);
}
}
}
else {
if (isset($css_style_options[$key])) {
if (!is_null($value) && $value != '') {
$options['cssStyles'][$css_style_options[$key]] = Html::escape($value);
}
}
}
}
$styles[$style->name] = $options;
}
}
return $styles;
}