Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit d4b047c1089ff3c89b90702a655ec59d10e8ae1f @amitaibu committed Jan 29, 2012
@@ -0,0 +1,6 @@
+name = "Commerce product subform"
+description = "Create and associate commerce product inside product node."
+core = "7.x"
+php = "5.2.4"
+dependencies[] = "commerce_product"
+dependencies[] = "subform"
@@ -0,0 +1,107 @@
+<?php
+
+/**
+ * @file
+ * Create and associate commerce product inside product node.
+ */
+
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * Add commerce product as subform.
+ */
+function commerce_product_subform_form_product_node_form_alter(&$form, &$form_state) {
+ $node = $form['#node'];
+
+ // commerce_product_ui_product_form expects a commerce product object as
+ // an argument.
+ if (empty($node->nid)) {
+ // Create a new product.
+ $commerce_product = commerce_product_new('product');
+ }
+ else {
+ // Get the existing product, from the product reference field.
+ $wrapper = entity_metadata_wrapper('node', $node);
+ $commerce_product = $wrapper->field_commerce_product_ref->value();
+ }
+
+ form_load_include($form_state, 'inc', 'commerce_product', 'includes/commerce_product.forms');
+ $form['commerce_product_subform'] = array(
+ '#type' => 'subform',
+ '#subform_id' => 'commerce_product_ui_product_form',
+ '#subform_arguments' => array($commerce_product),
+ '#required' => TRUE,
+ '#weight' => 10,
+ );
+
+ // Set the first submit handler to be "subform_submit_all", so the
+ // commerce product will be created, before handing the submitted node
+ // se we can associate the node to the commerce product.
+ // The last submit handler is the original 'node_form_submit', which
+ // will get all the values already populated, and save the node.
+ if (empty($node->nid)) {
+ array_unshift($form['actions']['submit']['#submit'], 'subform_submit_all', 'commerce_product_subform_commerce_product_submit');
+ }
+ else {
+ // The node already exists, so we assume there is already a reference
+ // to the commerce product, so just as "subform_submit_all" submit
+ // handler, to make sure the commerce product can be edited.
+ array_unshift($form['actions']['submit']['#submit'], 'subform_submit_all');
+ }
+
+ // Don't show the commerce product reference, as we have a 1:1
+ // relationship, between the commerce product and node product.
+ $form['field_commerce_product_ref']['#access'] = FALSE;
+}
+
+/**
+ * Submit handler; Set product reference on new product nodes.
+ */
+function commerce_product_subform_commerce_product_submit($form, &$form_state) {
+ // Get the *saved* commerce product from the $form_state.
+ $subform_state = subform_get_state('subform-commerce_product_subform', $form_state);
+ $commerce_product = $subform_state['commerce_product'];
+
+ // Set the form state values of the node, so on "node_form_submit", the
+ // node will be saved with the correct values.
+ $form_state['values']['field_commerce_product_ref'][LANGUAGE_NONE][0]['product_id'] = $commerce_product->product_id;
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * Commerce product form.
+ */
+function commerce_product_subform_form_commerce_product_ui_product_form_alter(&$form, $form_state) {
+ if (!empty($form_state['subform_parent'])) {
+ // We are inside a product node form.
+ $form['title']['#access'] = FALSE;
+ $form['status']['#access'] = FALSE;
+
+ $form['actions']['save_continue']['#access'] = FALSE;
+
+ // We can't deny access to the submit button, as it will prevent the
+ // commerce product form from submitting, so just hide it.
+ $form['actions']['#attributes'] = array('class' => array('element-invisible'));
+
+ $form['#validate'][] = 'commerce_product_subform_form_commerce_product_ui_validate';
+ }
+}
+
+/**
+ * Validate handler; Set the commerce product title as the product node.
+ */
+function commerce_product_subform_form_commerce_product_ui_validate($form, &$form_state) {
+ $parent_state = subform_get_parent_state($form_state);
+ form_set_value($form['title'], $parent_state['values']['title'], $form_state);
+}
+
+/**
+ * Implements hook_ctools_plugin_directory().
+ */
+function commerce_product_subform_ctools_plugin_directory($module, $plugin) {
+ if ($module == 'ctools' && !empty($plugin)) {
+ return "plugins/$plugin";
+ }
+}
@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * Plugin definition.
+ */
+$plugin = array(
+ 'title' => t('Commerce product subform'),
+ 'description' => t('Commerce product form element, as subform.'),
+ 'category' => t('Shopnet'),
+ 'required context' => new ctools_context_required(t('Product node'), 'node'), array(
+ 'form' => array('form'),
+ 'type' => array('product'),
+ ),
+);
+
+/**
+ * Render callback.
+ */
+function shopnet_product_commerce_product_subform_content_type_render($subtype, $conf, $args, $context) {
+ if (empty($context) || empty($context->form)) {
+ return;
+ }
+
+ $module = 'shopnet_product';
+ $block = new stdClass();
+ $block->module = $module;
+ $block->content = $context->form['commerce_product_subform'];
+ unset($context->form['commerce_product_subform']);
+ return $block;
+}
+
+/**
+ * Form callback.
+ */
+function shopnet_product_commerce_product_subform_content_type_edit_form($form, &$form_state) {
+ // provide a blank form so we have a place to have context setting.
+ return $form;
+}

0 comments on commit d4b047c

Please sign in to comment.