galleryslider-8.x-3.1/galleryslider.module
galleryslider.module
<?php
/**
* @file
* Contains galleryslider.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*
* @param $route_name
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
*
* @return string
*/
function galleryslider_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the simple module.
case 'help.page.galleryslider':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('simple Carousel: Download code at drupal.org') . '</p>';
$output .= '<p>' . t('Video tutorial will be uploaded soon') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function galleryslider_theme() {
return [
'galleryslider' => [
'variables' => ['items' => NULL, 'settings' => NULL],
],
'galleryslider_views' => [
'variables' => [],
],
];
}
/**
* Themeable for galleryslider.
*
* @param $variables
*/
function template_preprocess_galleryslider(&$variables) {
$items = $variables['items'];
$settings = _galleryslider_format_settings($variables['settings']);
$variables['attributes']['class'][] = 'slider';
$variables['attributes']['class'][] = '';
$variables['attributes']['data-settings'] = json_encode($settings);
$html = '';
if (!empty($items)) {
$i = 0;
$html .= '<ul data-title="Row">';
foreach ($items as $item) {
$html .= '<li ' . $i . '>' . render($item) . '</li>';
$i++;
}
}
$html .= '</ul>';
$output = [
'#type' => 'markup',
'#markup' => $html,
];
$variables['output'] = render($output);
}
/**
* Prepares variables for Views galleryslider carousel templates.
*
* Default template: galleryslider-views.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A View object.
*/
function template_preprocess_galleryslider_views(array &$variables) {
$handler = $variables['view']->style_plugin;
$settings = _galleryslider_format_settings($handler->options);
$variables['attributes']['class'][] = 'slider ';
$variables['attributes']['class'][] = '';
$variables['attributes']['data-settings'] = json_encode($settings);
template_preprocess_views_view_unformatted($variables);
}
/**
* Settings to display view at front page.
*
* @param $variables
*/
function galleryslider_preprocess_page(&$variables) {
$node = \Drupal::routeMatch()->getParameter('node');
$frontpage = \Drupal::service('path.matcher')->isFrontPage();
if ($node || $frontpage) {
$variables['#attached']['library'][] = 'galleryslider/galleryslider';
}
}
/**
* Default settings for simple.
*
* @param null $key
*
* @return array|mixed
*/
function _galleryslider_default_settings($key = NULL) {
$settings = [
'menu' => 'grid',
'imgGrid' => '',
'speed' => 300,
'image_style' => '',
'image_link' => '',
];
return isset($settings[$key]) ? $settings[$key] : $settings;
}
/**
* Return formatted js array of settings.
*
* @param $settings
*
* @return mixed
*/
function _galleryslider_format_settings($settings) {
$settings['speed'] = (int) $settings['speed'];
if (isset($settings['image_style'])) {
unset($settings['image_style']);
}
if (isset($settings['image_link'])) {
unset($settings['image_link']);
}
return $settings;
}
/**
* Convert a string of settings to array.
*
* @param string $str
* String to be converted to array.
*
* @return array
* Converted array.
*/
function _galleryslider_string_to_array($str) {
$str = trim($str);
$str = str_replace('[', '', $str);
$str = str_replace(']', '', $str);
$str = explode(',', $str);
return $str;
}
