You are here

function flexslider_add in Flex Slider 8.2

Same name and namespace in other branches
  1. 7.2 flexslider.module \flexslider_add()
  2. 7 flexslider.module \flexslider_add()

Returns array of required JavaScripts and settings for a flexslider instance.

Parameters

string $id: optional ID Attribute for FlexSlider container.

object|string $optionset: optional Option set to load or the machine name of an existing optionset.

Return value

array Array of asset attachments.

3 calls to flexslider_add()
flexslider.api.php in ./flexslider.api.php
API documentation for FlexSlider.
flexslider_library_test_page_attachments_alter in tests/modules/flexslider_library_test/flexslider_library_test.module
Implements hook_page_attachments_alter().
template_preprocess_flexslider in templates/flexslider.theme.inc
Prepares variables for flexslider template.

File

./flexslider.module, line 228
A light-weight, customizable image gallery plugin for Drupal based on jQuery.

Code

function flexslider_add($id = NULL, $optionset = NULL) {
  $attached = [];

  // Check optionset value.
  if (is_string($optionset)) {
    $name = $optionset;
    $optionset = Flexslider::load($name);
    if (empty($optionset)) {
      \Drupal::logger('flexslider')
        ->warning('Invalid optionset name supplied to flexslider_add: @name', [
        '@name' => $name,
      ]);
      return $attached;
    }
  }

  // Static array to remember which scripts are already attached.
  // @todo not currently in use
  $cache =& drupal_static(__FUNCTION__, []);
  $attached['library'][] = 'flexslider/integration';

  // If the ID or optionset aren't set, it is assumed the settings will be set
  // manually via the calling module/theme.
  if (!empty($optionset) && !in_array($optionset
    ->id(), $cache)) {
    $cache[] = $optionset
      ->id();
    $attached['drupalSettings']['flexslider']['optionsets'] = [
      $optionset
        ->id() => $optionset
        ->getOptions(TRUE),
    ];
  }
  if (!empty($id)) {

    // JavaScript settings.
    $attached['drupalSettings']['flexslider']['instances'] = [
      $id => $optionset ? $optionset
        ->id() : '-1',
    ];

    // Create an empty optionset setting if none given.
    if (empty($optionset)) {
      $attached['drupalSettings']['flexslider']['optionsets'] = [
        '-1' => [],
      ];
    }
  }
  return $attached;
}