【问题标题】:drupal_set_title not working in module formdrupal_set_title 不能以模块形式工作
【发布时间】:2016-02-08 17:22:23
【问题描述】:

我正在尝试学习 Drupal 8。所以现在,我正在“构建”表单。 我创建了一个模块。 在 mymodule\src\Form 里面 我有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;
  }

而且工作... 但是当我想给页面加上标题时,在我的例子中并使用

链接到 my_module.routing.yml, 127.0.0.1/form1
...
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,
    );
...

我收到以下错误:

错误: 致命错误:在第 16 行调用 C:\xampp\htdocs\drupalwebsite\modules\custom\mymodule\src\Form\form1.php 中未定义的函数 Drupal\mymodule\Form\drupal_set_title()

第 16 行是:

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

所以问题出在标题上。我试图解决它,但我不能。 有谁知道为什么? 非常感谢

【问题讨论】:

    标签: drupal drupal-modules drupal-8


    【解决方案1】:

    根据https://www.drupal.org/node/2067859,drupal_set_title() 在 D8 中被移除。您是否尝试过该链接中提到的替代方法,如下所示:

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

    【讨论】:

    • 你成就了我的一天!我不知道为什么我正在阅读的书混合 6/7/8。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2018-08-28
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多