You are here

public function JCarouselPager::buildConfigurationForm in Views Slideshow JCarousel 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides ViewsSlideshowWidgetBase::buildConfigurationForm

File

src/Plugin/ViewsSlideshowWidget/JCarouselPager.php, line 43

Class

JCarouselPager
Provides a pager using fields.

Namespace

Drupal\views_slideshow_jcarousel\Plugin\ViewsSlideshowWidget

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

  // Settings for fields pager.
  $options = [];

  // Get each field and it's name.
  foreach ($this
    ->getConfiguration()['view']->display_handler
    ->getHandlers('field') as $field_name => $field) {
    $options[$field_name] = $field
      ->adminLabel();
  }

  // Need to wrap this so it indents correctly.
  $form['views_slideshow_jcarousel_pager_fields_wrapper'] = [
    '#markup' => '<div class="vs-dependent">',
  ];
  $jcarousel = \Drupal::service('library.discovery')
    ->getLibraryByName('views_slideshow_jcarousel', 'jcarousel.js');
  if (!isset($jcarousel['js'][0]['data']) || !file_exists($jcarousel['js'][0]['data'])) {
    $form['views_slideshow_jcarousel_pager']['no_carousel_js'] = [
      '#type' => 'markup',
      '#prefix' => '<div style="color: red">',
      '#markup' => $this
        ->t('The jCarousel library was not found. Please extract the jCarousel package to libraries/jcarousel in Drupal root folder, so that the javascript files are located at libraries/jcarousel/lib. You can download the jCarousel package at @url', [
        '@url' => Link::fromTextAndUrl('http://sorgalla.com/jcarousel/', Url::FromUri('http://sorgalla.com/jcarousel/', [
          'attributes' => [
            'target' => '_blank',
          ],
        ]))
          ->toString(),
      ]),
      '#suffix' => '</div>',
      '#states' => [
        'visible' => [
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[type]"]' => [
            'value' => 'views_slideshow_jcarousel_pager',
          ],
        ],
      ],
    ];
  }
  else {

    // Add ability to choose which fields to show in the pager.
    $form['views_slideshow_jcarousel_pager_fields'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Pager fields'),
      '#options' => $options,
      '#default_value' => $this
        ->getConfiguration()['views_slideshow_jcarousel_pager_fields'],
      '#description' => $this
        ->t("Choose the fields that will appear in the pager."),
      '#prefix' => '<div id="' . $this
        ->getConfiguration()['dependency'] . '-views-slideshow-jcarousel-pager-fields-wrapper">',
      '#suffix' => '</div>',
      '#states' => [
        'visible' => [
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[type]"]' => [
            'value' => 'views_slideshow_jcarousel_pager',
          ],
        ],
      ],
    ];

    // Add field to see if they would like the carousel to activate the pager item
    // on slide change.
    $form['views_slideshow_jcarousel_pager_move_on_change'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Move To Active Pager Item On Slide Change'),
      '#default_value' => $this
        ->getConfiguration()['views_slideshow_jcarousel_pager_move_on_change'],
      '#description' => $this
        ->t('When the slide changes move the carousel to the active pager item.'),
      '#prefix' => '<div id="' . $this
        ->getConfiguration()['dependency'] . '-views-slideshow-jcarousel-pager-move-on-change">',
      '#suffix' => '</div>',
      '#states' => [
        'visible' => [
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[type]"]' => [
            'value' => 'views_slideshow_jcarousel_pager',
          ],
        ],
      ],
    ];
    $form['views_slideshow_jcarousel_pager_update_on_prev_next'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Make Previous/Next Buttons Update the Current Slide'),
      '#default_value' => $this
        ->getConfiguration()['views_slideshow_jcarousel_pager_update_on_prev_next'],
      '#description' => $this
        ->t('Cycle the slideshow forward or backwards when the pager previous/next buttons are clicked'),
      '#prefix' => '<div id="' . $this
        ->getConfiguration()['dependency'] . '-views-slideshow-jcarousel-pager-update-on-prev-next">',
      '#suffix' => '</div>',
      '#states' => [
        'visible' => [
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[type]"]' => [
            'value' => 'views_slideshow_jcarousel_pager',
          ],
        ],
      ],
    ];
    $skin_directory = DRUPAL_ROOT . '/libraries/jcarousel/skins';
    $skins = [];
    foreach (scandir($skin_directory) as $dir) {
      if ($dir !== '.' && $dir !== '..' && is_dir($skin_directory . '/' . $dir)) {
        $skins[$dir] = $dir;
      }
    }

    // Set the skin.
    $form['views_slideshow_jcarousel_pager_skin'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('jCarousel Skin'),
      '#options' => $skins,
      '#default_value' => $this
        ->getConfiguration()['views_slideshow_jcarousel_pager_skin'],
      '#description' => $this
        ->t('Choose the skin for your carousel.  You can add more by placing your skin in the jcarousel library directory.'),
      '#prefix' => '<div id="' . $this
        ->getConfiguration()['dependency'] . '-views-slideshow-jcarousel-pager-skin">',
      '#suffix' => '</div>',
      '#states' => [
        'visible' => [
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[type]"]' => [
            'value' => 'views_slideshow_jcarousel_pager',
          ],
        ],
      ],
    ];

    // Set orientation of the pager.
    $form['views_slideshow_jcarousel_pager_orientation'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Orientation of the Pager'),
      '#options' => [
        FALSE => 'Horizontal',
        TRUE => 'Vertical',
      ],
      '#default_value' => $this
        ->getConfiguration()['views_slideshow_jcarousel_pager_orientation'],
      '#description' => $this
        ->t('Should the pager be horizontal or vertical.'),
      '#prefix' => '<div id="' . $this
        ->getConfiguration()['dependency'] . '-views-slideshow-jcarousel-pager-orientation">',
      '#suffix' => '</div>',
      '#states' => [
        'visible' => [
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[type]"]' => [
            'value' => 'views_slideshow_jcarousel_pager',
          ],
        ],
      ],
    ];

    // Set the number of visible items.
    $form['views_slideshow_jcarousel_pager_visible'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Number of Visible Pager Items'),
      '#default_value' => $this
        ->getConfiguration()['views_slideshow_jcarousel_pager_visible'],
      '#description' => $this
        ->t('Set the number of pager items that are visible at a single time.'),
      '#size' => 10,
      '#prefix' => '<div id="' . $this
        ->getConfiguration()['dependency'] . '-views-slideshow-jcarousel-pager-visible">',
      '#suffix' => '</div>',
      '#states' => [
        'visible' => [
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[type]"]' => [
            'value' => 'views_slideshow_jcarousel_pager',
          ],
        ],
      ],
    ];

    // Set the number of items to scroll by.
    $form['views_slideshow_jcarousel_pager_scroll'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Scroll'),
      '#default_value' => $this
        ->getConfiguration()['views_slideshow_jcarousel_pager_scroll'],
      '#description' => $this
        ->t('The number of items to scroll by.'),
      '#size' => 10,
      '#prefix' => '<div id="' . $this
        ->getConfiguration()['dependency'] . '-views-slideshow-jcarousel-pager-scroll">',
      '#suffix' => '</div>',
      '#states' => [
        'visible' => [
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[type]"]' => [
            'value' => 'views_slideshow_jcarousel_pager',
          ],
        ],
      ],
    ];

    // Wrap the carousel
    $form['views_slideshow_jcarousel_pager_wrap'] = [
      '#type' => 'select',
      '#title' => t('Wrapping'),
      '#options' => [
        NULL => 'Disabled',
        'circular' => 'Circular',
        'first' => 'First',
        'last' => 'Last',
        'both' => 'Both',
      ],
      '#default_value' => $this
        ->getConfiguration()['views_slideshow_jcarousel_pager_wrap'],
      '#description' => $this
        ->t('Wrap the carousel.'),
      '#prefix' => '<div id="' . $this
        ->getConfiguration()['dependency'] . '-views-slideshow-jcarousel-pager-wrap">',
      '#suffix' => '</div>',
      '#states' => [
        'visible' => [
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this
            ->getConfiguration()['dependency'] . '[type]"]' => [
            'value' => 'views_slideshow_jcarousel_pager',
          ],
        ],
      ],
    ];
  }
  $form['views_slideshow_jcarousel_pager_fields_wrapper_close'] = [
    '#markup' => '</div>',
  ];
  return $form;
}