2

I want to load default values in my form. I am fetching the node of the table row from form list and passing it into route.

my_form_eform:
 path: '/edit/{bar}'
 options:
 parameters:
  bar:
  type: entity:node
 defaults:
  _form: '\Drupal\my_form\Form\EditForm'
  _title: 'Edit Item'
 requirements:
  _permission: 'access content'

and code for editform.php is

namespace Drupal\my_form\Form;

use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\NodeInterface;

class EditForm extends FormBase{


// public function content(AccountInterface $index, Request $request) {

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

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

 // $id = $bar;
//  $result = db_select('my_form','t')  
// ->fields('t',array('id','title','author','stream','description','price'))
// ->condition('id',$id) 
// ->execute()->fetchAssoc();

// $dtitle = $result['title'];
// $dauthor = $result['author'];
// $ddesc = $result['description'];
// $dstream = $result['stream'];
// $dprice = $result['price'];

$form = array();

$form['title'] = [
  '#type' => 'textfield',
  '#title' => t('Title of your book.'),
   '#default_value'=> $dtitle
];
$form['author'] = [
  '#type' => 'textfield',
  '#title' => $this->t('Author.'),
   '#default_value'=> $dauthor
  ];
$form['stream'] = [
  '#type' => 'select',
  '#title' => t('Stream'),
  '#options' => array(
  'ECE' => t('ECE'),
  'CSE' => t('CSE'),
  'ME'=> t('ME'),
),
   '#default_value'=> $dstream
  ];
  $form['desc'] = [
  '#type' => 'textfield',
  '#title' => t('Description'),
   '#default_value'=> $ddesc
  ];
  $form['Price'] = [
  '#type' => 'textfield',
  '#title' => t('Price'),
   '#default_value'=> $dprice,
  ];
$form['ADD'] = [
  '#type' => 'submit',
  '#value' => $this->t('Update'),
];
return $form;

}

I read about parameter upcasting, but its not required for node instance.

If I am passing the id in function itself through account interface it's giving the error

must be compatible with formbuilder class

Now I am using NodeInterface argument but its also giving the error

Argument 3 passed to Drupal\my_form\Form\EditForm::buildForm() must be an instance of Drupal\Core\Form\NodeInterface

Please help. How can I load my id so that default values for form fields can be fetched from db?

0

2 Answers 2

1

You need to make it an optional argument. Annoying but unavoidable workaround with the current design.

And to have it already upcasted to a node, you need to name your variable {node}.

2
  • $url = Url::FromRoute('my_form_eform',array('bar' => $id));i have passed the argument while redirecting from the table t the form page..The problem is how to load the id in the form.
    – ronit
    Commented Jan 18, 2016 at 12:54
  • See updated answer.
    – Berdir
    Commented Jan 18, 2016 at 14:24
0

Your routing definition contains a little mistake: please add 2 spaces before type: entity:node for Drupal to pick it up.

Also, I suggest you to extend ConfigFormBase instead of FormBase. Then, you can use $bar = $this->getRequest()->get('bar') to get the Node object of type \Drupal\node\Entity\Node. You have to drop the 3rd parameter of buildForm.

Because ConfigFormBase is an abstract class, you will also have to add a function body for getEditableConfigNames, e.g.:

protected function getEditableConfigNames() {
  return [];
}

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.