You are here

class SharebarSettingsForm in ShareBar 8

Implements an example form.

Hierarchy

Expanded class hierarchy of SharebarSettingsForm

File

src/Form/SharebarSettingsForm.php, line 17
Contains \Drupal\sharebar\Form\SharebarSettingsForm.

Namespace

Drupal\sharebar\Form
View source
class SharebarSettingsForm extends ConfigFormBase {

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

  /**
   * Form builder: Configure the sharebar system.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $extra = NULL) {
    $config = $this
      ->config('sharebar.settings');

    //  echo "<pre>"; print_r($config); echo "</pre>"; die;
    // echo "hi"; echo $config->get('sharebar_bar_pages_enabled'); die;
    _drupal_add_css(drupal_get_path('module', 'sharebar') . '/css/colorpicker.css');
    _drupal_add_js(drupal_get_path('module', 'sharebar') . '/js/colorpicker.js');
    _drupal_add_js('jQuery(document).ready(function($) {
			var ids = ["edit-sharebar-bar-background","edit-sharebar-bar-border"];
			$.each(ids, function() {
				var id = this;
				$("#"+this).ColorPicker({
					onSubmit: function(hsb, hex, rgb, el) {
						$(el).val(hex);
						$(el).ColorPickerHide();
					},
					onBeforeShow: function () {
						$(this).ColorPickerSetColor(this.value);
					},
					onChange: function(hsb, hex, rgb, el) {
						$("#"+id).val(hex);
					}
				});
			});
		});
  ', 'inline');
    $form['buttonsset'] = array(
      '#type' => 'fieldset',
      '#title' => t('Buttons'),
      '#description' => t(''),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['buttonsset']['buttons'] = array(
      '#theme' => 'sharebar_buttons_table',
      '#weight' => 0,
    );

    // Add sharebar.
    $form['addsharebar'] = array(
      '#type' => 'fieldset',
      '#weight' => 1,
      '#title' => t('Add sharebar'),
      '#description' => t('The following settings allow you to automatically add the Sharebar to your pages.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['addsharebar']['nodetypes'] = array(
      '#type' => 'fieldset',
      '#title' => t('Content Types'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $node_types = node_type_get_types();
    $node_names = node_type_get_names();
    if (is_array($node_names) && count($node_names)) {
      foreach ($node_names as $key => $value) {
        $form['addsharebar']['nodetypes']['sharebar_bar_posts_' . $node_types[$key]->type . '_enabled'] = array(
          '#type' => 'checkbox',
          '#title' => t('Automatically add Sharebar to content type @value (only affects content type @value)', array(
            '@value' => $value,
          )),
          '#default_value' => \Drupal::config('sharebar.settings')
            ->get('sharebar_bar_posts_' . $node_types[$key]->type . '_enabled'),
        );
      }
    }
    $form['addsharebar']['sharebar_bar_pages_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Automatically add to all pages except content pages.'),
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_pages_enabled'),
    );

    // Display options.
    $form['displayoptions'] = array(
      '#type' => 'fieldset',
      '#weight' => 2,
      '#title' => t('Display options'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['displayoptions']['sharebar_bar_horizontal'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display horizontal Sharebar if the page is resized to less than 1000px?'),
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_horizontal'),
    );
    $form['displayoptions']['sharebar_bar_oncontent'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display sharebar in content'),
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_oncontent'),
    );
    $form['displayoptions']['sharebar_bar_credit'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display credit link back to the Sharebar plugin? If disabled, please consider donating.'),
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_credit'),
    );
    $form['displayoptions']['sharebar_bar_idcontent'] = array(
      '#type' => 'textfield',
      '#title' => t('Custom CSS Container when displayed in content region(only id selector is supported)'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_idcontent'),
      '#states' => array(
        'visible' => array(
          ':input[name="sharebar_bar_oncontent"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['displayoptions']['sharebar_bar_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Custom CSS Container when not displayed in content region (only id selector is supported)'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_id'),
      '#states' => array(
        'visible' => array(
          ':input[name="sharebar_bar_oncontent"]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
    );
    $form['displayoptions']['sharebar_bar_idhorizontal'] = array(
      '#type' => 'textfield',
      '#title' => t('Custom CSS Container when displayed horizontally (only id selector is supported)'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_idhorizontal'),
      '#states' => array(
        'visible' => array(
          ':input[name="sharebar_bar_horizontal"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['displayoptions']['sharebar_bar_toptoffset'] = array(
      '#type' => 'textfield',
      '#title' => t('Top offset'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_toptoffset'),
    );
    $form['displayoptions']['sharebar_bar_position'] = array(
      '#type' => 'select',
      '#title' => t('Sharebar Position'),
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_position'),
      '#options' => array(
        'left' => 'Left',
        'right' => 'Right',
      ),
    );
    $form['displayoptions']['sharebar_bar_leftoffset'] = array(
      '#type' => 'textfield',
      '#title' => t('Left Offset (used when positioned to left)'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_leftoffset'),
    );
    $form['displayoptions']['sharebar_bar_rightoffset'] = array(
      '#type' => 'textfield',
      '#title' => t('Right Offset (used when positioned to right)'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_rightoffset'),
    );
    $form['displayoptions']['sharebar_bar_width'] = array(
      '#type' => 'textfield',
      '#title' => t('Minimum width in pixels required to show vertical Sharebar to the left of post (cannot be blank)'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_width'),
    );

    // Customize.
    $form['customize'] = array(
      '#type' => 'fieldset',
      '#weight' => 3,
      '#title' => t('Customize'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['customize']['sharebar_bar_swidth'] = array(
      '#type' => 'textfield',
      '#title' => t('Sharebar Width'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_swidth'),
    );
    $form['customize']['sharebar_bar_twitter_username'] = array(
      '#type' => 'textfield',
      '#title' => t('Twitter Username'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_twitter_username'),
    );
    $form['customize']['sharebar_bar_background'] = array(
      '#type' => 'textfield',
      '#title' => t('Sharebar Background Color'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_background'),
    );
    $form['customize']['sharebar_bar_border'] = array(
      '#type' => 'textfield',
      '#title' => t('Sharebar Border Color'),
      '#size' => 10,
      '#default_value' => \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_border'),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $node_types = node_type_get_types();
    $node_names = node_type_get_names();
    if (is_array($node_names) && count($node_names)) {
      foreach ($node_names as $key => $value) {
        $this
          ->config('sharebar.settings')
          ->set('sharebar_bar_posts_' . $node_types[$key]->type . '_enabled', $form_state
          ->getValue('sharebar_bar_posts_' . $node_types[$key]->type . '_enabled'))
          ->save();
      }
    }
    $this
      ->config('sharebar.settings')
      ->set('sharebar_bar_pages_enabled', $form_state
      ->getValue('sharebar_bar_pages_enabled'))
      ->set('sharebar_bar_horizontal', $form_state
      ->getValue('sharebar_bar_horizontal'))
      ->set('sharebar_bar_oncontent', $form_state
      ->getValue('sharebar_bar_oncontent'))
      ->set('sharebar_bar_credit', $form_state
      ->getValue('sharebar_bar_credit'))
      ->set('sharebar_bar_idcontent', $form_state
      ->getValue('sharebar_bar_idcontent'))
      ->set('sharebar_bar_id', $form_state
      ->getValue('sharebar_bar_id'))
      ->set('sharebar_bar_idhorizontal', $form_state
      ->getValue('sharebar_bar_idhorizontal'))
      ->set('sharebar_bar_toptoffset', $form_state
      ->getValue('sharebar_bar_toptoffset'))
      ->set('sharebar_bar_position', $form_state
      ->getValue('sharebar_bar_position'))
      ->set('sharebar_bar_leftoffset', $form_state
      ->getValue('sharebar_bar_leftoffset'))
      ->set('sharebar_bar_rightoffset', $form_state
      ->getValue('sharebar_bar_rightoffset'))
      ->set('sharebar_bar_width', $form_state
      ->getValue('sharebar_bar_width'))
      ->set('sharebar_bar_swidth', $form_state
      ->getValue('sharebar_bar_swidth'))
      ->set('sharebar_bar_twitter_username', $form_state
      ->getValue('sharebar_bar_twitter_username'))
      ->set('sharebar_bar_background', $form_state
      ->getValue('sharebar_bar_background'))
      ->set('sharebar_bar_border', $form_state
      ->getValue('sharebar_bar_border'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
ConfigFormBaseTrait::getEditableConfigNames abstract protected function Gets the configuration names that will be editable. 32
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.
SharebarSettingsForm::buildForm public function Form builder: Configure the sharebar system. Overrides ConfigFormBase::buildForm
SharebarSettingsForm::getFormId public function . Overrides FormInterface::getFormId
SharebarSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
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.