I need help with this,I am trying to figure out what would be the best solution and if there could be an easier route. I am using Drupal commerce, plus OG, plus other modules. My question has to do with the product reference field. On a node ( I am using as a product), which reference an entity type product ( I am calling variant), on that node that has a product reference select list I want to filter the select lists items to only that groups related items. On the product entity type I created an entity reference field (called og_group_ref) which relates back to the parent group.
My question is how can I do this in code. I want to do something as follows. I am not great at coding but this is what I have come up so far.
// need to figure out the hook/s for this
function ????($id, $node) {
// Load the currently logged in user.
global $user;
$currentuser = $user;
// load current presaved node id -- that has not been saved
//node_save($node);
$node = node_load($nid);
// load the current presaved nodes = group id
$og_id = current($node->og_groups);
// check if the current node is a node of type product
if ($node->type == 'product') {
// check if the current user is logged -- prob wont need these below
//if ((user_is_logged_in()) && (in_array('brand owner', $user->roles))){
//if (og_is_member('node', $og_id, 'user', $currentuser)){
//Not sure how to how do this
//programmatically loaded all referenced product entities (variant) of the product reference field (field_product_variation)
//$my_entity = reset(entity_load('field_product_variation', array($id)));
//Not sure how to how do this
// Load all product entity of type(variant) of the current users group - filter it per that group. This entity type(variant) uses the entity reference field (og_group_ref) to reference to the parent group.
$nids = array();
$fields = variable_get('og_group_ref', array());
if (isset($fields[$node->type])) {
foreach ($fields[$node->type] as $field) {
$query = new \EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->propertyCondition('status', 1);
$query->fieldCondition($field, 'target_id', $node->nid);
$result = $query->execute();
$nids = isset($result['node']) ? array_merge(array_keys($result['node']), $nids) : $nids;
}
$nodes = (!empty($nids)) ? node_load_multiple($nids) : array();
}
// set the (field_product_variation) select list to the loaded list of current user's group's created products entity (variants)
//Not sure how to how do this
// return the new list in the field_product_variation select list;
}}}
}
- This was originally referenced -- https://www.drupal.org/node/2569843#comment-10340927