1

In my case, I use Drupal Commerce (Kickstart 2x distribution) with a images field (multiples/unlimited values).

I need to display all images of parent product AND images of their childrens (variations).

Another case are to display images of a field but if empty, I want to display images of another images field.

What's the good way for this?

  1. Relation module
  2. hook_field_load()
  3. hook_query_alter()
  4. hook_node_load()
  5. hook_load()

Related questions:

How to create a single slideshow combining several image fields?

0

Here my current solution but I think it is better to use hook_preprocess_field()?

/**
 * Implements hook_node_load().
 */
function MY_MODULE_entity_load($entities, $type){
    if('commerce_product' === $type && !path_is_admin(current_path())){
        foreach($entities as $entity){
            if(in_array($entity->type, array('MY_PRODUCT_TYPE')) && property_exists($entity, 'sku') && !empty($entity->sku)){
                $query = new EntityFieldQuery;
                $query->entityCondition('entity_type', 'node', '=')
                  ->propertyCondition('type', $entity->type)
                  ->fieldCondition('field_product', 'product_id', $entity->product_id, '=')
                  ->range(0, 1);

                if ($result = $query->execute()) {
                    reset($result['node']);
                    $nid = key($result['node']);
                    $parent_product = node_load($result['node'][$nid]->nid);
                    $parent_product_images = field_get_items('node', $parent_product, 'MY_IMAGES_FIELD');
                    if (is_array($parent_product_images)) {
                        $pos = count($entity->field_images['und']);
                        foreach($parent_product_images as $ppi_field){
                            $entity->field_images['und'][$pos++] = $ppi_field;
                        }
                    }
                }
            }
        }
    }
}

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.