Build a From from another Form | Drupal 8

Build a From from another Form

Submitted by editor on Thu, 12/24/2015 - 12:53
Question

How to build a From from another form on Drupal 8 ?

Example : (From user login block)

  public function build() {
    $form = \Drupal::formBuilder()->getForm('Drupal\user\Form\UserLoginForm');
    //You can use Form Class (\Drupal\Core\Form\FormBuilderInterface) or The form ID, Like
    //$form = \Drupal::formBuilder()->getForm('user_login_form');

    unset($form['name']['#attributes']['autofocus']);
    // When unsetting field descriptions, also unset aria-describedby attributes
    // to avoid introducing an accessibility bug.
    // @todo Do this automatically in https://www.drupal.org/node/2547063.
    unset($form['name']['#description']);
    unset($form['name']['#attributes']['aria-describedby']);
    unset($form['pass']['#description']);
    unset($form['pass']['#attributes']['aria-describedby']);
    $form['name']['#size'] = 15;
    $form['pass']['#size'] = 15;
    $form['#action'] = $this->url('<current>', [], ['query' => $this->getDestinationArray(), 'external' => FALSE]);
    // Build action links.
    $items = array();
    if (\Drupal::config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
      $items['create_account'] = \Drupal::l($this->t('Create new account'), new Url('user.register', array(), array(
            'attributes' => array(
              'title' => $this->t('Create a new user account.'),
              'class' => array('create-account-link'),
            ),
      )));
    }
    $items['request_password'] = \Drupal::l($this->t('Reset your password'), new Url('user.pass', array(), array(
          'attributes' => array(
            'title' => $this->t('Send password reset instructions via email.'),
            'class' => array('request-password-link'),
          ),
    )));
    return array(
      'user_login_form' => $form,
      'user_links' => array(
        '#theme' => 'item_list',
        '#items' => $items,
      ),
    );
  }

Add new comment