You are here

class SliderProForm in Slider Pro 8

Class SliderProForm @package Drupal\slider_pro\Form

Hierarchy

Expanded class hierarchy of SliderProForm

File

src/Form/SliderProForm.php, line 14

Namespace

Drupal\slider_pro\Form
View source
class SliderProForm extends EntityForm {

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    /** @var \Drupal\slider_pro\Entity\SliderProInterface $slider_pro */
    $slider_pro = $this->entity;
    $options = array_merge(SliderProDefaults::defaultOptions(), $slider_pro
      ->getOptions());
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $slider_pro
        ->label(),
      '#description' => $this
        ->t('A human-readable title for this option set.'),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $slider_pro
        ->id(),
      '#machine_name' => [
        'exists' => '\\Drupal\\slider_pro\\Entity\\SliderPro::load',
      ],
      '#disabled' => !$slider_pro
        ->isNew(),
    ];

    // Options Vertical Tab Group table.
    $form['tabs'] = [
      '#type' => 'vertical_tabs',
    ];

    // General Settings.
    $form['general'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('General Settings'),
      '#group' => 'tabs',
      '#open' => TRUE,
    ];
    $form['general']['width'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Width'),
      '#description' => 'The width of the slider. Eg 200px or 50%',
      '#default_value' => $options['width'],
      '#required' => TRUE,
    ];
    $form['general']['visible_size'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Visible size'),
      '#description' => 'Sets the size (for example 100%) of the visible area, allowing for more slides to become visible near the selected slide. Be sure that the width of your slider is less than 100%. Example',
      '#default_value' => $options['visible_size'],
      '#required' => TRUE,
    ];
    $form['general']['force_size'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Force size'),
      '#options' => [
        'none' => $this
          ->t('None'),
        'fullWidth' => $this
          ->t('Full width'),
        'fullWindow' => $this
          ->t('Full window'),
      ],
      '#default_value' => $options['force_size'],
      '#description' => $this
        ->t('Indicates if the size of the slider will be forced to full width or full window.'),
      '#required' => TRUE,
    ];
    $form['general']['height'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Height'),
      '#default_value' => $options['height'],
      '#description' => 'The height of the slider. Eg 200px or 50%',
    ];
    $form['general']['orientation'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Orientation'),
      '#options' => [
        'horizontal' => $this
          ->t('Horizontal'),
        'vertical' => $this
          ->t('Vertical'),
      ],
      '#default_value' => $options['orientation'],
      '#description' => $this
        ->t('Indicates whether the slides will be arranged horizontally or vertically.'),
      '#required' => TRUE,
    ];
    $form['general']['buttons'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Buttons'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['buttons'],
      '#description' => $this
        ->t('Indicates whether the buttons will be created.'),
      '#required' => TRUE,
    ];
    $form['general']['transition'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Transition'),
      '#options' => [
        0 => $this
          ->t('Slide'),
        1 => $this
          ->t('Fade'),
      ],
      '#default_value' => $options['transition'],
      '#description' => $this
        ->t('Indicates which transition will be used.'),
      '#required' => TRUE,
    ];
    $form['general']['arrows'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Arrows'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['arrows'],
      '#description' => $this
        ->t('Indicates whether the arrow buttons will be created.'),
      '#required' => TRUE,
    ];

    // Thumbnail Settings.
    $form['thumbnails'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Thumbnail Settings'),
      '#group' => 'tabs',
      '#open' => FALSE,
    ];
    $form['thumbnails']['thumbnail_position'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Position'),
      '#options' => [
        0 => $this
          ->t('No thumbs'),
        'left' => $this
          ->t('Left'),
        'right' => $this
          ->t('Right'),
        'top' => $this
          ->t('Top'),
        'bottom' => $this
          ->t('Bottom'),
      ],
      '#default_value' => $options['thumbnail_position'],
      '#required' => TRUE,
      '#description' => $this
        ->t('Sets the position of the thumbnail scroller.'),
    ];
    $form['thumbnails']['thumbnail_width'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Width'),
      '#description' => $this
        ->t('The width of each thumbnail. Eg 200px.'),
      '#default_value' => $options['thumbnail_width'],
      '#required' => TRUE,
      '#states' => [
        'invisible' => [
          'select[name="thumbnail_position"]' => [
            'value' => '0',
          ],
        ],
      ],
    ];
    $form['thumbnails']['thumbnail_height'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('height'),
      '#description' => $this
        ->t('The height of each thumbnail. Eg 200px.'),
      '#default_value' => $options['thumbnail_height'],
      '#required' => TRUE,
      '#states' => [
        'invisible' => [
          'select[name="thumbnail_position"]' => [
            'value' => '0',
          ],
        ],
      ],
    ];
    $form['thumbnails']['thumbnail_pointer'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Pointer'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['thumbnail_pointer'],
      '#required' => TRUE,
      '#description' => $this
        ->t('Indicates if a pointer will be displayed for the selected thumbnail.'),
      '#states' => [
        'invisible' => [
          'select[name="thumbnail_position"]' => [
            'value' => '0',
          ],
        ],
      ],
    ];
    $form['thumbnails']['thumbnail_arrows'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Arrows'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['thumbnail_arrows'],
      '#required' => TRUE,
      '#description' => $this
        ->t('Indicates whether the thumbnail arrows will be enabled.'),
      '#states' => [
        'invisible' => [
          'select[name="thumbnail_position"]' => [
            'value' => '0',
          ],
        ],
      ],
    ];
    $form['thumbnails']['fade_thumbnail_arrows'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Fade arrows'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['fade_thumbnail_arrows'],
      '#required' => TRUE,
      '#description' => $this
        ->t('Indicates whether the thumbnail arrows will be faded.'),
      '#states' => [
        'invisible' => [
          [
            'select[name="thumbnail_position"]' => [
              'value' => '0',
            ],
          ],
          [
            'select[name="thumbnail_arrows"]' => [
              'value' => '0',
            ],
          ],
        ],
      ],
    ];

    // Layer Settings.
    $form['layers'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Layer Settings'),
      '#group' => 'tabs',
      '#open' => FALSE,
    ];
    $form['layers']['wait_for_layers'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Wait for layers'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['wait_for_layers'],
      '#description' => $this
        ->t('Indicates whether the slider will wait for the layers to disappear before going to a new slide.'),
    ];
    $form['layers']['auto_scale_layers'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Auto scale layers'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['auto_scale_layers'],
      '#description' => $this
        ->t('Indicates whether the layers will be scaled automatically.'),
    ];

    // Advanced Settings.
    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced Settings'),
      '#group' => 'tabs',
      '#open' => FALSE,
    ];
    $form['advanced']['center_image'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Center image'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['center_image'],
      '#description' => $this
        ->t('Indicates if the image will be centered.'),
      '#required' => TRUE,
    ];
    $form['advanced']['allow_scale_up'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Allow scale up'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['allow_scale_up'],
      '#description' => $this
        ->t('Indicates if the image can be scaled up more than its original size.'),
      '#required' => TRUE,
    ];
    $form['advanced']['auto_height'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Auto height'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['auto_height'],
      '#description' => $this
        ->t('Indicates if height of the slider will be adjusted to the height of the selected slide.'),
      '#required' => TRUE,
    ];
    $form['advanced']['slide_distance'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Slide distance'),
      '#default_value' => $options['slide_distance'],
      '#description' => $this
        ->t('Sets the distance between the slides.'),
      '#required' => TRUE,
    ];
    $form['advanced']['start_slide'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Start slide'),
      '#default_value' => $options['start_slide'],
      '#description' => $this
        ->t('Sets the slide that will be selected when the slider loads.'),
      '#required' => TRUE,
    ];
    $form['advanced']['shuffle'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Shuffle'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['shuffle'],
      '#description' => $this
        ->t('Indicates if the slides will be shuffled.'),
      '#required' => TRUE,
    ];
    $form['advanced']['loop'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Loop'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['loop'],
      '#description' => $this
        ->t('Indicates if the slider will be loopable (infinite scrolling).'),
      '#required' => TRUE,
    ];
    $form['advanced']['autoplay'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Autoplay'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['autoplay'],
      '#description' => $this
        ->t('Indicates whether or not autoplay will be enabled.'),
      '#required' => TRUE,
    ];
    $form['advanced']['autoplay_delay'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Autoplay delay'),
      '#default_value' => $options['autoplay_delay'],
      '#description' => $this
        ->t('Sets the delay/interval (in milliseconds) at which the autoplay will run.'),
      '#required' => TRUE,
      '#states' => [
        'visible' => [
          'select[name="autoplay"]' => [
            'value' => '1',
          ],
        ],
      ],
    ];
    $form['advanced']['autoplay_direction'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Autoplay direction'),
      '#options' => [
        'normal' => $this
          ->t('Normal'),
        'backwards' => $this
          ->t('Backwards'),
      ],
      '#default_value' => $options['autoplay_direction'],
      '#description' => $this
        ->t('Indicates whether autoplay will navigate to the next slide or previous slide.'),
      '#required' => TRUE,
      '#states' => [
        'visible' => [
          'select[name="autoplay"]' => [
            'value' => '1',
          ],
        ],
      ],
    ];
    $form['advanced']['autoplay_on_hover'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Autoplay on hover'),
      '#options' => [
        'pause' => $this
          ->t('Pause'),
        'stop' => $this
          ->t('Stop'),
        'none' => $this
          ->t('None'),
      ],
      '#default_value' => $options['autoplay_on_hover'],
      '#description' => $this
        ->t('Indicates if the autoplay will be paused or stopped when the slider is hovered.'),
      '#required' => TRUE,
      '#states' => [
        'visible' => [
          'select[name="autoplay"]' => [
            'value' => '1',
          ],
        ],
      ],
    ];
    $form['advanced']['keyboard'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Keyboard'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['keyboard'],
      '#description' => $this
        ->t('Indicates whether keyboard navigation will be enabled.'),
      '#required' => TRUE,
    ];
    $form['advanced']['full_screen'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Full screen'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['full_screen'],
      '#description' => $this
        ->t('Indicates whether the full-screen button is enabled.'),
      '#required' => TRUE,
    ];
    $form['advanced']['fade_full_screen'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Fade full screen'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['fade_full_screen'],
      '#description' => $this
        ->t('Indicates whether the button will fade in only on hover.'),
      '#required' => TRUE,
      '#states' => [
        'visible' => [
          'select[name="full_screen"]' => [
            'value' => '1',
          ],
        ],
      ],
    ];
    $form['advanced']['update_hash'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Update hash'),
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#default_value' => $options['update_hash'],
      '#description' => $this
        ->t('Indicates whether the hash will be updated when a new slide is selected.'),
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\slider_pro\SliderProInterface $slider_pro */
    $slider_pro = $this->entity;
    $status = parent::save($form, $form_state);
    switch ($status) {
      case SAVED_NEW:
        drupal_set_message($this
          ->t('Created the %label Slider Pro optionset.', [
          '%label' => $slider_pro
            ->label(),
        ]));
        break;
      default:
        drupal_set_message($this
          ->t('Saved the %label Slider Pro optionset.', [
          '%label' => $slider_pro
            ->label(),
        ]));
    }
    $form_state
      ->setRedirectUrl($slider_pro
      ->toUrl('collection'));
  }

  /**
   * {@inheritdoc}
   */
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    $options = [];
    $values = $form_state
      ->getValues();
    foreach ($values as $key => $value) {
      if (in_array($key, [
        'id',
        'label',
      ])) {
        $entity
          ->set($key, $value);
      }
      else {
        $options[$key] = $value;
      }
    }
    $entity
      ->set('options', $options);
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);

    // Prevent access to delete button when editing default configuration.
    if ($this->entity
      ->id() == 'default' && isset($actions['delete'])) {
      $actions['delete']['#access'] = FALSE;
    }
    return $actions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 7
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 2
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 10
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides FormInterface::submitForm 17
EntityForm::__get public function
EntityForm::__set public function
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SliderProForm::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
SliderProForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties Overrides EntityForm::copyFormValuesToEntity
SliderProForm::form public function Gets the actual form array to be built. Overrides EntityForm::form
SliderProForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.