You are here

class StyleguideForm in Style Guide 8

Same name and namespace in other branches
  1. 2.x src/Form/StyleguideForm.php \Drupal\styleguide\Form\StyleguideForm

Defines a form builder to display form controls in style guide.

Hierarchy

Expanded class hierarchy of StyleguideForm

File

src/Form/StyleguideForm.php, line 14

Namespace

Drupal\styleguide\Form
View 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

Namesort descending Modifiers Type Description Overrides
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::config protected function Retrieves a configuration object.
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.
StyleguideForm::$generator protected property The styleguide generator.
StyleguideForm::$moduleHandler protected property The module handler.
StyleguideForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
StyleguideForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
StyleguideForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
StyleguideForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
StyleguideForm::__construct public function Constructs a new StyleguideForm object.
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.