Forms

Forms

Forms are a very important part of Drupal. They are used to create content, edit content, and even to create users.

Create a form with Ajax Callback

/**
 * @file
 * Contains \Drupal\MODULE_NAME\Form\FormClassName.
 */

namespace Drupal\MODULENAME\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Database\Database;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;

use Drupal\file\Entity\File;


// Create a class that extends from FormBase
class FORMNAME extends FormBase
{

  protected $files;
  /**
   * {@inheritdoc}
   */
  public function getFormId()
  {
    // We give the form an ID
    return 'FORMID';
  }

// Create the required buildForm() function
  public function buildForm(array $form, FormStateInterface $form_state)
  {
    Database::setActiveConnection('external');


// Text
$form['FIELD'] = array(
  '#type' => 'textarea',
  '#title' => t('Titulo:'),
  '#maxlength' => 1000,
  '#required' => TRUE,
  '#default_value' => $result[0]->FIELD_NAME
);
// Date
$form['FIELD'] = array(
  '#type' => 'date',
  '#title' => t('TITLE'),
  '#required' => FALSE,
  '#default_value' => $result[0]->FIELD_NAME
);

// Select
$form['FIELD'] = array(
  '#type' => 'select',
  '#title' => t('TITLE'),
  '#options' => $options,
  '#default_value' => $result[0]->FIELD_NAME,
);
// Number
$form['FIELD'] = array(
  '#type' => 'number',
  '#step' => 'any',
  '#title' => t('TITLE:'),
  '#required' => FALSE,
  '#default_value' => $result[0]->FIELD_NAME
);

// File Upload
$extensions = 'jpg png';
$max_upload = 16000000; // bytes

$form['FIELD'] = [
  '#type' => 'managed_file',
  '#title' => t('TITLE'),
  '#description' => $this->t('Allowed extensions: @allowed_ext', ['@allowed_ext' => $extensions]),
  '#upload_location' => 'public://PATH',
  '#multiple' => FALSE,
  '#required' => FALSE,
  '#default_value' => [$result[0]->FIELD_NAME],
  '#upload_validators' => [
    'file_validate_extensions' => [
      $extensions,
    ],
    'file_validate_size' => [
      $max_upload,
    ],
  ],
];

// Buttons
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
  '#type' => 'submit',
  '#default_value' => $this->t('Save'),
  '#button_type' => 'primary',
];

$form['actions']['delete'] = [
  '#type' => 'button',
  '#value' => t('Delete'),
  '#attributes' => [
    'class' => ['btn', 'btn-danger', 'use-ajax'],
  ],
  '#ajax' => [
    'callback' => '::myAjaxCallback', // don't forget :: when calling a class method.
    //'callback' => [$this, 'myAjaxCallback'], //alternative notation
    'disable-refocus' => FALSE, // Or TRUE to prevent re-focusing on the triggering element.
    // 'event' => 'click',
    'wrapper' => 'drupal-modal', // This element is updated with this AJAX callback.
    'effect' => 'fade',
    'progress' => [
      'type' => 'throbber',
    ],
  ]
];

return $form;
  }

Add a Button with custom submit function

An example of how to add a button to a form and execute a custom function when the button is clicked.

Form field Attributes

These are some of the most common attributes you can use to customize your form fields.

Add classes to form

Add a class to the form element

Add class to form element Label

Add a class to the form element label

Get the element name instead of value

Last updated