Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a dedicated service to formataddonsnames #5

Open
wants to merge 4 commits into
base: 8.x-4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 6 additions & 39 deletions modules/views_slideshow_cycle/views_slideshow_cycle.theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,11 @@ function template_preprocess_views_slideshow_cycle_main_frame(&$vars) {
}

// Load the json2 library.
// @todo Check if there is a better way to detect optional libraries.
$json2 = \Drupal::service('library.discovery')->getLibraryByName('views_slideshow_cycle', 'json2');
if (isset($json2['js'][0]['data']) && file_exists($json2['js'][0]['data'])) {
$vars['#attached']['library'][] = 'views_slideshow_cycle/json2';
}
$vars['#attached']['library'][] = 'views_slideshow_cycle/json2';

// Load the pause library
// @todo Check if there is a better way to detect optional libraries.
// Load the pause library.
if (!empty($settings['pause_in_middle'])) {
$pause = \Drupal::service('library.discovery')->getLibraryByName('views_slideshow_cycle', 'jquery_pause');
if (isset($pause['js'][0]['data']) && file_exists($pause['js'][0]['data'])) {
$vars['#attached']['library'][] = 'views_slideshow_cycle/jquery_pause';
}
$vars['#attached']['library'][] = 'views_slideshow_cycle/jquery_pause';
}

