class StyleguideForm in Style Guide 8
Same name and namespace in other branches
- 2.x src/Form/StyleguideForm.php \Drupal\styleguide\Form\StyleguideForm
Defines a form builder to display form controls in style guide.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\styleguide\Form\StyleguideForm
Expanded class hierarchy of StyleguideForm
File
- src/
Form/ StyleguideForm.php, line 14
Namespace
Drupal\styleguide\FormView source
class StyleguideForm extends FormBase {
/**
* The styleguide generator.
*
* @var \Drupal\styleguide\GeneratorInterface
*/
protected $generator;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a new StyleguideForm object.
*
* @param \Drupal\styleguide\GeneratorInterface $styleguide_generator
* The styleguide generator.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(GeneratorInterface $styleguide_generator, ModuleHandlerInterface $module_handler) {
$this->generator = $styleguide_generator;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('styleguide.generator'), $container
->get('module_handler'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'styleguide_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $form_keys = []) {
// @todo Use element names and defaults from element_info service.
$form = [];
$options = [];
$list = $this->generator
->wordList();
foreach ($list as $item) {
$options[$item] = $item;
}
$form['select'] = [
'#type' => 'select',
'#title' => $this
->t('Select'),
'#options' => $options,
'#description' => $this->generator
->sentence(),
];
$form['checkbox'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Checkbox'),
'#value' => 1,
'#default_value' => 1,
'#description' => $this->generator
->sentence(),
];
$form['checkboxes'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Checkboxes'),
'#options' => $options,
'#description' => $this->generator
->sentence(),
];
$form['radios'] = [
'#type' => 'radios',
'#title' => $this
->t('Radios'),
'#options' => $options,
'#description' => $this->generator
->sentence(),
];
$form['textfield'] = [
'#type' => 'textfield',
'#title' => $this
->t('Textfield'),
'#default_value' => $this->generator
->words(3, 'ucfirst'),
'#description' => $this->generator
->sentence(),
];
$form['entity_autocomplete'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Node entity autocomplete'),
'#description' => $this->generator
->sentence(),
'#target_type' => 'node',
];
$form['textfield-machine'] = [
'#type' => 'textfield',
'#title' => $this
->t('Textfield, with machine name'),
'#default_value' => $this->generator
->words(3, 'ucfirst'),
'#description' => $this->generator
->sentence(),
];
$form['machine_name'] = [
'#type' => 'machine_name',
'#title' => $this
->t('Machine name'),
'#machine_name' => [
'exists' => 'styleguide_machine_name_exists',
'source' => [
'textfield-machine',
],
],
'#description' => $this->generator
->sentence(),
];
$form['textarea'] = [
'#type' => 'textarea',
'#title' => $this
->t('Textarea'),
'#default_value' => $this->generator
->paragraphs(5, TRUE),
'#description' => $this->generator
->sentence(),
];
$form['date'] = [
'#type' => 'date',
'#title' => $this
->t('Date'),
'#description' => $this->generator
->sentence(),
];
$form['file'] = [
'#type' => 'file',
'#title' => $this
->t('File'),
'#description' => $this->generator
->sentence(),
];
$form['managed_file'] = [
'#type' => 'managed_file',
'#title' => $this
->t('Managed file'),
'#description' => $this->generator
->sentence(),
];
$form['password'] = [
'#type' => 'password',
'#title' => $this
->t('Password'),
'#default_value' => $this->generator
->words(),
'#description' => $this->generator
->sentence(),
];
$form['password_confirm'] = [
'#type' => 'password_confirm',
'#title' => $this
->t('Password confirm'),
];
$form['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight'),
'#delta' => 10,
'#description' => $this->generator
->sentence(),
];
$form['details-close'] = [
'#type' => 'details',
'#title' => $this
->t('Details closed'),
'#open' => FALSE,
'#description' => $this->generator
->sentence(),
];
$form['details-open'] = [
'#type' => 'details',
'#title' => $this
->t('Details open'),
'#open' => TRUE,
'#description' => $this->generator
->sentence(),
];
$form['fieldset'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Fieldset'),
'#description' => $this->generator
->sentence(),
];
$elements = [
'fieldset',
'details-close',
'details-open',
];
$count = 0;
foreach ($form as $key => $value) {
if ($value['#type'] != 'fieldset' && $value['#type'] != 'checkbox' && $count < 2) {
$count++;
foreach ($elements as $item) {
$form[$item][$key . '-' . $item] = $value;
}
}
}
$form['vertical_tabs']['elements'] = [
'#type' => 'vertical_tabs',
'#default_tab' => 'fieldset',
];
foreach ($elements as $element) {
$form['vertical_tabs'][$element] = $form[$element];
$form['vertical_tabs'][$element]['#type'] = 'details';
$form['vertical_tabs'][$element]['#group'] = 'elements';
}
$form['markup'] = [
'#markup' => $this
->t('<p><em>Markup</em>: Note that markup does not allow titles or descriptions. Use "item" for those options.</p>') . $this->generator
->paragraphs(1, TRUE),
];
$form['item'] = [
'#type' => 'item',
'#title' => $this
->t('Item'),
'#markup' => $this->generator
->paragraphs(1, TRUE),
'#description' => $this->generator
->sentence(),
];
$form['image_button'] = [
'#type' => 'image_button',
'#src' => 'core/misc/druplicon.png',
'#attributes' => [
'height' => 40,
],
'#name' => $this
->t('Image button'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
$form['button'] = [
'#type' => 'button',
'#value' => $this
->t('Button'),
];
$form['button_disabled'] = [
'#type' => 'button',
'#value' => $this
->t('Button - disabled'),
'#disabled' => TRUE,
];
if ($this->moduleHandler
->moduleExists('filter')) {
$form['text_format'] = [
'#title' => $this->generator
->sentence(),
'#type' => 'text_format',
'#default_value' => $this->generator
->paragraphs(5, TRUE),
'#format' => filter_default_format(),
];
}
if (!empty($form_keys)) {
$items = [];
foreach ($form_keys as $key) {
if (isset($form[$key])) {
$items[$key] = $form[$key];
}
}
return $items;
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// TODO: Implement submitForm() method.
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | Retrieves a configuration object. | |
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. | |
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. | |
StyleguideForm:: |
protected | property | The styleguide generator. | |
StyleguideForm:: |
protected | property | The module handler. | |
StyleguideForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
StyleguideForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
StyleguideForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
StyleguideForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
StyleguideForm:: |
public | function | Constructs a new StyleguideForm object. | |
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. |