You are here

public function SliderPro::buildOptionsForm in Slider Pro 8

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

src/Plugin/views/style/SliderPro.php, line 96

Class

SliderPro
Plugin annotation @ViewsStyle( id = "slider_pro", title = @Translation("Slider Pro"), help = @Translation("Displays a view as a Slider Pro, using the Slider Pro jQuery plugin."), theme = "slider_pro_views_style", theme_file =…

Namespace

Drupal\slider_pro\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $fields = $this
    ->getAvailableFields();
  if (empty($fields)) {
    drupal_set_message($this
      ->t('To configure Slider Pro you have to add at least one field'), 'error');
    return $form;
  }
  $form['general'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('General settings'),
    '#open' => TRUE,
  ];
  $form['general']['optionset'] = [
    '#title' => t('Option set'),
    '#type' => 'select',
    '#options' => $this->sliderProManager
      ->getOptionList(),
    '#default_value' => $this->options['optionset'],
    '#required' => TRUE,
  ];
  $form['general']['fields'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Fields on slide'),
    '#options' => $this
      ->getAvailableFields(),
    '#description' => $this
      ->t('Select which fields you want to use on each slide.'),
    '#default_value' => $this->options['fields'],
    '#required' => TRUE,
  ];
  $form['thumbnails'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Thumbnail settings'),
    '#open' => FALSE,
  ];
  $form['thumbnails']['thumbnail_fields'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Fields'),
    '#options' => $this
      ->getAvailableFields(),
    '#description' => $this
      ->t('Select which fields you want to display on the thumbs. This setting will only be applied if the selected optionset defines a thumbnail position'),
    '#default_value' => $this->options['thumbnail_fields'],
  ];
  $form['layers_wrapper'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Layer settings'),
    '#open' => !empty($this->options['number_of_layers']) ? TRUE : FALSE,
  ];
  $form['layers_wrapper']['number_of_layers'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Number of layers'),
    '#options' => array_combine([
      0,
      1,
      2,
      3,
      4,
      5,
    ], [
      $this
        ->t('None'),
      1,
      2,
      3,
      4,
      5,
    ]),
    '#default_value' => $this->options['number_of_layers'],
    '#description' => $this
      ->t('Provide the number of layers you want to use. Afterwards save the options and re-open them to start configuring your layers.'),
  ];
  $form['layers_wrapper']['layers'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Fields'),
      $this
        ->t('Background'),
      $this
        ->t('Position'),
      $this
        ->t('Show transition'),
      $this
        ->t('Hide transition'),
      $this
        ->t('Delay'),
      $this
        ->t('Duration'),
      $this
        ->t('Weight'),
    ],
    '#attributes' => [
      'id' => 'slider-pro-layers',
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'weight',
      ],
    ],
    '#empty' => $this
      ->t('No layers configured yet...'),
  ];
  for ($key = 0; $key < $this->options['number_of_layers']; $key++) {
    $layer = isset($this->options['layers'][$key]) ? $this->options['layers'][$key] : [
      'fields' => [],
      'background' => '',
      'position' => 'topLeft',
      'show_transition' => 'left',
      'hide_transition' => 'right',
      'show_delay' => 0,
      'stay_duration' => 0,
      'weight' => 0,
    ];
    $form['layers_wrapper']['layers'][$key]['#attributes']['class'][] = 'draggable';
    $form['layers_wrapper']['layers'][$key]['#weight'] = $layer['weight'];
    $form['layers_wrapper']['layers'][$key]['fields'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Fields'),
      '#title_display' => 'invisible',
      '#options' => $this
        ->getAvailableFields(),
      '#default_value' => $layer['fields'],
      '#description' => $this
        ->t('Select which fields you want to display on this layer.'),
      '#required' => TRUE,
    ];
    $form['layers_wrapper']['layers'][$key]['background'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Background'),
      '#title_display' => 'invisible',
      '#options' => [
        '' => $this
          ->t('None'),
        'sp-white' => $this
          ->t('White transparant'),
        'sp-black' => $this
          ->t('Black transparant'),
      ],
      '#default_value' => $layer['background'],
    ];
    $form['layers_wrapper']['layers'][$key]['position'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Position'),
      '#title_display' => 'invisible',
      '#options' => [
        'topLeft' => $this
          ->t('Top left'),
        'topCenter' => $this
          ->t('Top center'),
        'topRight' => $this
          ->t('Top right'),
        'bottomLeft' => $this
          ->t('Bottom left'),
        'bottomCenter' => $this
          ->t('Bottom center'),
        'bottomRight' => $this
          ->t('Bottom right'),
        'centerLeft' => $this
          ->t('Center left'),
        'centerRight' => $this
          ->t('Center right'),
        'centerCenter' => $this
          ->t('Center center'),
      ],
      '#default_value' => $layer['position'],
      '#required' => TRUE,
    ];
    $form['layers_wrapper']['layers'][$key]['show_transition'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Show transition'),
      '#title_display' => 'invisible',
      '#options' => [
        'left' => $this
          ->t('Left'),
        'right' => $this
          ->t('Right'),
        'up' => $this
          ->t('Up'),
        'down' => $this
          ->t('Down'),
      ],
      '#default_value' => $layer['show_transition'],
      '#required' => TRUE,
    ];
    $form['layers_wrapper']['layers'][$key]['hide_transition'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Show transition'),
      '#title_display' => 'invisible',
      '#options' => [
        'left' => $this
          ->t('Left'),
        'right' => $this
          ->t('Right'),
        'up' => $this
          ->t('Up'),
        'down' => $this
          ->t('Down'),
      ],
      '#default_value' => $layer['hide_transition'],
      '#required' => TRUE,
    ];
    $form['layers_wrapper']['layers'][$key]['show_delay'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Show delay'),
      '#title_display' => 'invisible',
      '#size' => 5,
      '#required' => TRUE,
      '#default_value' => $layer['show_delay'],
      '#description' => $this
        ->t('Sets a delay for the show transition. This delay starts from the moment when the transition to the new slide starts.'),
    ];
    $form['layers_wrapper']['layers'][$key]['stay_duration'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Stay duration'),
      '#title_display' => 'invisible',
      '#size' => 5,
      '#required' => TRUE,
      '#default_value' => $layer['stay_duration'],
      '#description' => $this
        ->t('Sets how much time a layer will stay visible before being hidden automatically.'),
    ];

    // @todo: use this button if figured out how ajax calls in this form work.

    /*$form['layers_wrapper']['layers'][$key]['remove'] = [
        '#type' => 'submit',
        '#value' => $this->t('Remove'),
        '#submit' => [[$this, 'removeLayerSubmit']],
        '#ajax' => [
          'callback' => [
            'Drupal\slider_pro\Plugin\views\style\SliderPro',
            'ajaxRefreshLayers'
          ],
        ],
        '#limit_validation_errors' => [],
      ];*/
    $form['layers_wrapper']['layers'][$key]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight'),
      '#title_display' => 'invisible',
      '#default_value' => $layer['weight'],
      '#attributes' => [
        'class' => [
          'weight',
        ],
      ],
    ];
  }

  //Sort by weight.
  uasort($form['layers_wrapper']['layers'], [
    '\\Drupal\\Component\\Utility\\SortArray',
    'sortByWeightProperty',
  ]);

  // @todo: use this button if figured out how ajax calls in this form work.

  /*$form['layers_wrapper']['add_layer'] = [
      '#type' => 'submit',
      '#value' => $this->t('Add layer'),
      '#submit' => [[$this, 'addNewLayerSubmit']],
      '#ajax' => [
        'callback' => [
          'Drupal\slider_pro\Plugin\views\style\SliderPro',
          'ajaxRefreshLayers'
        ],
      ],
      '#limit_validation_errors' => [],
    ];*/
}