3

I want to render a Custom Form from a Controller class. So i've written the code like this

class MyController extends ControllerBase {

  public function content() {

    $form = \Drupal::formBuilder()->getForm('Drupal\MyModule\Form\CustomForm');
    return $form;
  }
}

but i want to put some values in $form_state i.e. set #default_values.

But here $form is a renderable array, not an object so there is no $form_state object. So, what to set default values ?

3

An edit form for what exactly?

getForm() accepts arguments. All additional arguments are passed to the buildForm() method of your form. So you pass whatever you are editing in, and when building the form, you get the current value and set it as #default_value of the form elements.

If you are editing entities, then you should consider using an entity form. I'm maintaining the _entityform_block module, which has the following build() method in it's block, you could do something similar in a controller:

$values = array();
// Specify selected bundle if the entity has bundles.
if (\Drupal::entityTypeManager()->getDefinition($this->configuration['entity_type'])->hasKey('bundle')) {
  $bundle_key = \Drupal::entityTypeManager()->getDefinition($this->configuration['entity_type'])->getKey('bundle');
  $values = array($bundle_key => $this->configuration['bundle']);
}

$entity = \Drupal::entityTypeManager()
  ->getStorage($this->configuration['entity_type'])
  ->create($values);

if ($entity instanceof EntityOwnerInterface) {
  $entity->setOwnerId(\Drupal::currentUser()->id());
}

$form = \Drupal::entityTypeManager()
  ->getFormObject($this->configuration['entity_type'], 'default')
  ->setEntity($entity);
return \Drupal::formBuilder()->getForm($form);
  • I need a 'edit form' like 'node edit' form. If the url is 'mysite/product/edit/15' then i want to open a form which will be filled with the data of 15th product. Data will be fetched from my custom db table. So, in my OP i mentioned how i rendered the empty form but it need to load with that fetched values. how to do this ? – Jasodeep Chatterjee Apr 28 '16 at 6:00
  • I said how you do this. You load your data and pass it to the form. Then you set the default values there and update there. Also, sounds like you should consider to use an entity, then you get a lot of things for free. You just need to define the path and Drupal will handle that URL, controlle and route for you and even build a form for you. – Berdir Apr 28 '16 at 6:22
  • actually, my controller and routing part is working fine, i just need to know how to set default values in the form before it is rendered. D8 configuration won't work here because i will fetch value form DB, it can't be loaded from module.settings.yml – Jasodeep Chatterjee Apr 28 '16 at 6:34
0

I think you should use the same hook_form_alter as in D7. No need to alter the form in controller. FormBuilder::getForm returns renderable form array, not a Form API array.

  • Actually, I'm trying to create a EDIT FORM , is the hook_form_alter is the correct approach, or should I use controller to manage it in D8? – Jasodeep Chatterjee Apr 27 '16 at 14:07
  • Controller is a thing that generates output. It utilizes Render API. To deal with forms you must use Form API, which can be done utilizing form hooks like hook_form_alter or form classes (if you are building your own form). So, yes, this is correct approach. – Stanislav Agapov Apr 27 '16 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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