如何将参数传递给表单生成器?


15

我在module_name.routing.yml中有以下路由。

module_name.usergroup_delete:
  path: 'module_name/usergroup/delete/{arg1}'
  defaults:
    _form: '\Drupal\module_name\Form\DeleteUserGroup'
    _title: 'Delete User group'
  requirements:
    _permission: 'access admin menus'

这是module_name / src / Form / DeleteUserGroup.php中的代码。

namespace Drupal\module_name\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class DeleteUserGroup extends ConfigFormBase {

  public function getFormId() {
    return 'delete_user_group';
  }
/**
 * General form for switching all nodes from one user to another.
 */
  public function buildForm(array $form, FormStateInterface $form_state,$product_code) {
  $form['_product'] = array(
        '#type' => 'value',
        '#value' => $product_code,);
//get the user group and username to display the confirmation message 
$result = db_query('select field_p_group_name_value from {content_type_usergroup} ctu'       
        . ' where vid=%d',$product_code);      
    while ($row = $result->fetchObject()) {
      $user_group = $row->field_p_group_name_value;
    }

return confirm_form($form,t('Are you sure you want to delete "' .$user_group. '" User Group?'),
        isset($_GET['destination']) ? $_GET['destination'] : "function_name",t('This action cannot be undone.'),t('Delete'),t('Cancel'));

  }
/**
 * #submit callback for node_adoption_transfer_form().
 */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_values = $form_state->getValues();
  //if ($form_state['values']['confirm']) {
    $param = $form_state->getValue('_product');
    drupal_set_message(t('Group ' .$param.' will get deleted.'));               
    db_query('DELETE FROM {content_type_usergroup} WHERE vid = %d', $param);
    //delete the users of the usergroup too
    db_query('DELETE FROM {usergroup_user_map} WHERE group_id=%d', $param);
    drupal_set_message(t('usergroup has been deleted.'));
    drupal_goto('function_name');
  }

  protected function getEditableConfigNames() {
    return "delete_user_group";
  }

}

我收到以下错误:

DeleteUserGroup :: buildForm()必须与Drupal \ Core \ Form \ FormInterface :: buildForm(array $ form,Drupal \ Core \ Form \ FormStateInterface $ form_state)兼容

为什么?


谁能解释使用WebForm模块时的等同性?我没有专门针对使用该模块构建的表单的模块,那么route.yml文件中的“ _form”指向什么?
约翰·科根

Answers:


28

该参数在routing.yml和build方法中必须具有相同的名称。并且在表单中使用参数时,必须在参数列表中设置一个空值:

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

可恢复的致命错误:传递给db_query()的参数2必须是数组类型,给出了字符串
Crazyrubixfan

当我试图在函数中使用arg1时
Crazyrubixfan

1
好的,然后解决此问题的形式参数。buildForm()的调用成功。db_query()遇到新问题,该问题不接受路由中的string参数。
4k4,9

是啊谢谢 。问题出在db_query。现在正在获取参数
Crazyrubixfan '16

11

首先创建一个routing.yml文件

admin_notes.form:
  path: '/example_module/form/{arg}'
  defaults:
    _form: '\Drupal\example_module\Form\ExampleForm'
  requirements:
    _permission: 'access content'

然后在文件夹结构/src/Form/ExampleForm.php下创建一个.php文件,然后构建一个表单

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

             $form['example_note'] = array(
            '#type' => 'textarea',
            '#title' => t('Block contents'),
            '#description' => t('This text will appear in the example block.'),
            '#default_value' => $arg,
        );
       $form['actions'] = ['#type' => 'actions'];
        $form['actions']['delete'] = array(
            '#type' => 'submit',
            '#value' => $this->t('Delete'),
        );

return $form;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.