data['context']['entity']; $entity = entity_load_single($entity_data['entity_type'], $entity_data['entity_id']); $product_ids = entity_metadata_wrapper($entity_data['entity_type'], $entity)->{$entity_data['product_reference_field_name']}->raw(); // Load all the active products intended for sale on this form. $products = commerce_product_load_multiple($product_ids, array('status' => 1)); // Load all the terms $tids = array_keys($element['#options']); $terms = taxonomy_term_load_multiple($tids); // The radio buttons are displayed based on the term-id's, so we have to // couple the images (in the product) to the term-id's. $color_radio_settings = array(); foreach ($products as $product) { $image_field = field_get_items('commerce_product', $product, 'field_image_color'); $image = !empty($image_field[0]['uri']) ? file_create_url($image_field[0]['uri']) : null; $color_term_field = field_get_items('commerce_product', $product, 'field_product_color'); $tid = !empty($color_term_field[0]['tid']) ? $color_term_field[0]['tid'] : $product->product_id; $title = module_exists('i18n_taxonomy') ? i18n_taxonomy_term_name($terms[$tid]) : $terms[$tid]->name; $color_radio_settings[$tid] = array('title' => $title, 'image' => $image); } // Add class and javascript to radios $count++; $element['#attributes']['class'][] = "pb-color-radios pb-color-radios-$count"; $element['#attached']['js'][] = drupal_get_path('module', 'pbcosmetics') . '/pbcosmetics.js'; $element['#attached']['js'][] = array( 'data' => array('pbCosmetics' => array( "pb-color-radios-$count" => $color_radio_settings, )), 'type' => 'setting', ); } }