1

I am having a problem getting a field to print and know it is something I am missing. I am trying to print a product field in a view that is created programmatically. Everything else is working fine, but I cannot pull the item size in to the view. here is the code:

 $form['assigned'] = array(
'#type' => 'fieldset',
'#title' => t('Assigned orders'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$orders = views_get_view('get_drivers_orders');
$orders->set_display('default');
$orders->set_arguments(array('0' => $account->uid));
// change the amount of items to show
$orders->pre_execute();
$orders->execute();
$orders->render();
if ($orders->result) {
foreach ($orders->result as $key => $value) {
  $order = uc_order_load($value->field_driver_id_users_order_id);
  $form['assigned'][$key] = array(
    '#type' => 'fieldset',
    '#title' => t('Order #' . $value->field_driver_id_users_order_id),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['assigned'][$key]['view'] = array(
    '#markup' => l('View order', 'user/' . $account->uid . '/delivery/' . $order->order_id),
  );
  foreach ($order->products as $product_id => $product) {
    $form['assigned'][$key][$product_id]['attribute'] = array(
      '#type' => 'item',
      '#title' => t($product->title),
      '#markup' => isset($product->data['attributes']['What would you like']) ? $product->data['attributes']['What would you like'][0] : '',
    );
    $form['assigned'][$key][$product_id]['size'] = array(
      '#type' => 'item',
      '#title' => t('Size:'),
      '#markup' => isset($product->data['size']) ? $product->data['size'][und][0]['value'] : '', 
    );
  }
  $address = uc_custom_process_get_order_address($order);
  $form['assigned'][$key]['map'] = array(
    '#markup' => l('View map','http://www.google.com/maps?q='.$address.'(tooltip/infowindow title)',array('attributes' => array('target'=>'_blank'))),
  );
  $form['assigned'][$key]['total'] = array(
    '#type' => 'item',
    '#title' => t('Total:'),
    '#markup' => '$' . $order->order_total,
  );
}

Here is an image of the krumo of the the view with the products, I do hope someone can help! the object from krumo in view }

I have looked over the proposed solution and it does not fit my use case. I have a view that shows an individual order, that order has products, and those products have all of there values stored in $product->data. I have tried everything to get information from the 'size' field to display in that view and it is not happening. I am not trying to change a value, just display it! I hope this helps everyone help me! :D

0

So the issues was that Krumo pulled that information of its own accord, and the $order object did not have those details. I had to call $product->nid first, query the DB for the field values i needed, and insert them. I ended up with this code, in case it can help anyone!

    foreach ($order->products as $product_id => $product) {
    $pid = $product->nid;
    $ps = db_query('SELECT field_size_value FROM field_data_field_size WHERE entity_id = :pid', array(':pid'=>$pid))->fetchField();
    $pp = db_query('SELECT field_pack_value FROM field_data_field_pack WHERE entity_id = :pid', array(':pid'=>$pid))->fetchField();
    $form['assigned'][$key][$product_id]['attribute'] = array(
      '#type' => 'item',
      '#title' => t($product->title),
      '#markup' => isset($product->data['attributes']['What would you like']) ? $product->data['attributes']['What would you like'][0] : '',
    );
    if(!empty($ps)){
        $form['assigned'][$key][$product_id]['size'] = array(
      '#type' => 'item',
      '#title' => t('Size:'),
      '#markup' => $ps, 
    );
    }
    if(!empty($pp)){
      $form['assigned'][$key][$product_id]['pack'] = array(
        '#type' => 'item',
        '#title' => t('Pack:'),
        '#markup' => $pp, 
      );
    }
  }

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.