Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Add a Form in to a Basic Controller

By editor , 28 November, 2016
Question

How to add a form using FormBase in to a page like ControllerBase ?

NOTE: I don't know this is the best practice, anyway it's working !!!.

Here an Example To add A Exposed filter to a EntityListBuilder.

1. Create the Form using FormBase. (How to create a Form, here ID = my_test_form_id).
2. Create the basic page (or, here EntityListBuilder) (How to create a Simple Page).
3. Insert the form into the page (Tuto).
4. Handle form submit using \Drupal::request()

Example:

//Form
class MyEntityExposedFilters extends FormBase {

  public function getFormId() {
    return 'my_test_form_id';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = array();
    $form['filters']['text'] = array(
      '#title' => $this->t('Text'),
      '#type' => 'textfield',
      '#default_value' => \Drupal::request()->get('text'),
    );
    $form['filters']['submit_apply'] = [
      '#type' => 'submit',
      '#value' => t('Filter'),
    ];

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    //Handle Submit on MyExposedListBuilder.
  }
}


//Page
class MyExposedListBuilder extends EntityListBuilder {

  public function render() {
    //Exposed form
    $form = \Drupal::formBuilder()->getForm('\Drupal\mymodule\Form\MyEntityExposedFilters');
    $build['form'] = $form;

    //Result table
    $build += parent::render();
    return $build;
  }

  /**
   * Loads entity IDs using a pager sorted by the entity id.
   */
  protected function getEntityIds() {
    $form_id = \Drupal::request()->get('form_id');
    if ($form_id && $form_id === 'my_test_form_id') {
      $query = \Drupal::entityQuery($this->entityTypeId);
      $text = \Drupal::request()->get('text');
      $query->condition('text', $text, 'CONTAINS');
      if ($this->limit) {
        $query->pager($this->limit);
      }
      $res = $query->execute();
    }
    else {
      $res = parent::getEntityIds();
    }
    return $res;
  }
}

 

Tags

  • Drupal 8
  • Code
  • Form
  • Drupal 9
Build a From from another Form
Create a Form with Drupal 8 using Form API
Create a simple module for Drupal 8 - Step 2 - Create Simple Page

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer