0

i am building a form in d8 using form builder , what i need to display the html results along with the form .But unlike we display the result using html markup in controller, its not displaying the result however .

in module_name.routing.yml

module_name.findAllUsers:
  path: '/module_name/findusersgroup'
  defaults:
    _form:  '\Drupal\module_name\Form\UserLookUp'
    _title: 'Find users of a Group'
  requirements:
    _permission: 'access admin menus

and in module_name/src/Form/UserLookUp.php

<?php

namespace Drupal\module_name\Form;

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

class UserLookUp extends ConfigFormBase {

  public function getFormId() {
    return 'user_lookup';
  }
/**
 * General form for switching all nodes from one user to another.
 */
  public function buildForm(array $form, FormStateInterface $form_state) {
   $operation = select_usergroup_dropdown_options();

    // two ways to transfer the nid
    $form['operation'] = array(
        '#type' => 'select',
        '#title' => t('Select User group:'),
        '#options' => select_usergroup_dropdown_options(),
        '#description' => t(''),
        '#required' => TRUE,
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Find Users'),
    );
    return $form;
}
/**
 * #submit callback for node_adoption_transfer_form().
 */
  public function submitForm(array &$form, FormStateInterface $form_state) {
//   $form_state = new FormState();
   $form_state->setRebuild();
   //get the dropdown values selected
    $operation = $form_state->getValue('operation');
    $res = '';
   //query the db to get all the users under this group
        $query =  "SELECT vid,uid,init as email,u.name,ctu.field_p_group_name_value as group_name FROM {content_type_usergroup} ctu"
            . " INNER JOIN usergroup_user_map um ON ctu.vid = um.group_id"
            . " LEFT JOIN users_field_data u ON u.uid = um.user_id"
            . " WHERE ctu.vid=$operation"
            . " ORDER BY name";

   if(count(db_query($query)->fetchObject())==0){
       $res='<p style=""> No users found for this group </p>';     
   }
   else {
        $res .= page_1_maketable_3($query);
        #print $res;exit();
        #$form['result'] = array( '#value' => $res, '#type' => 'markup' );
        $build = array(
          '#markup' => $res
        );
       return $build;
   }

  }

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

and in function page_1_maketable_3() am making the html table from the query as

 function page_1_maketable_3($query) {
        $buf = '<form method="post" action="module_name/batchdelete"><table class="inline">';
           $result = db_query($query);
          // Body
          $rowcount = 0;
          while ($row = $result->fetchObject()) {
              $id = $row->vid;
              unset($row->vid);  
          if ($rowcount == 0) {
              $width = sizeof($row);
              // Header
              $buf .= '<tr>';
              foreach($row as $key => $value) {
                $buf .= '<th>' . $key . '</th>';
              }
              if($isedit) {
                  $buf .= '<th>Edit</th>';
                  $buf .= '<th>Delete</th>';
                if($batchop)$buf .= '<th>Select items</th>';          
              }
              if($isaha)$buf .= '<th>Delete</th>';
              $buf .= '</tr>';
            }
            ++$rowcount;;
            $buf .= '<tr>';
            foreach($row as $key => $value) {
              $buf .= '<td>' . $value . '</td>';           
            }    
            //adding one more td with edit button if needed
           if($isedit) {
               $buf .= '<td><a href='.$url.$id.'>edit</a></td>';
               $buf .= '<td><a href='.$urlDelete.$id.'>delete</a></td>';
               $buf .= '<td><input type="checkbox" name="chkbox[]" value='.$id.' ></td>';
           }
           if($isaha) $buf .= '<td><a href='.$urlAhaDelete.$id.'/user/'.$row['uid'].'>delete</a></td>';
           $buf .= '</tr>';
          }

          $buf .= '</table>';
          if($batchop)$buf .= '<input type="submit" name="btchDelete" value="Delete Selected Items" id="batchDelete"></input>';
          $buf .= '</form><br/>';
          return "($rowcount rows)" . $buf;
    }

1 Answer 1

2

The naming module_name/src/Controller/Form/UserLookUp.php looks like you want to mix form and controller. That's not possible.

What you can do is to rebuild the form in submit and put the additional html in buildForm(). There you check $form_state. If it is empty you have a new form and if it is filled with data you have a rebuild. Then you can react on the user input to generate the html that should be displayed along the form.

5
  • sorry typo err !
    – KTM
    Commented May 4, 2016 at 10:00
  • As you can see i given $form_state->setRebuild(); in the submit handler .
    – KTM
    Commented May 4, 2016 at 10:02
  • but the form submit isn't doing much here. Poster is saying to do the logic and check form state in buildForm.
    – Kevin
    Commented May 4, 2016 at 10:14
  • okay , so how it should go the submit handler to display the html result ?
    – KTM
    Commented May 4, 2016 at 13:09
  • 1
    Move the code to display the html from submit to build. The submit handler will then only contain the logic whether to rebuild or submit. Of course you have to provide both possibilities, otherwise the user will never get out of it.
    – 4uk4
    Commented May 4, 2016 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.