4

I am providing a form to a user to create a product. I want to provide a few fields from a product type, along with some normal Form API input. Example, I only want them to specify the price and SKU. I want the submit handler to take care of creating the title name and any other fields. In this case, a product lives as product entities and as product display nodes. I want to present my user a single form to create and manage products.

I looks like I should be able to do this with the Field API, but I just see how to attached all fields from an entity. I just want to attach a few.

Am I able to do this with the Field API?

EDIT TO ADD MORE INFO:

For example, when a user creates a new product, they will enter a 6 digit SKU. Each product will have that SKU, with another 3 digits appended, specifying a category. I want the user to simply enter the base SKU, like ABCDEF, select category Y and Z, and the submit handler creates the products with the correct SKUs, like ABCDEF-0001, ABCDEF-0002, and creates the node display. There are also specific fields of the product type, so I need the user to populate the fields for these products, using the WYSIWYG filter they would otherwise use on the product create form.

1
  • I think I want the widget of the field, and the ability to validate and collect the field values on submit.
    – vintorg
    Nov 24, 2013 at 3:46

2 Answers 2

10
+50

I think you can certainly use field_attach_form. Take a look at the $options argument that is explained in more detail in the _field_invoke documentation. For more information you can also have a look at this issue: https://drupal.org/node/1825844

You can include any field of any bundle into any form using this approach:

field_attach_form('node', $entity, $form, $form_state, NULL, array(
  'field_name' => 'field_sku',
));

When I last used this approach I remember having passed a copy of the original form into field_attach_form() in order to prevent my original form from being modified to much. Then I have copied only field element into my original form:

$form_tmp = $form;
field_attach_form('node', $entity, $form_tmp, $form_state, NULL, array(
  'field_name' => 'field_sku'
));
$form['field_sku'] = $form_tmp['field_sku'];

Just look at the mentioned functions to see what they do. I'm not sure if this last part is needed.

And one more link to explore:

3
  • I can add multiple fields?
    – vintorg
    Nov 26, 2013 at 7:28
  • Yes, just call field_attach_form() several time.
    – berliner
    Nov 26, 2013 at 9:38
  • Just wanted to say thanks for this answer! Helped me immensely! Oct 7, 2015 at 15:11
1

I think you are looking for Inline Entity Form module

Provides a widget for inline management (creation, modification, removal) of referenced entities. The primary use case is the parent -> children one (product display -> products, order -> line items, etc.), where the child entities are never managed outside the parent form. Existing entities can also be referenced.

1
  • I need to present certain fields from the product display, and certain fields from the products, with their WYSIWYG filters. I don't think this module offers that.
    – vintorg
    Nov 24, 2013 at 17:45

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.