You are here

class AdminSettingsForm in Tipsy 8

Class AdminSettingsForm.

@package Drupal\tipsy\Form

Hierarchy

Expanded class hierarchy of AdminSettingsForm

1 string reference to 'AdminSettingsForm'
tipsy.routing.yml in ./tipsy.routing.yml
tipsy.routing.yml

File

src/Form/AdminSettingsForm.php, line 15

Namespace

Drupal\tipsy\Form
View source
class AdminSettingsForm extends ConfigFormBase {
  protected $configFactory;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactory $configFactory) {
    $this->configFactory = $configFactory
      ->getEditable('tipsy.settings');
  }

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

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

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

  /**
   * Function to add one option.
   */
  public function tipsyAddOneOption(array $form, FormStateInterface $form_state) {
    $options_count = $this->configFactory
      ->get("total_options");
    $options_count++;
    $this->configFactory
      ->set('total_options', $options_count);
    $form_state
      ->setRebuild();
  }

  /**
   * Function to return form with existing form fields.
   */
  public function tipsyAddMoreCallback(array $form, FormStateInterface $form_state) {
    return $form['custom_selectors'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $total_options = $this->configFactory
      ->get("total_options");
    $settings = _tipsy_get_settings();
    $form = array();
    $form['#tree'] = TRUE;

    // Add admin js and admin css files.
    $form['#attached']['library'][] = 'tipsy/tipsy_admin';
    $form['drupal_forms'] = array(
      '#type' => 'details',
      '#title' => $this
        ->t('Drupal forms general settings'),
      '#weight' => -5,
      '#open' => TRUE,
    );
    $form['drupal_forms']['forms'] = array(
      '#type' => 'checkbox',
      '#default_value' => $this->configFactory
        ->get("wide_settings")['drupal_forms']['forms'],
      '#title' => $this
        ->t('Apply Tipsy for form items descriptions on all Drupal forms.'),
      '#description' => $this
        ->t('This will automatically enable Tipsy tooltips to form elements descriptions.'),
    );
    $form['drupal_forms']['wrapper'] = array(
      '#tree' => TRUE,
      '#weight' => 0,
      '#prefix' => '<div class="clear-block" id="tipsy-drupal-forms-wrapper">',
      '#suffix' => '</div>',
    );
    $form['drupal_forms']['wrapper']['options'] = $this
      ->tipsyOptionsForm($settings['drupal_forms'], TRUE);
    $form['custom_selectors'] = array(
      '#type' => 'details',
      '#title' => $this
        ->t('Custom selectors'),
      '#description' => '<div class="my_selector">
    <div class="contents">Tooltip HTML that can\'t really fit into an attribute</div>Ordinary text that the user sees</div>',
      '#prefix' => '<div id="names-fieldset-wrapper">',
      '#suffix' => '</div>',
      '#open' => TRUE,
    );

    // Variable $total_options determine the number of textfields to build.
    $custom_selectors = $this->configFactory
      ->get('wide_settings')['custom_selectors'];
    for ($i = 0; $i <= $total_options; $i++) {
      isset($custom_selectors[$i]) ?: ($custom_selectors[$i] = $this->configFactory
        ->get('new_rule_settings'));
      $form['custom_selectors'][$i]['selector'] = array(
        '#type' => 'textarea',
        '#weight' => 0,
        '#rows' => 2,
        '#default_value' => $custom_selectors[$i]['selector'],
      );
      $form['custom_selectors'][$i]['options']['fade'] = array(
        '#type' => 'checkbox',
        '#default_value' => $custom_selectors[$i]['options']['fade'],
        '#description' => $this
          ->t('This will make the tooltip fade.'),
        '#title' => $this
          ->t('Make Tipsy tooltips fade.'),
        '#weight' => 1,
        '#prefix' => '<div class="tipsy-selector-options clear-block">',
      );
      $form['custom_selectors'][$i]['options']['gravity'] = array(
        '#type' => 'select',
        '#default_value' => $custom_selectors[$i]['options']['gravity'],
        '#title' => $this
          ->t('Tipsy arrow position'),
        '#description' => $this
          ->t('Specify the position of the tooltip when it appears.'),
        '#weight' => 2,
        '#options' => array(
          'nw' => $this
            ->t('North west'),
          'n' => $this
            ->t('North'),
          'ne' => $this
            ->t('North east'),
          'w' => $this
            ->t('West'),
          'e' => $this
            ->t('East'),
          'sw' => $this
            ->t('South west'),
          's' => $this
            ->t('South'),
          'se' => $this
            ->t('South east'),
          'autoNS' => $this
            ->t('Auto detect North/South'),
          'autoWE' => $this
            ->t('Auto detect West/East'),
        ),
      );
      $form['custom_selectors'][$i]['options']['delayIn'] = array(
        '#type' => 'textfield',
        '#default_value' => $custom_selectors[$i]['options']['delayIn'],
        '#title' => $this
          ->t('Delay when appearing'),
        '#description' => $this
          ->t('Amount of milliseconds for the tooltip to appear.'),
        '#size' => 5,
        '#maxlength' => 5,
        '#weight' => 3,
      );
      $form['custom_selectors'][$i]['options']['delayOut'] = array(
        '#type' => 'textfield',
        '#default_value' => $custom_selectors[$i]['options']['delayOut'],
        '#title' => $this
          ->t('Delay when disappearing'),
        '#description' => $this
          ->t('Amount of milliseconds for the tooltip to disappear.'),
        '#size' => 5,
        '#maxlength' => 5,
        '#weight' => 4,
      );
      $form['custom_selectors'][$i]['options']['trigger'] = array(
        '#type' => 'select',
        '#default_value' => $custom_selectors[$i]['options']['trigger'],
        '#description' => $this
          ->t('Specify what action will make the tooltip appear.'),
        '#title' => $this
          ->t('Tipsy trigger'),
        '#weight' => 5,
        '#options' => array(
          'focus' => $this
            ->t('Focus'),
          'hover' => $this
            ->t('Hover'),
        ),
      );
      $form['custom_selectors'][$i]['options']['opacity'] = array(
        '#type' => 'textfield',
        '#default_value' => $custom_selectors[$i]['options']['opacity'],
        '#title' => $this
          ->t('Tooltip opacity'),
        '#description' => $this
          ->t('A value between 0 and 1.'),
        '#size' => 5,
        '#maxlength' => 4,
        '#weight' => 6,
      );
      $form['custom_selectors'][$i]['options']['offset'] = array(
        '#type' => 'textfield',
        '#default_value' => $custom_selectors[$i]['options']['offset'],
        '#title' => $this
          ->t('Tooltip offset'),
        '#description' => $this
          ->t('Number of pixels in which the tooltip will distance from the element.'),
        '#size' => 5,
        '#maxlength' => 5,
        '#weight' => 7,
      );
      $form['custom_selectors'][$i]['options']['html'] = array(
        '#type' => 'checkbox',
        '#default_value' => $custom_selectors[$i]['options']['html'],
        '#description' => $this
          ->t('This will let HTML code be parsed inside the tooltip.'),
        '#title' => $this
          ->t('Allow HTML in tooltip content.'),
        '#weight' => 1,
      );
      $form['custom_selectors'][$i]['options']['tooltip_content'] = array(
        '#type' => 'details',
        '#title' => $this
          ->t('Tooltip content'),
        '#open' => TRUE,
        '#weight' => 9,
      );
      $form['custom_selectors'][$i]['options']['tooltip_content']['source'] = array(
        '#type' => 'radios',
        '#title' => $this
          ->t('Source'),
        '#default_value' => $custom_selectors[$i]['options']['tooltip_content']['source'],
        '#options' => array(
          'attribute' => $this
            ->t('HTML attribute'),
          'child' => $this
            ->t('Child element'),
        ),
      );
      $form['custom_selectors'][$i]['options']['tooltip_content']['selector'] = array(
        '#type' => 'textarea',
        '#title' => $this
          ->t('Selector'),
        '#default_value' => $custom_selectors[$i]['options']['tooltip_content']['selector'],
        '#description' => $this
          ->t("The name of the HTML attribute or a selector pointing to the child element (e.g: .content). <br /> Refer to the module's README.txt for more information."),
        '#rows' => 1,
        '#maxlength' => 400,
      );
      $form['closure'] = array(
        '#weight' => 10,
        '#suffix' => '</div>',
      );
    }
    $form['custom_selectors']['add_option'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Add one more'),
      '#submit' => array(
        '::tipsyAddOneOption',
      ),
      '#ajax' => array(
        'callback' => '::tipsyAddMoreCallback',
        'wrapper' => 'names-fieldset-wrapper',
      ),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $options_count = $this->configFactory
      ->get("total_options");
    $wide_settings = [
      'drupal_forms' => [
        'forms' => $values['drupal_forms']['forms'],
        'options' => $values['drupal_forms']['wrapper']['options'],
      ],
      'custom_selectors' => [],
    ];
    for ($i = 0; $i <= $options_count; $i++) {
      $wide_settings['custom_selectors'][$i] = $values['custom_selectors'][$i];
    }
    $this->configFactory
      ->set('wide_settings', $wide_settings)
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Implementation of form function for the tooltip options.
   */
  public function tipsyOptionsForm($settings = FALSE, $drupal_forms = FALSE) {
    if ($settings == FALSE) {
      $settings = _tipsy_get_settings(TRUE);
    }
    $form = array();
    $form['fade'] = array(
      '#type' => 'checkbox',
      '#default_value' => $settings['options']['fade'],
      '#description' => $this
        ->t('This will make the tooltip fade.'),
      '#title' => $this
        ->t('Make Tipsy tooltips fade.'),
      '#weight' => 0,
      '#prefix' => '<div class="tipsy-selector-options clear-block">',
    );
    $form['gravity'] = array(
      '#type' => 'select',
      '#default_value' => $settings['options']['gravity'],
      '#title' => $this
        ->t('Tipsy arrow position'),
      '#description' => $this
        ->t('Specify the position of the tooltip when it appears.'),
      '#weight' => 2,
      '#options' => array(
        'nw' => $this
          ->t('North west'),
        'n' => $this
          ->t('North'),
        'ne' => $this
          ->t('North east'),
        'w' => $this
          ->t('West'),
        'e' => $this
          ->t('East'),
        'sw' => $this
          ->t('South west'),
        's' => $this
          ->t('South'),
        'se' => $this
          ->t('South east'),
        'autoNS' => $this
          ->t('Auto detect North/South'),
        'autoWE' => $this
          ->t('Auto detect West/East'),
      ),
    );
    $form['delayIn'] = array(
      '#type' => 'textfield',
      '#default_value' => $settings['options']['delayIn'],
      '#title' => $this
        ->t('Delay when appearing'),
      '#description' => $this
        ->t('Amount of milliseconds for the tooltip to appear.'),
      '#size' => 5,
      '#maxlength' => 5,
      '#weight' => 3,
    );
    $form['delayOut'] = array(
      '#type' => 'textfield',
      '#default_value' => $settings['options']['delayOut'],
      '#title' => $this
        ->t('Delay when disappearing'),
      '#description' => $this
        ->t('Amount of milliseconds for the tooltip to disappear.'),
      '#size' => 5,
      '#maxlength' => 5,
      '#weight' => 4,
    );
    $form['trigger'] = array(
      '#type' => 'select',
      '#default_value' => $settings['options']['trigger'],
      '#description' => $this
        ->t('Specify what action will make the tooltip appear.'),
      '#title' => $this
        ->t('Tipsy trigger'),
      '#weight' => 5,
      '#options' => array(
        'focus' => $this
          ->t('Focus'),
        'hover' => $this
          ->t('Hover'),
      ),
    );
    $form['opacity'] = array(
      '#type' => 'textfield',
      '#default_value' => $settings['options']['opacity'],
      '#title' => $this
        ->t('Tooltip opacity'),
      '#description' => $this
        ->t('A value between 0 and 1.'),
      '#size' => 5,
      '#maxlength' => 4,
      '#weight' => 6,
    );
    $form['offset'] = array(
      '#type' => 'textfield',
      '#default_value' => $settings['options']['offset'],
      '#title' => $this
        ->t('Tooltip offset'),
      '#description' => $this
        ->t('Number of pixels in which the tooltip will distance from the element.'),
      '#size' => 5,
      '#maxlength' => 5,
      '#weight' => 7,
    );
    if ($drupal_forms == FALSE) {
      $form['html'] = array(
        '#type' => 'checkbox',
        '#default_value' => $settings['options']['html'],
        '#description' => $this
          ->t('This will let HTML code be parsed inside the tooltip.'),
        '#title' => $this
          ->t('Allow HTML in tooltip content.'),
        '#weight' => 1,
      );
      $form['tooltip_content'] = array(
        '#type' => 'details',
        '#title' => $this
          ->t('Tooltip content'),
        '#weight' => 9,
        '#open' => TRUE,
      );
      $form['tooltip_content']['source'] = array(
        '#type' => 'radios',
        '#title' => $this
          ->t('Source'),
        '#default_value' => $settings['options']['tooltip_content']['source'],
        '#options' => array(
          'attribute' => $this
            ->t('HTML attribute'),
          'child' => $this
            ->t('Child element'),
        ),
      );
      $form['tooltip_content']['selector'] = array(
        '#type' => 'textarea',
        '#title' => $this
          ->t('Selector'),
        '#default_value' => $settings['options']['tooltip_content']['selector'],
        '#description' => $this
          ->t("The name of the HTML attribute or a selector pointing to the child element (e.g: .content). <br /> Refer to the module's README.txt for more information."),
        '#rows' => 1,
        '#maxlength' => 400,
      );
    }
    $form['closure'] = array(
      '#weight' => 10,
      '#suffix' => '</div>',
    );
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdminSettingsForm::$configFactory protected property The config factory. Overrides FormBase::$configFactory
AdminSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AdminSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
AdminSettingsForm::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AdminSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AdminSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
AdminSettingsForm::tipsyAddMoreCallback public function Function to return form with existing form fields.
AdminSettingsForm::tipsyAddOneOption public function Function to add one option.
AdminSettingsForm::tipsyOptionsForm public function Implementation of form function for the tooltip options.
AdminSettingsForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct
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::$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.
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.