Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Last active April 16, 2020 17:04
Show Gist options
  • Save jtarleton/d4fb15698d7bbe0e068df7cc8af4ce7c to your computer and use it in GitHub Desktop.
Save jtarleton/d4fb15698d7bbe0e068df7cc8af4ce7c to your computer and use it in GitHub Desktop.
Basic Drupal8 Custom Form
<?php
namespace Drupal\nyc_common\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\nyc_map\Utils\NycLocationServices;
use Drupal\nyc_map\Utils\NycGeometryUtility;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\nyc_map\Ajax\CustomCategoryJsCommand;
use Drupal\nyc_map\Ajax\CustomOnStreetJsCommand;
use Drupal\nyc_map\Ajax\CustomCornerBetweenJsCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Ajax\AnnounceCommand;
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
use Drupal\comment\Entity\Comment;
use Drupal\Component\Utility\SafeMarkup;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Link;
use Drupal\Core\Url;
class NycTestForm extends FormBase {
use NycLocationServices;
public $text = '';
public $nid;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'nyc_test_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $nid = NULL) {
$form['#prefix'] = '<div>';
$form['name'] = [
'#prefix' => '',
'#suffix' => '',
'#required'=>TRUE,
'#attributes'=>[
'aria-label'=>'Name',
'tabindex'=>0,
'class'=>[
'pull-left',
'nyc-map-tabbable'
],
'id' => 'edit-name',
'name' => 'name'
],
'#type' => 'textfield',
'#element_validate' => array(array($this, 'nycTestFormElementValidator')),
'#title' => t('Your name'),
// '#default_value' => '',
'#description' => t('enter your name.')
];
$form['city'] = [
'#prefix' => '',
'#suffix' => '',
'#required'=>TRUE,
'#attributes'=>[
'aria-label'=>'city',
'tabindex'=>0,
'class'=>[
'pull-left',
'nyc-map-tabbable'
],
'id' => 'edit-city',
'name' => 'city'
],
'#type' => 'textfield',
'#element_validate' => array(array($this, 'nycTestFormElementValidator2')),
'#title' => t('Your city'),
// '#default_value' => '',
'#description' => t('enter your name.')
];
$form['actions'] = array('#type' => 'actions');
$form['actions']['button'] = [
'#type' => 'submit',
'#value' => t('SUBMIT'),
'#attributes' => [
'tabindex'=>0,
'class' => [
'nyc-map-tabbable'
//'use-ajax',
]
]
];
$form['#suffix'] = '</div>';
return $form;
}
/**
* Validate handler
* @param array
* @param object
* @return array
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
//@todo Custom validation rules if needed
parent::validateForm($form, $form_state);
return $form;
}
/**
* AJAX callback handler that displays any errors or a success message.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$name = $form_state->getValue('name');
$form_values = $form_state->getValues();
$uid = \Drupal::currentUser()->id();
$name2 = $form_values['name'];
$msg = 'Thanks for submitting ' .$name. $name2;
drupal_set_message($msg, 'status');
}
public static function nycTestFormElementValidator(&$element, FormStateInterface $form_state, &$complete_form) {
$ok = true;
if (!$ok) {
$form_state->setErrorByName('name', 'Not ok');
}
}
public static function nycTestFormElementValidator2(&$element, FormStateInterface $form_state, &$complete_form) {
$ok = true;
if (!$ok) {
$form_state->setErrorByName('city', 'bad city');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment