Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
drupal/melt_glossify / melt_glossify.module
Size: Mime:
<?php

/**
 * @file
 * Contains melt_glossify.module.
 */

use Drupal\Component\Serialization\Yaml;

require_once __DIR__ . '/melt_glossify.tokens.inc';

/**
 * Implements hook_page_attachments().
 */
function melt_glossify_page_attachments(array &$attachments) {

  // Exit if we're on an admin route.
  if (\Drupal::service('router.admin_context')->isAdminRoute()) {
    return FALSE;
  }

  $config = \Drupal::config('melt_glossify.settings');
  $color = $config->get('tippyjs.color');

  try {
    $yaml = Yaml::decode($config->get('tippyjs.yaml'));
  }
  catch (Exception $e) {
    \Drupal::logger('melt_glossify')->error('@message', [
      '@message' => $e,
    ]);
  }

  if (!empty($color)) {
    $tippyCss = melt_glossify_create_tippyjs_inline_css($color);
    $attachments['#attached']['html_head'][] = [
      [
        '#type' => 'html_tag',
        '#tag' => 'style',
        '#value' => $tippyCss,

      ],
      // A key, to make it possible to recognize
      // this HTML element when altering.
      'tippyjs-css',
    ];
  }

  $attachments['#attached']['drupalSettings']['meltGlossify']['tippyjs'] = $yaml;
  $attachments['#attached']['library'][] = 'melt_glossify/tippy';
}

/**
 * Creates the CSS to style the tooltip background  and arrow.
 *
 * @param string $color
 *   Color for tippy popup and arrow.
 *
 * @return string
 *   Inline CSS.
 */
function melt_glossify_create_tippyjs_inline_css($color) {
  $tippyCss = "
    .tippy-tooltip {
      background-color: $color;
    }
    .tippy-popper[x-placement^='top'] .tippy-arrow {
      border-top-color: $color;
    }
    .tippy-popper[x-placement^='bottom'] .tippy-arrow {
      border-bottom-color: $color;
    }
    .tippy-popper[x-placement^='left'] .tippy-arrow {
      border-left-color: $color;
    }
    .tippy-popper[x-placement^='right'] .tippy-arrow {
      border-right-color: $color;
    }
    .tippy-tooltip .tippy-roundarrow {
      fill: $color;
    }
  ";

  return $tippyCss;
}