You are here

function slick_attach in Slick Carousel 7.2

Same name and namespace in other branches
  1. 7.3 slick.module \slick_attach()

Returns the fully cached JS and CSS assets for the given slick.

Parameters

array $attach: An array of conditions to load the relevant assets, empty means basic.

array $settings: An array of settings to check for the supported features.

Return value

array The cacheable array formatted for the '#attached' property.

4 calls to slick_attach()
my_module_render_slick_detail in ./slick.api.php
Detailed sample #2.
my_module_render_slick_theme in ./slick.api.php
Using slick_attach() to a custom theme or renderable array.
slick_build in ./slick.module
Returns a cacheable renderable array of a single slick instance.
slick_fields_field_formatter_view in slick_fields/slick_fields.module
Implements hook_field_formatter_view().

File

./slick.module, line 287
Slick carousel integration, the last carousel you'll ever need.

Code

function slick_attach(array $attach, array $settings = array()) {
  $path = drupal_get_path('module', 'slick');
  $main = slick_get_skins_by_group('main');
  $load = array();
  $attach += array(
    'attach_slick_css' => variable_get('slick_css', TRUE),
    'attach_module_css' => variable_get('slick_module_css', TRUE),
  );
  $load['css'] = $load['js'] = array();
  if ($easing = libraries_get_path('easing')) {
    $load['js'] += array(
      $easing . '/jquery.easing.min.js' => array(
        'group' => JS_LIBRARY,
        'weight' => -6,
      ),
    );
  }
  if (!empty($settings['mousewheel'])) {
    if ($mousewheel = libraries_get_path('mousewheel')) {
      $load['js'] += array(
        $mousewheel . '/jquery.mousewheel.min.js' => array(
          'group' => JS_LIBRARY,
          'weight' => -5,
        ),
      );
    }
  }
  $load['library'][] = array(
    'slick',
    'slick',
  );
  $load['js'] += array(
    $path . '/js/slick.load.min.js' => array(
      'weight' => 0,
    ),
  );
  if (!empty($settings['media_switch'])) {
    $switch = str_replace('-switch', '', $settings['media_switch']);
    $switch = $switch == 'iframe' ? 'media' : $switch;
    foreach (array(
      'colorbox',
      'photobox',
      'media',
    ) as $component) {
      if ($switch == $component) {
        $load['library'][] = array(
          'slick',
          'slick.' . $component,
        );
      }
    }
  }
  if (isset($settings['skin']) && ($skin = $settings['skin'])) {
    if (isset($main[$skin]['css'])) {
      if ($attach['attach_slick_css']) {
        $load['css'] += array(
          libraries_get_path('slick') . '/slick/slick-theme.css' => array(
            'weight' => -100,
          ),
        );
      }
      if ($attach['attach_module_css']) {
        $load['css'] += array(
          $path . '/css/theme/slick.theme.css' => array(
            'weight' => -99,
          ),
        );
      }
      if (is_array($main[$skin]['css'])) {
        $load['css'] += $main[$skin]['css'];
      }
    }
    if (isset($main[$skin]['js']) && is_array($main[$skin]['js'])) {
      $load['js'] += $main[$skin]['js'];
    }
    $navs = array(
      'thumbnail',
      'arrows',
      'dots',
    );
    foreach ($navs as $nav) {
      if (isset($settings['skin_' . $nav]) && ($skin = $settings['skin_' . $nav])) {
        $nav_skins = slick_get_skins_by_group($nav);
        if (!empty($nav_skins) && isset($nav_skins[$skin]['css']) && is_array($nav_skins[$skin]['css'])) {
          $load['css'] += $nav_skins[$skin]['css'];
        }
        unset($nav_skins);
      }
    }

    // @todo drop backward compatibility.
    if (!empty($settings['thumbnail_hover'])) {
      $settings['thumbnail_effect'] = 'hover';
    }
    if (!empty($settings['thumbnail_effect'])) {
      $load['css'] += array(
        $path . '/css/components/slick.thumbnail--' . $settings['thumbnail_effect'] . '.css' => array(),
      );
    }
  }

  // Attach default JS settings to allow responsive displays have a lookup,
  // excluding wasted+/trouble options, e.g.: PHP string vs JS object.
  $defaults = slick_get_options();
  $excludes = explode(' ', 'mobileFirst appendDots asNavFor prevArrow nextArrow respondTo');
  $js_settings = array_diff_key($defaults, drupal_map_assoc($excludes));
  $load['js'][] = array(
    'data' => array(
      'slick' => $js_settings,
    ),
    'type' => 'setting',
  );
  drupal_alter('slick_attach_load_info', $load, $attach, $main, $settings);
  return $load;
}