Peter February 2016

drupal_set_title not working in module form

I´m trying to learn Drupal 8. So now, I´m "building" forms. I created a module. INSIDE mymodule\src\Form I have form1.php

namespace Drupal\mymodule\Form;

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

class form1 extends FormBase {

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

  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your name'),
    '#maxlength' => 30,
    );
    $form['Lastname'] = array(
    '#type' => 'textfield',
    '#title' => t('Your lastname'),
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );
    return $form;
  }

And WORKS... But when I want to put a title to the page, in my case and linked to my_module.routing.yml, 127.0.0.1/form1 using

...
public function buildForm(array $form, FormStateInterface $form_state) {

drupal_set_title(t('This is a Title'));

    $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your name'),
    '#maxlength' => 30,
    );
...

I receive the following ERROR:

ERROR: Fatal error: Call to undefined function Drupal\mymodule\Form\drupal_set_title() in C:\xampp\htdocs\drupalwebsite\modules\custom\mymodule\src\Form\form1.php on line 16

An the line 16 is:

drupal_set_title(t('This is a Title'));

So the problem is with the title. I tried to resolve it but I could not. Anyone know why? Thanks a lot

Answers


Brian Showalter February 2016

According to https://www.drupal.org/node/2067859, drupal_set_title() was removed in D8. Have you tried the alternate method that's mentioned in that link, like this:

$form['#title'] = $this->t('This is a Title');

Post Status

Asked in February 2016
Viewed 3,579 times
Voted 14
Answered 1 times

Search




Leave an answer