web-dev-qa-db-ja.com

フォームビルダーにパラメーターを渡すにはどうすればよいですか?

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)と互換性がある必要があります

どうして?

17
Crazyrubixfan

パラメータは、routing.ymlとビルドメソッドで同じ名前にする必要があります。また、フォームでパラメーターを使用する場合は、パラメーターリストにnull値を設定する必要があります。

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

最初に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;
}
11
Shreya Shetty