class SharebarSettingsForm in ShareBar 8
Implements an example form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\sharebar\Form\SharebarSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SharebarSettingsForm
File
- src/
Form/ SharebarSettingsForm.php, line 17 - Contains \Drupal\sharebar\Form\SharebarSettingsForm.
Namespace
Drupal\sharebar\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
ConfigFormBaseTrait:: |
abstract protected | function | Gets the configuration names that will be editable. | 32 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SharebarSettingsForm:: |
public | function |
Form builder: Configure the sharebar system. Overrides ConfigFormBase:: |
|
SharebarSettingsForm:: |
public | function |
. Overrides FormInterface:: |
|
SharebarSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |