Skip to main content
Category:

in this tuto, I'll sho you how to alter an existing form in drupal 8.

add classes and prefix, suffix to Form tag

// add prefix to <form> tag
$form['#prefix'] = '<div class="row my-class">';
// add suffix to </form> tag
$form['#suffix'] = '</div>';
//add a class in form tag <form class="col-10 ..."
$form['#attributes']['class'][] = 'col-10';

alter form element : 

delete description, add class to input, add class to label, add class to wrapper and alter title display option.

// delete description from name element
unset($form['name']['#description']);
// add class to input
$form['name']['#attributes']['class'][] = 'form-control';
// change placeholder
$form['name']['#attributes']['placeholder'] = t("Title");
// add class to wrapper
$form['name']['#wrapper_attributes']['class'][] = 'col-12 form-group';
// add class to label
$form['name']['#label_attributes']['class'][] = 'my-form-label';
// invisible label
// $form['name']['#title_display'] = 'invisible';

alter submit buttons : 

// change submit button value
$form['actions']['submit']['#value'] = 'Send';
// add class to submit buttons
$form['actions']['submit']['#attributes']['class'][] = 'my-form-submit';
// add prefix to submit buttons
$form['actions']['submit']['#prefix'] = '<div class="col-12"><div class="form-group">';
// add suffix to submit buttons
$form['actions']['submit']['#suffix'] = '</div></div>';

add new element form and delete existing element : 


// Add a new element checkbox to form.
$form['terms'] = array(
  '#type' => 'checkbox',
  '#title' => t("I accept the terms."),
  '#required' => TRUE,
);

// example how to delete element in form
//unset($form['field_example']);

Full code : 

use Drupal\Core\Form\FormStateInterface;

/**
 * @param $form
 * @param FormStateInterface $form_state
 * @param $form_id
 */
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id)
{
  switch ($form_id) {
    case 'user_login_form':

      // add prefix to <form> tag
      $form['#prefix'] = '<div class="row my-class">';
      // add suffix to </form> tag
      $form['#suffix'] = '</div>';
      //add a class in form tag <form class="col-10 ..."
      $form['#attributes']['class'][] = 'col-10';

      // delete description from name element
      unset($form['name']['#description']);
      // add class to input
      $form['name']['#attributes']['class'][] = 'form-control';
      // change placeholder
      $form['name']['#attributes']['placeholder'] = t("Title");
      // add class to wrapper
      $form['name']['#wrapper_attributes']['class'][] = 'col-12 form-group';
      // add class to label
      $form['name']['#label_attributes']['class'][] = 'my-form-label';
      // invisible label
      // $form['name']['#title_display'] = 'invisible';


      // change submit button value
      $form['actions']['submit']['#value'] = 'Send';
      // add class to submit buttons
      $form['actions']['submit']['#attributes']['class'][] = 'my-form-submit';
      // add prefix to submit buttons
      $form['actions']['submit']['#prefix'] = '<div class="col-12"><div class="form-group">';
      // add suffix to submit buttons
      $form['actions']['submit']['#suffix'] = '</div></div>';

      // Add a new element checkbox to form.
      $form['terms'] = array(
        '#type' => 'checkbox',
        '#title' => t("I accept the terms."),
        '#required' => TRUE,
      );

      // example how to delete element in form
      //unset($form['field_example']);

      break;
  }

}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook