Thursday, March 22, 2018

Drupal 7: How to print a view with arguments using PHP and Devel module

function yourtheme_pon_denied(&$head_elements) {

  //get user id
  global $user;
  $userId = $user->uid;

  //define argument to apply to view
  $userOneArg = array($userId);
  //set view name (just look at the view edit url)
  $view = views_get_view('commerce_backoffice_user_orders');
  //set view display (just look at the view edit url)
  $view->set_display("user_pon_total_orders");
  //set argument to apply to view
  $view->set_arguments($userOneArg);
  $view->execute();
  //assign view result into an array
  $userOneArg_array = $view->result;

  //show view array structure with the help of Devel module
  dpm($userOneArg_fIds);

}

From this output I am now able to define the "$non_formatted" variable:
  //get row values from view and assign them into the previously defined array variable
  foreach($userOneArg_array as $userOneArg_vResults) {

    $non_formatted = $userOneArg_vResults->field_commerce_order_total[0]['rendered']['#markup'];
    $formatted = str_replace("$","",$non_formatted);
    $formatted_int = floatval($formatted);

    $userOneArg_fIds[] = $formatted_int;

  }

Final result:

function yourtheme_pon_denied(&$head_elements) {

  //get user id
  global $user;
  $userId = $user->uid;

  //define argument to apply to view
  $userOneArg = array($userId);
  //set view name (just look at the view edit url)
  $view = views_get_view('commerce_backoffice_user_orders');
  //set view display (just look at the view edit url)
  $view->set_display("user_pon_total_orders");
  //set argument to apply to view
  $view->set_arguments($userOneArg);
  $view->execute();
  //assign view result into an array
  $userOneArg_array = $view->result;

  //define array to hold raw results
  $userOneArg_fIds = array();

  //get row values from view and assign them into the previously defined array variable
  foreach($userOneArg_array as $userOneArg_vResults) {

    $non_formatted = $userOneArg_vResults->field_commerce_order_total[0]['rendered']['#markup'];
    $formatted = str_replace("$","",$non_formatted);
    $formatted_int = floatval($formatted);

    $userOneArg_fIds[] = $formatted_int;

  }

  //show view array structure with the help of Devel module
  dpm($userOneArg_fIds);

  print array_sum($userOneArg_fIds);

}

No comments:

Post a Comment