// Load jQuery Cycle library.
Expand All @@ -95,46 +87,21 @@ function template_preprocess_views_slideshow_cycle_main_frame(&$vars) {
$processedCycles['#views_slideshow_cycle_main_' . $vss_id] = '#views_slideshow_cycle_main_' . $vss_id;

// Add hover intent library.
// @todo Check if there is a better way to detect optional libraries.
if ($settings['pause']) {
$hoverIntent = \Drupal::service('library.discovery')->getLibraryByName('views_slideshow', 'jquery_hoverIntent');
if (isset($hoverIntent['js'][0]['data']) && file_exists($hoverIntent['js'][0]['data'])) {
$vars['#attached']['library'][] = 'views_slideshow/jquery_hoverIntent';
}
$vars['#attached']['library'][] = 'views_slideshow/jquery_hoverIntent';
}

// Add the slideshow elements.
$vars['attributes']['class'][] = 'views_slideshow_cycle_teaser_section';

$styles = '';
if (isset($view->display_handler->display->display_options['style_options']['views_slideshow_cycle'])) {
$styles = $view->display_handler->display->display_options['style_options']['views_slideshow_cycle'];
}

$styles_default = '';
if (isset($view->display['default']->display_options['style_options']['views_slideshow_cycle'])) {
$styles_default = $view->display['default']->display_options['style_options']['views_slideshow_cycle'];
}

// Retrieve the number of items per frame.
if (isset($styles['items_per_slide']) && $styles['items_per_slide'] > 0) {
$items_per_slide = $styles['items_per_slide'];
}
elseif (isset($styles_default['items_per_slide']) && $styles_default['items_per_slide'] > 0) {
$items_per_slide = $styles_default['items_per_slide'];
}
else {
$items_per_slide = 1;
}

$vars['items_per_slide'] = $items_per_slide;
$vars['items_per_slide'] = $settings['items_per_slide'];

$items = array();
$index = 0;
$vars['rendered_rows'] = '';
foreach ($rows as $count => $item) {
$items[] = $item;
if (count($items) == $items_per_slide || $count == (count($rows) - 1)) {
if (count($items) == $settings['items_per_slide'] || $count == (count($rows) - 1)) {
$vars['rendered_rows'][] = array(
'#theme' => $vars['view']->buildThemeFunctions('views_slideshow_cycle_main_frame_row'),
'#vss_id' => $vss_id,
Expand Down
27 changes: 27 additions & 0 deletions src/FormatAddonsName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @file
* Contains \Drupal\views_slideshow\FormatAddonsName.
*/

namespace Drupal\views_slideshow;


/**
* Provides a class to manipulate addons names.
*/
class FormatAddonsName implements FormatAddonsNameInterface {

/**
* Format callback to move from underscore separated words to camelCase.
*/
public function format($subject) {
return preg_replace_callback('/_(.?)/', function($matches) {
if (isset($matches[1])) {
return strtoupper($matches[1]);
}
}, $subject);
}

}
20 changes: 20 additions & 0 deletions src/FormatAddonsNameInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* @file
* Contains \Drupal\views_slideshow\FormatAddonsNameInterface.
*/

namespace Drupal\views_slideshow;


/**
* Provides a class for CRUD operations on path aliases.
*/
interface FormatAddonsNameInterface {

/**
* Format callback manipulate addons names.
*/
public function format($subject);
}
11 changes: 0 additions & 11 deletions src/Plugin/views/style/Slideshow.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,4 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) {
}
}

/**
* Format callback to move from underscore separated words to camelCase.
*/
public function formatAddonName($subject) {
return preg_replace_callback('/_(.?)/', function($matches) {
if (isset($matches[1])) {
return strtoupper($matches[1]);
}
}, $subject);
}

}
5 changes: 4 additions & 1 deletion views_slideshow.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ services:

plugin.manager.views_slideshow.widget:
class: Drupal\views_slideshow\ViewsSlideshowWidgetPluginManager
parent: default_plugin_manager
parent: default_plugin_manager

views_slideshow.format_addons_name:
class: Drupal\views_slideshow\FormatAddonsName
17 changes: 7 additions & 10 deletions views_slideshow.theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function _views_slideshow_preprocess_views_view_slideshow(&$vars) {
$view = $vars['view'];
$rows = $vars['rows'];
$num_divs = count($rows);
// @todo: This #name element is not available of Views edit page.
$vss_id = $view->element['#name'] . '-' . $view->current_display;

// Give each slideshow a unique id if there are more than one on the page.
Expand Down Expand Up @@ -86,10 +87,10 @@ function _views_slideshow_preprocess_views_view_slideshow(&$vars) {
foreach ($addons as $addon_id => $addon_info) {
foreach ($addon_info['accepts'] as $imp_key => $imp_value) {
if (is_array($imp_value)) {
$methods[$imp_key][] = \Drupal::service('plugin.manager.views_slideshow.slideshow_type')->formatAddonName($addon_id);
$methods[$imp_key][] = \Drupal::service('views_slideshow.format_addons_name')->format($addon_id);
}
else {
$methods[$imp_value][] = \Drupal::service('plugin.manager.views_slideshow.slideshow_type')->formatAddonName($addon_id);
$methods[$imp_value][] = \Drupal::service('views_slideshow.format_addons_name')->format($addon_id);
}
}
}
Expand Down Expand Up @@ -183,7 +184,7 @@ function template_preprocess_views_slideshow_pager_widget_render(&$vars) {
$vars['#attached']['library'][] = 'views_slideshow/widget_info';
$vars['#attached']['drupalSettings']['viewsSlideshowPager'][$vars['vss_id']] = array(
$vars['location'] => array(
'type' => \Drupal::service('plugin.manager.views_slideshow.slideshow_type')->formatAddonName($vars['settings']['type']),
'type' => \Drupal::service('views_slideshow.format_addons_name')->format($vars['settings']['type']),
),
);

Expand Down Expand Up @@ -216,12 +217,8 @@ function template_preprocess_views_slideshow_pager_fields(&$vars) {
);

// Add hover intent library.
// @todo Check if there is a better way to detect optional libraries.
if ($vars['settings']['views_slideshow_pager_fields']['views_slideshow_pager_fields_hover']) {
$hoverIntent = \Drupal::service('library.discovery')->getLibraryByName('views_slideshow', 'jquery_hoverIntent');
if (isset($hoverIntent['js'][0]['data']) && file_exists($hoverIntent['js'][0]['data'])) {
$vars['#attached']['library'][] = 'views_slideshow/jquery_hoverIntent';
}
$vars['#attached']['library'][] = 'views_slideshow/jquery_hoverIntent';
}

$vars['widget_id'] = $vars['attributes']['id'];
Expand All @@ -231,7 +228,7 @@ function template_preprocess_views_slideshow_pager_fields(&$vars) {
// Render all the fields unless there is only 1 slide and the user specified
// to hide them when there is only one slide.
$vars['rendered_field_items'] = '';
if (empty($vars['settings']['hide_on_single_slide']) || count($vars['view']->result) > $vars['view']->style_options['views_slideshow_cycle']['items_per_slide']) {
if (empty($vars['settings']['hide_on_single_slide']) || count($vars['view']->result) > $vars['view']->style_plugin->options['views_slideshow_cycle']['items_per_slide']) {
foreach ($vars['view']->result as $count => $node) {
$rendered_fields = '';
foreach ($vars['settings']['views_slideshow_pager_fields']['views_slideshow_pager_fields_fields'] as $field => $use) {
Expand Down Expand Up @@ -284,7 +281,7 @@ function template_preprocess_views_slideshow_controls_widget_render(&$vars) {
$vars['#attached']['library'][] = 'views_slideshow/widget_info';
$vars['#attached']['drupalSettings']['viewsSlideshowControls'][$vars['vss_id']] = array(
$vars['location'] => array(
'type' => \Drupal::service('plugin.manager.views_slideshow.slideshow_type')->formatAddonName($vars['settings']['type']),
'type' => \Drupal::service('views_slideshow.format_addons_name')->format($vars['settings']['type']),
),
);

Expand Down