You are here

class SettingsForm in bootstrap simple carousel 8

Class SettingsForm.

Provides a settings form.

@package Drupal\bootstrap_simple_carousel\Form

Hierarchy

Expanded class hierarchy of SettingsForm

2 files declare their use of SettingsForm
bootstrap_simple_carousel.install in ./bootstrap_simple_carousel.install
Defines install of bootstrap simple carousel.
CarouselBlock.php in src/Plugin/Block/CarouselBlock.php
1 string reference to 'SettingsForm'
bootstrap_simple_carousel.routing.yml in ./bootstrap_simple_carousel.routing.yml
bootstrap_simple_carousel.routing.yml

File

src/Form/SettingsForm.php, line 18

Namespace

Drupal\bootstrap_simple_carousel\Form
View source
class SettingsForm extends ConfigFormBase {
  const ORIGINAL_IMAGE_STYLE_ID = 'original';
  const DEFAULT_IMAGE_TYPE_ID = 'img-default';
  const FLUID_IMAGE_TYPE_ID = 'img-fluid';
  const CIRCLE_IMAGE_TYPE_ID = 'img-circle';

  /**
   * Image style service.
   *
   * @var \Drupal\image\ImageStyleInterface
   */
  protected $imageStyleService;

  /**
   * SettingsForm constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->imageStyleService = $entity_type_manager
      ->getStorage('image_style');
    parent::__construct($config_factory);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'bootstrap_simple_carousel_admin_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'bootstrap_simple_carousel.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('bootstrap_simple_carousel.settings');
    $form['interval'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Interval'),
      '#description' => $this
        ->t('The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle.'),
      '#default_value' => $config
        ->get('interval'),
    ];
    $form['wrap'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Wrap'),
      '#description' => $this
        ->t('Whether the carousel should cycle continuously or have hard stops.'),
      '#default_value' => $config
        ->get('wrap'),
    ];
    $form['pause'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Pause on hover'),
      '#description' => $this
        ->t("If is checked, pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on mouseleave. If is unchecked, hovering over the carousel won't pause it."),
      '#default_value' => $config
        ->get('pause'),
    ];
    $form['indicators'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Indicators'),
      '#description' => $this
        ->t('Show carousel indicators'),
      '#default_value' => $config
        ->get('indicators'),
    ];
    $form['controls'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Controls'),
      '#description' => $this
        ->t('Show carousel arrows (next/prev).'),
      '#default_value' => $config
        ->get('controls'),
    ];
    $form['assets'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Assets'),
      '#description' => $this
        ->t("Includes bootstrap framework v4.0.0, don't check it, if you use the bootstrap theme, or the bootstrap framework are already included."),
      '#default_value' => $config
        ->get('assets'),
    ];
    $form['image_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Bootstrap image type'),
      '#description' => $this
        ->t('Bootstrap image type for carousel items.'),
      '#options' => $this
        ->getImagesTypes(),
      '#default_value' => $config
        ->get('image_type'),
    ];
    $form['image_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Image style'),
      '#description' => $this
        ->t('Image style for carousel items. If you will be use the image styles for bootstrap items, you need to set up the same width for the "bootstrap carousel" container.'),
      '#options' => $this
        ->getImagesStyles(),
      '#default_value' => $config
        ->get('image_style'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * Return images styles.
   *
   * @return array
   *   List of image styles
   */
  protected function getImagesStyles() {
    $styles = $this->imageStyleService
      ->loadMultiple();
    $options = [
      static::ORIGINAL_IMAGE_STYLE_ID => $this
        ->t('Original image'),
    ];
    foreach ($styles as $key => $value) {
      $options[$key] = $value
        ->get('label');
    }
    return $options;
  }

  /**
   * Return bootstrap images types.
   *
   * @return array
   *   Image types list
   */
  protected function getImagesTypes() {
    $options = [
      static::DEFAULT_IMAGE_TYPE_ID => $this
        ->t('Image none'),
      static::FLUID_IMAGE_TYPE_ID => $this
        ->t('Image fluid'),
      static::CIRCLE_IMAGE_TYPE_ID => $this
        ->t('Image circle'),
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory
      ->getEditable('bootstrap_simple_carousel.settings')
      ->set('interval', $form_state
      ->getValue('interval'))
      ->set('wrap', $form_state
      ->getValue('wrap'))
      ->set('pause', $form_state
      ->getValue('pause'))
      ->set('indicators', $form_state
      ->getValue('indicators'))
      ->set('controls', $form_state
      ->getValue('controls'))
      ->set('assets', $form_state
      ->getValue('assets'))
      ->set('image_type', $form_state
      ->getValue('image_type'))
      ->set('image_style', $form_state
      ->getValue('image_style'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
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.
SettingsForm::$imageStyleService protected property Image style service.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::CIRCLE_IMAGE_TYPE_ID constant
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::DEFAULT_IMAGE_TYPE_ID constant
SettingsForm::FLUID_IMAGE_TYPE_ID constant
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::getImagesStyles protected function Return images styles.
SettingsForm::getImagesTypes protected function Return bootstrap images types.
SettingsForm::ORIGINAL_IMAGE_STYLE_ID constant
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::__construct public function SettingsForm constructor. Overrides ConfigFormBase::__construct
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.