You are here

public function ViewsAccordion::buildOptionsForm in Views Accordion 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/views/style/ViewsAccordion.php \Drupal\views_accordion\Plugin\views\style\ViewsAccordion::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

src/Plugin/views/style/ViewsAccordion.php, line 55

Class

ViewsAccordion
Style plugin to render each item in an ordered or unordered list.

Namespace

Drupal\views_accordion\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);

  // Find out how many items the display is currently configured to show
  // (row-start-open).
  $maxitems = $this->displayHandler
    ->getOption('items_per_page');

  // If items_per_page is set to unlimitted (0), 10 rows will be what the user
  // gets to choose from.
  $maxitems = $maxitems == 0 ? 10 : $maxitems;

  // Setup our array of options for choosing which row should start opened
  // (row-start-open).
  $rsopen_options = [];
  for ($i = 1; $i <= $maxitems; $i++) {
    $rsopen_options[] = $this
      ->t('Row @number', [
      '@number' => $i,
    ]);
  }
  $rsopen_options['none'] = $this
    ->t('None');
  $rsopen_options['random'] = $this
    ->t('Random');

  /*
   * See /core/core.libraries.yml and http://api.jqueryui.com/1.10/easings/
   * We've used all the easing effects form ui effects core.
   */
  $animated_options = [
    'none' => $this
      ->t('None'),
    'linear' => $this
      ->t('Linear'),
    'swing' => $this
      ->t('Swing'),
    'easeInQuart' => $this
      ->t('easeInQuart'),
    'easeOutQuart' => $this
      ->t('easeOutQuart'),
    'easeInOutQuart' => $this
      ->t('easeInOutQuart'),
    'easeInExpo' => $this
      ->t('easeInExpo'),
    'easeOutExpo' => $this
      ->t('easeOutExpo'),
    'easeInOutExpo' => $this
      ->t('easeInOutExpo'),
    'easeInBack' => $this
      ->t('easeInBack'),
    'easeOutBack' => $this
      ->t('easeOutBack'),
    'easeInOutBack' => $this
      ->t('easeInOutBack'),
    'easeInQuad' => $this
      ->t('easeInQuad'),
    'easeOutQuad' => $this
      ->t('easeOutQuad'),
    'easeInOutQuad' => $this
      ->t('easeInOutQuad'),
    'easeInQuint' => $this
      ->t('easeInQuint'),
    'easeOutQuint' => $this
      ->t('easeOutQuint'),
    'easeInOutQuint' => $this
      ->t('easeInOutQuint'),
    'easeInCirc' => $this
      ->t('easeInCirc'),
    'easeOutCirc' => $this
      ->t('easeOutCirc'),
    'easeInOutCirc' => $this
      ->t('easeInOutCirc'),
    'easeInBounce' => $this
      ->t('easeInBounce'),
    'easeOutBounce' => $this
      ->t('easeOutBounce'),
    'easeInOutBounce' => $this
      ->t('easeInOutBounce'),
    'easeInCubic' => $this
      ->t('easeInCubic'),
    'easeOutCubic' => $this
      ->t('easeOutCubic'),
    'easeInOutCubic' => $this
      ->t('easeInOutCubic'),
    'easeInSine' => $this
      ->t('easeInSine'),
    'easeOutSine' => $this
      ->t('easeOutSine'),
    'easeInOutSine' => $this
      ->t('easeInOutSine'),
    'easeInElastic' => $this
      ->t('easeInElastic'),
    'easeOutElastic' => $this
      ->t('easeOutElastic'),
    'easeInOutElastic' => $this
      ->t('easeInOutElastic'),
  ];
  $c = count($this->options['grouping']);

  // Add a form use group header field for every grouping, plus one.
  for ($i = 0; $i <= $c; $i++) {
    $grouping = !empty($this->options['grouping'][$i]) ? $this->options['grouping'][$i] : [];
    $grouping += [
      'use-grouping-header' => 0,
    ];
    $form['grouping'][$i]['use-grouping-header'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use the group header as the Accordion header'),
      '#default_value' => $grouping['use-grouping-header'],
      '#description' => $this
        ->t("If checked, the Group's header will be used to open/close the accordion."),
      '#states' => [
        'invisible' => [
          ':input[name="style_options[grouping][' . $i . '][field]"]' => [
            'value' => '',
          ],
        ],
      ],
    ];
  }
  $form['grouping']['#prefix'] = '<div class="form-item">' . $this
    ->t('<strong>IMPORTANT:</strong> The <em>first field</em> in order of appearance <em>will</em> be the one used as the "header" or "trigger" of the accordion action.') . '</div>';
  $form['disableifone'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Disable if only one result'),
    '#default_value' => $this->options['disableifone'],
    '#description' => $this
      ->t("If set, the accordion will not be shown when there are less than 2 results."),
  ];
  $form['row-start-open'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Row to display opened on start'),
    '#default_value' => $this->options['row-start-open'],
    '#description' => $this
      ->t('Choose which row should start opened when the accordion first loads. If you want all to start closed, choose "None", and make sure to have "Allow for all rows to be closed" on below.'),
    '#options' => $rsopen_options,
  ];
  $form['collapsible'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Collapsible'),
    '#default_value' => $this->options['collapsible'],
    '#description' => $this
      ->t('Whether all the sections can be closed at once. Allows collapsing the active section.'),
  ];
  $form['animated'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Animation effect'),
    '#default_value' => $this->options['animated'],
    '#description' => $this
      ->t('Choose what animation effect you would like to see, or "None" to disable it.'),
    '#options' => $animated_options,
  ];
  $form['animation_time'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Animation time'),
    '#default_value' => $this->options['animation_time'],
    '#min' => 0,
    '#step' => 1,
    '#description' => $this
      ->t('The animation duration in milliseconds'),
  ];
  $form['heightStyle'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('heightStyle'),
    '#default_value' => $this->options['heightStyle'],
    '#description' => $this
      ->t('Controls the height of the accordion and each panel.'),
    '#options' => [
      'auto' => 'auto',
      'fill' => 'fill',
      'content' => 'content',
    ],
  ];
  $form['event'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Event'),
    '#default_value' => $this->options['event'],
    '#description' => $this
      ->t('The event on which to trigger the accordion.'),
    '#options' => [
      'click' => $this
        ->t('Click'),
      'mouseover' => $this
        ->t('Mouseover'),
    ],
  ];
  $form['use_header_icons'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use header icons'),
    '#default_value' => $this->options['use_header_icons'],
    '#description' => $this
      ->t('Icons to use for headers, matching an icon provided by the <a href="http://api.jqueryui.com/theming/icons/" target="_false">jQuery UI CSS Framework</a>. Uncheck to have no icons displayed.'),
  ];
  $show_if_use_header_icons = [
    'visible' => [
      ':input[name="style_options[use_header_icons]"]' => [
        'checked' => TRUE,
      ],
    ],
  ];
  $form['icon_header'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Closed row header icon'),
    '#default_value' => $this->options['icon_header'],
    '#states' => $show_if_use_header_icons,
  ];
  $form['icon_active_header'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Opened row header icon'),
    '#default_value' => $this->options['icon_active_header'],
    '#states' => $show_if_use_header_icons,
  ];
}