1

I want to create a .tpl file for each specific product type form. I get that I can create a custom product template and add this code on my theme's template.php:

'commerce_product_ui_product_form' => array(
               'arguments' => array(
                    'form' => NULL,
                    ),
               'template' => 'templates/products form/node--add--product', // set the path here if not in root theme directory
               'render element' => 'form',
            )

The problem is this changes all the product type's form. How can I find the Form ID for each specific product type form?

enter image description here

  • you can do it by using "node--add--yourtype" – Adi Mar 9 '16 at 5:19
  • Hi @Adi Thanks for the fast reply. I will try it, but what will be the code for my theme's template.php, because I noticed that the product type form has only the id="commerce_product_ui_product_form", what ID can I use for each specific product type? For example I have a product type Venue package what will be the ID? – Joshua Abellare Mar 9 '16 at 5:26
  • you dsm in your template then you can find of your product form id – Adi Mar 9 '16 at 5:40
  • @Adi I tried the dsm and like I said, all the product type form has the same ID "commerce_product_ui_product_form", or am I missing something? – Joshua Abellare Mar 9 '16 at 7:01
0

It's the same form ID for every type, but you can find context in the form state, e.g.

function MYMODULE_form_commerce_product_ui_product_form_alter(&$form, &$form_state, $form_id) {
  $product = $form_state['commerce_product'];
  $product_type = commerce_product_type_load($product->type);

  if ($product_type->name == 'foo') {
    // ...
  }
}
  • Thanks @Clive. But if I do it this way I won't be able to create a .tpl file since this is for a custom module. I was wondering if I can add it on my theme's template instead so that I can make my own custom .tpl file because I will be moving a lot of stuff around and adding custom views on the .tpl file. – Joshua Abellare Mar 9 '16 at 9:22
  • Why not just call the theme function something else and if ($product_type->name == 'foo') { $form['#theme'] = 'bar'; }? – Clive Mar 9 '16 at 9:25
  • I get this error whenever I try out your code: "Notice: Trying to get property of non-object in custom_module_form_commerce_product_ui_product_form_alter" I use this code if ($product_type->name == 'caterer_package') { $form['#theme'] = 'templates/product/product-node-form'; } – Joshua Abellare Mar 9 '16 at 10:12
  • Not sure what to tell you I'm afraid, that code is actually copied from the Commerce Product UI module's own code for the same form :/ commerce_product_product_form. If the code is working for them but not you, the problem is likely to be somewhere else – Clive Mar 9 '16 at 10:16

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.