I am new to drupal 8. 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.