Chris@5: ''];
Chris@5: $options['row_class_default'] = ['default' => TRUE];
Chris@5: $options['slideshow_type'] = ['default' => 'views_slideshow_cycle'];
Chris@5: $options['slideshow_skin'] = ['default' => 'default'];
Chris@5:
Chris@5: $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
Chris@5: foreach ($typeManager->getDefinitions() as $id => $definition) {
Chris@5: $instance = $typeManager->createInstance($id, []);
Chris@5: $options[$id] = $instance->defaultConfiguration();
Chris@5: }
Chris@5:
Chris@5: $widgetTypeManager = \Drupal::service('plugin.manager.views_slideshow.widget_type');
Chris@5: $widgetTypes = $widgetTypeManager->getDefinitions();
Chris@5: foreach (['top', 'bottom'] as $location) {
Chris@5: foreach ($widgetTypes as $widgetTypeId => $widgetTypeDefinition) {
Chris@5: $options['widgets']['contains'][$location]['contains'][$widgetTypeId]['contains'] = $widgetTypeManager->createInstance($widgetTypeId, [])->defaultConfiguration();
Chris@5: }
Chris@5: }
Chris@5:
Chris@5: return $options;
Chris@5: }
Chris@5:
Chris@5: /**
Chris@5: * {@inheritdoc}
Chris@5: */
Chris@5: public function buildOptionsForm(&$form, FormStateInterface $form_state) {
Chris@5: parent::buildOptionsForm($form, $form_state);
Chris@5:
Chris@5: // Wrap all the form elements to help style the form.
Chris@5: $form['views_slideshow_wrapper'] = [
Chris@5: '#markup' => '
',
Chris@5: ];
Chris@5:
Chris@5: // Skins.
Chris@5: $form['slideshow_skin_header'] = [
Chris@5: '#markup' => '
' . $this->t('Style') . '
',
Chris@5: ];
Chris@5:
Chris@5: /* @var \Drupal\Component\Plugin\PluginManagerInterface */
Chris@5: $skinManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_skin');
Chris@5:
Chris@5: // Get all skins to create the option list.
Chris@5: $skins = [];
Chris@5: foreach ($skinManager->getDefinitions() as $id => $definition) {
Chris@5: $skins[$id] = $definition['label'];
Chris@5: }
Chris@5: asort($skins);
Chris@5:
Chris@5: // Create the drop down box so users can choose an available skin.
Chris@5: $form['slideshow_skin'] = [
Chris@5: '#type' => 'select',
Chris@5: '#title' => $this->t('Skin'),
Chris@5: '#options' => $skins,
Chris@5: '#default_value' => $this->options['slideshow_skin'],
Chris@5: '#description' => $this->t('Select the skin to use for this display. Skins allow for easily swappable layouts of things like next/prev links and thumbnails. Note that not all skins support all widgets, so a combination of skins and widgets may lead to unpredictable results in layout.'),
Chris@5: ];
Chris@5:
Chris@5: // Slides.
Chris@5: $form['slides_header'] = [
Chris@5: '#markup' => '
' . $this->t('Slides') . '
',
Chris@5: ];
Chris@5:
Chris@5: // Get all slideshow types.
Chris@5: $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
Chris@5: $types = $typeManager->getDefinitions();
Chris@5:
Chris@5: if ($types) {
Chris@5:
Chris@5: // Build our slideshow options for the form.
Chris@5: $slideshow_options = [];
Chris@5: foreach ($types as $id => $definition) {
Chris@5: $slideshow_options[$id] = $definition['label'];
Chris@5: }
Chris@5:
Chris@5: $form['slideshow_type'] = [
Chris@5: '#type' => 'select',
Chris@5: '#title' => $this->t('Slideshow Type'),
Chris@5: '#options' => $slideshow_options,
Chris@5: '#default_value' => $this->options['slideshow_type'],
Chris@5: ];
Chris@5:
Chris@5: // @todo: check if default values are properly passed to the buildConfigurationForm().
Chris@5: foreach ($types as $id => $definition) {
Chris@5: $configuration = [];
Chris@5: if (!empty($this->options[$id])) {
Chris@5: $configuration = $this->options[$id];
Chris@5: }
Chris@5: $instance = $typeManager->createInstance($id, $configuration);
Chris@5:
Chris@5: $form[$id] = [
Chris@5: '#type' => 'fieldset',
Chris@5: '#title' => $this->t('@module options', ['@module' => $definition['label']]),
Chris@5: '#collapsible' => TRUE,
Chris@5: '#attributes' => ['class' => [$id]],
Chris@5: '#states' => [
Chris@5: 'visible' => [
Chris@5: ':input[name="style_options[slideshow_type]"]' => ['value' => $id],
Chris@5: ],
Chris@5: ],
Chris@5: ];
Chris@5:
Chris@5: $form = $instance->buildConfigurationForm($form, $form_state);
Chris@5: }
Chris@5: }
Chris@5: else {
Chris@5: $form['enable_module'] = [
Chris@5: '#markup' => $this->t('There is no Views Slideshow plugin enabled. Go to the @modules and enable a Views Slideshow plugin module. For example Views Slideshow Cycle.', ['@modules' => Link::fromTextAndUrl($this->t('Modules Page'), Url::fromRoute('system.modules_list'))->toString()]),
Chris@5: ];
Chris@5: }
Chris@5:
Chris@5: // Widgets.
Chris@5: // @todo: Improve the UX by using Ajax.
Chris@5: $form['widgets_header'] = [
Chris@5: '#markup' => '
' . $this->t('Widgets') . '
',
Chris@5: ];
Chris@5:
Chris@5: // Define the available locations.
Chris@5: $location = ['top' => $this->t('Top'), 'bottom' => $this->t('Bottom')];
Chris@5:
Chris@5: // Loop through all locations so we can add header for each location.
Chris@5: foreach ($location as $location_id => $location_name) {
Chris@5: $form['widgets'][$location_id]['header'] = [
Chris@5: '#markup' => '
' . $this->t('@location Widgets', ['@location' => $location_name]) . '
',
Chris@5: ];
Chris@5: }
Chris@5:
Chris@5: /* @var \Drupal\Component\Plugin\PluginManagerInterface */
Chris@5: $widgetTypeManager = \Drupal::service('plugin.manager.views_slideshow.widget_type');
Chris@5:
Chris@5: // Get all widgets types that are registered.
Chris@5: $widgets = $widgetTypeManager->getDefinitions();
Chris@5: if (!empty($widgets)) {
Chris@5:
Chris@5: // Build our weight values by number of widgets.
Chris@5: $weights = [];
Chris@5: for ($i = 1; $i <= count($widgets); $i++) {
Chris@5: $weights[$i] = $i;
Chris@5: }
Chris@5:
Chris@5: // Loop through our widgets and locations to build our form values for
Chris@5: // each widget.
Chris@5: foreach ($widgets as $widget_id => $widget_info) {
Chris@5:
Chris@5: // Determine if this widget type is compatible with any slideshow type.
Chris@5: $compatible_slideshows = [];
Chris@5: foreach ($types as $slideshow_id => $slideshow_info) {
Chris@5: if ($widgetTypeManager->createInstance($widget_id, [])->checkCompatiblity($slideshow_info)) {
Chris@5: $compatible_slideshows[] = $slideshow_id;
Chris@5: }
Chris@5: }
Chris@5:
Chris@5: // Display the widget config form only if the widget type is compatible
Chris@5: // with at least one slideshow type.
Chris@5: if (!empty($compatible_slideshows)) {
Chris@5: foreach ($location as $location_id => $location_name) {
Chris@5: // Use Widget Checkbox.
Chris@5: $form['widgets'][$location_id][$widget_id]['enable'] = [
Chris@5: '#type' => 'checkbox',
Chris@5: '#title' => $widget_info['label'],
Chris@5: '#default_value' => $this->options['widgets'][$location_id][$widget_id]['enable'],
Chris@5: '#description' => $this->t('Should @name be rendered at the @location of the slides.', ['@name' => $widget_info['label'], '@location' => $location_name]),
Chris@5: '#dependency' => [
Chris@5: 'edit-style-options-slideshow-type' => $compatible_slideshows,
Chris@5: ],
Chris@5: ];
Chris@5:
Chris@5: // Need to wrap this so it indents correctly.
Chris@5: $form['widgets'][$location_id][$widget_id]['wrapper'] = [
Chris@5: '#markup' => '
',
Chris@5: ];
Chris@5:
Chris@5: // Widget weight.
Chris@5: // We check to see if the default value is greater than the number
Chris@5: // of widgets just in case a widget has been removed and the form
Chris@5: // hasn't been saved again.
Chris@5: $weight = (isset($this->options['widgets'][$location_id][$widget_id]['weight'])) ? $this->options['widgets'][$location_id][$widget_id]['weight'] : 0;
Chris@5: if ($weight > count($widgets)) {
Chris@5: $weight = count($widgets);
Chris@5: }
Chris@5: $form['widgets'][$location_id][$widget_id]['weight'] = [
Chris@5: '#type' => 'select',
Chris@5: '#title' => $this->t('Weight of the @name', ['@name' => $widget_info['label']]),
Chris@5: '#default_value' => $weight,
Chris@5: '#options' => $weights,
Chris@5: '#description' => $this->t('Determines in what order the @name appears. A lower weight will cause the @name to be above higher weight items.', ['@name' => $widget_info['label']]),
Chris@5: '#prefix' => '
',
Chris@5: '#suffix' => '
',
Chris@5: '#states' => [
Chris@5: 'visible' => [
Chris@5: ':input[name="style_options[widgets][' . $location_id . '][' . $widget_id . '][enable]"]' => ['checked' => TRUE],
Chris@5: ],
Chris@5: ],
Chris@5: ];
Chris@5:
Chris@5: // Build the appropriate array for the states API.
Chris@5: $widget_dependency = 'style_options[widgets][' . $location_id . '][' . $widget_id . ']';
Chris@5:
Chris@5: // Get the current configuration of this widget type.
Chris@5: $configuration = [];
Chris@5: if (!empty($this->options['widgets'][$location_id][$widget_id])) {
Chris@5: $configuration = $this->options['widgets'][$location_id][$widget_id];
Chris@5: }
Chris@5: $configuration['dependency'] = $widget_dependency;
Chris@5: $instance = $widgetTypeManager->createInstance($widget_id, $configuration);
Chris@5:
Chris@5: // Get the configuration form of this widget type.
Chris@5: $form['widgets'][$location_id][$widget_id] = $instance->buildConfigurationForm($form['widgets'][$location_id][$widget_id], $form_state);
Chris@5:
Chris@5: // Close the vs-dependent wrapper.
Chris@5: $form['widgets'][$location_id][$widget_id]['wrapper_close'] = [
Chris@5: '#markup' => '
',
Chris@5: ];
Chris@5: }
Chris@5: }
Chris@5: }
Chris@5: }
Chris@5:
Chris@5: // Browse locations and remove the header if no widget is available.
Chris@5: foreach ($location as $location_id => $location_name) {
Chris@5: // If no widget is available, the only key is "header".
Chris@5: if (count(array_keys($form['widgets'][$location_id])) == 1) {
Chris@5: unset($form['widgets'][$location_id]);
Chris@5: }
Chris@5: }
Chris@5:
Chris@5: // Remove the widget section header if there is no widget available.
Chris@5: if (empty($form['widgets'])) {
Chris@5: unset($form['widgets']);
Chris@5: unset($form['widgets_header']);
Chris@5: }
Chris@5:
Chris@5: $form['views_slideshow_wrapper_close'] = [
Chris@5: '#markup' => '
',
Chris@5: ];
Chris@5:
Chris@5: // Add a library to style the form.
Chris@5: $form['#attached']['library'] = ['views_slideshow/form'];
Chris@5: }
Chris@5:
Chris@5: /**
Chris@5: * {@inheritdoc}
Chris@5: */
Chris@5: public function validateOptionsForm(&$form, FormStateInterface $form_state) {
Chris@5: // Validate all slideshow type plugins values.
Chris@5: $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
Chris@5: foreach ($typeManager->getDefinitions() as $id => $definition) {
Chris@5: $type = $typeManager->createInstance($id);
Chris@5: $type->validateConfigurationForm($form, $form_state);
Chris@5: }
Chris@5: }
Chris@5:
Chris@5: /**
Chris@5: * {@inheritdoc}
Chris@5: */
Chris@5: public function submitOptionsForm(&$form, FormStateInterface $form_state) {
Chris@5: // Submit all slideshow type plugins values.
Chris@5: $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
Chris@5: foreach ($typeManager->getDefinitions() as $id => $definition) {
Chris@5: $type = $typeManager->createInstance($id);
Chris@5: $type->submitConfigurationForm($form, $form_state);
Chris@5: }
Chris@5: }
Chris@5:
Chris@5: }