5

I am creating an inventory management system in drupal. I have two different content types: and item and a purchase order.

An item has a name, style code, Colors (this is a field collection with a color name and color code), and a range of sizes (3 to 15 for shoes and XS to 4XL for shirts). Some items don't have colors and some don't have sizes.

In a purchase order, there is a purchase order number and any number of items ordered (a field collection with an entity reference to the item, a field for the number of items ordered, and field for the number of items received). Each item ordered will have one color (one of the colors that the item is available in) and any number of the sizes that was selected in the given item.

My question is how I can populate the color selection (probably a dropdown) with the colors that are in the field collection of the selected item (entity reference). I would also need to populate the sizes list with the sizes that were selected from the item, as well.

I am still pretty new to drupal, and I realize that I may be going about it the wrong way or that I may not have explained everything that someone would need to know to help me. But I am open to new ideas or different solutions. I haven't done much module development, but I would be willing to learn if needed.

3 Answers 3

1

You can try and look at this module. It allows to have corresponding node references and hence if you select something from one node, you can prepopulate the other node as well. http://drupal.org/project/cnr

1

You will probably need to write your own module to handle the content creation with the individual collections. In a bug report on drupal.org, I give a code example that can be used to create a node and field collection (and a field collection within the field collection).

A stripped-down version might look like this:

<?php
$container = entity_create('entity_type', array('type' => 'entity_bundle')); // replace entity_type and entity_bundle with your info

// Set default values for fields in $container, except for the collection,
// which will be done later

// Now save the container
$container->save(); // necessary in order to attach field collections

// Create the collection
$field_collection = entity_create('field_collection_item', array('field_name' => 'field_collection_1')); // replace 'field_collection_1' as appropriate

// Set default values as necessary

// Now save the field_collection
$field_collection->setHostEntity('entity_type', $container); // again, replace entity_type as appropriate
$field_collection->save(TRUE); // calling this with TRUE is important!
}

$container->save(); // must be called again, otherwise the collections will not be added
?>

There's a lot of save() calls in here, but it gets the job done.

1

First let's review a bit the business model of your purchase order Content Type. Let's say there is an item node that represents a T-shirt that is available in green, blue and yellow colors and S, M, L sizes. Now, I want to buy two of those T-shirts, one small size in green color, and one large size, in blue color. So, the purchase order instance/node for this usecase, would have two field collection items:

  1. Item id 3 (let's say 3 is the nid of the T-shirt), color green, size small
  2. Item id 3 (let's say 3 is the nid of the T-shirt), color blue, size large

So, your field collection definition in the purchase order CT should have the following fields

  • Item id (single value field)
  • Color (single value field)
  • Size (single value field)

and given that model, I would also add a single value checkbox labeled 'Item received'. So it becomes apparent, that you need to change your current implementation to the one above, otherwise with your current implementation, a user cannot order the same T-shirt in different color/size combination (if I understood correctly from the details you provided)

Now, the CNR module suggested above is not a good solution, because a) it has been deprecated in favor of CER b) it is used in cases you want to populate bidirectional references between entities (if node A refers to B, then B is aware that it is referenced by A), which is not the usecase here.

Again, if I understood correctly, when a purchase order is placed (somehow in your context), the node create form for purchase order CT appears to the end user. Then, if the user selects an item, there should be color and size dropdown menus for choosing the desired color and size, depending on the underlying referenced item availabilities. Is that correct? Well, if this is the case, then coreyp_1 solution is not applicable either, as it implies that you save the purchase node programmatically. If you change your model to the one suggested above, then you don't have to do so. You will already have the field infrastructure in the purchase order CT and if you manage to pre-populate colors and sizes appropriately while creating a new instance of it, Drupal will handled the saving for you.

So, back to the original question. The solution depends on the field types for Color and Size. Are they taxonomies? Are they plain list items? In any case, the same fields (both for Color and Size) that are originally used for the Item, should be re-used for the purchase order CT as well. And as far as the pre-population is concerned, it can be implemented as suggested here

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.