revealjs-8.x-1.0-alpha2/revealjs.module
revealjs.module
<?php
/**
* @file
* Revealjs integration for Views.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*
* {@inheritDoc}
*/
function revealjs_help($route_name, RouteMatchInterface $route_match){
switch ($route_name){
//Help for the module
case 'help.page.revealjs':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Revealjs integration for Drupal View.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme_registry_alter().
*
* {@inheritDoc}
*/
function revealjs_theme_registry_alter(&$theme_registry) {
$path = drupal_get_path('module', 'revealjs');
// Duplicate the registry entry for the views_view template.
$theme_registry['revealjs'] = $theme_registry['views_view'];
// Update the entry to indicate that the template is in the module directory, not the theme directory.
$theme_registry['revealjs']['template'] = 'views-view-revealjs';
$theme_registry['revealjs']['path'] = $path . '/templates';
}
/**
* Implements hook_preprocess_HOOK().
*
* {@inheritDoc}
*/
function template_preprocess_views_view_revealjs(&$variables) {
$config = Drupal::config('revealjs.settings');
if ($config->get('html5shiv') === TRUE) {
$variables['attachment_before']['#attached']['library'][] = 'revealjs/html5shiv';
}
// Attach CSS/JS of Reveal.js library.
$variables['attachment_before']['#attached']['library'][] = 'revealjs/revealjs';
$options = $variables['view']->style_plugin->options;
// Attach CSS theme selected by user.
if (!empty($options['theme_slide'])) {
$variables['attachment_before']['#attached']['library'][] = 'revealjs/' . $options['theme_slide'];
}
// Convert string into numbers.
$options['defaultTiming'] = (int) $options['defaultTiming'];
$options['autoSlide'] = (int) $options['autoSlide'];
$options['viewDistance'] = (int) $options['viewDistance'];
$options['margin'] = (float) $options['margin'];
foreach (['height', 'width'] as $option_name) {
if (!array_key_exists($option_name, $options)) {
continue;
}
// Consider 0 as a non-valid value, so ignore if 0 or empty.
if (empty($options[$option_name])) {
unset($options[$option_name]);
}
elseif (FALSE === strpos($options[$option_name],'%')) {
$options[$option_name] = (int) $options[$option_name];
}
}
foreach (['minScale', 'maxScale'] as $option_name) {
if (!array_key_exists($option_name, $options)) {
continue;
}
// Consider 0 as a non-valid value, so ignore if 0 or empty.
if (empty($options[$option_name])) {
unset($options[$option_name]);
}
else {
$options[$option_name] = (float) $options[$option_name];
}
}
unset($options['theme_slide']);
$variables['attachment_before']['#attached']['drupalSettings']['revealConfig'] = revealjs_build_configuration($options);
}
/**
* Build the configuration to be passed to the Revealjs script.
*
* @param array $options
*
* @return array
* A configuration array containing options and dependencies.
*/
function revealjs_build_configuration(array $options = array()) {
$settings = Drupal::config('revealjs.settings')->get();
$options += $settings ?? [];
$library_path = 'libraries/reveal.js';
$dependencies = [
'highlight' => ['/plugin/highlight/highlight.js'],
'markdown' => ['/plugin/markdown/markdown.js'],
'math' => ['/plugin/math/math.js'],
'notes' => ['/plugin/notes/notes.js'],
'search' => ['/plugin/search/search.js'],
'zoom' => ['/plugin/zoom-js/zoom.js'],
];
$configuration['dependencies'] = [];
foreach ($dependencies as $option_name => $required_files) {
if (!empty($options[$option_name])) {
unset($options[$option_name]);
$sources = [];
foreach ($required_files as $relative_path) {
if (file_exists(DRUPAL_ROOT . '/' . $library_path . $relative_path)) {
$sources[] = base_path() . $library_path . $relative_path;
}
else {
continue 2;
}
}
$options['dependencies'][$option_name] = $sources;
}
}
$configuration['options'] = $options;
return $configuration;
}
