0

I've been trying to do a "simple task" with Drupal 8. Using Ubercart 4.x, I want to totally change the product (add to cart) page.

For that I've created a twig file called node--product.html.twig and I've fully customized it. But the form is not there.

In order to have the form I've added this to my .theme file:

function theme_preprocess_node__product(&$variables) {
  // Creates a AddToCartForm for the given product (with node->id());
  $form_object = new \Drupal\uc_product\Form\AddToCartForm($variables['node']->id());
  $variables['product_form'] = \Drupal::formBuilder()->getForm($form_object, $variables['node']);
}

And now the form is accessible via the product_form variable in the node--product.html.twig file.

But that's where my issue start, I only find two ways of handling the form, either I do this:

{{ form }}

But then I have the form with the "normal" design, or I create it myself by doing something like this:

<form id="{{ product_form['#form_id'] }}" method="{{ product_form['#method'] }}" action="{{ product_form['#action'] }}">
  (...)
</form>

This way works since I can now customize the attributes, but I don't have access to their ID (I'm getting the values from the node--product node and not the form).

So my question is: How can I handle this in order for me to fully customize the product node?

2
  • What happens if you {{ kint(product_form) }}? – user72672 Mar 25 '18 at 20:46
  • Hi were you able to theme the checkout page by any change? – Prerit Mohan Apr 16 '19 at 18:04
0

This might work:

public function MYFUNCTION() {
  $myform = \Drupal::formBuilder()->getForm('Drupal\[mymodule]\Form\[myformclass]');
  // If you want modify the form:
  $myform['field']['#value'] = 'From my controller';

  $build = [
    '#theme' => 'mytemplate'
    '#form' => $myform,
  ];
}

In twig:

{{ form }}

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.