-1

I am trying to use Views Megarow to update my entities inline. My problem is how to refresh the view after I save an entity. This is important especially in a case where the view outputs the title of the node and I update the title, for instance, or in the case where I delete the node and want it removed from the view listing.

The Views Megarow readme refers me to Commerce Backoffice for an example. I see that there is a menu entry as follows:

$items['commerce_backoffice/order/%commerce_order/%'] = array(
  'title callback' => 'commerce_order_ui_order_title',
  'title arguments' => array(2),
  'page callback' => 'commerce_backoffice_order_view',
  'page arguments' => array(2, 3),
  'delivery callback' => 'ajax_deliver',
  'access callback' => 'commerce_order_access',
  'access arguments' => array('view', 2),
);

And then the callback:

function commerce_backoffice_order_view($order, $display_id) {
  $title = commerce_order_ui_order_title($order);
  $view = entity_view('commerce_order', array($order->order_id => $order), 'backoffice');
  $view = $view['commerce_order'][$order->order_id];
  $output = drupal_render($view);

  $return = views_megarow_display($title, $output, $order->order_id);
  if (!empty($view['#megarow_refresh_parent'])) {
    $return['#commands'][] = views_megarow_command_refresh_parent($order->order_id, $display_id);
  }

  return $return;
}

So the commerce order is loaded, then passed with a display id to that callback function. The entity is then created and rendered, and displayed in the megarow, and an Ajax command is returned.

What exactly is the $display_id, and how do I structure the megarow link so that jQuery does the substitution?

I would appreciate it if someone could share a working code example and a conceptual walk-through.

  • 2
    It's not much fun I'm afraid :) Last time I did it (a few months ago) I had to conditionally monkey patch one of the Megarow module's core JS functions to have any chance. Don't have the code on this computer but I'll try to dig it out tonight – Clive Feb 18 '14 at 13:50
  • Seems off-topic as the question asking for code. – Beebee Feb 18 '14 at 14:00
  • @Beebee Totally agree - but the painful memory of cobbling this together (and banging my head against a lot of walls) fills me with sympathy for the OP, I must admit :) – Clive Feb 18 '14 at 14:03
0

I found a quiet simple solution for this "recalculate-confirm-inline-update" UI pattern in Views Megarow forms.

Implemented scenario : Step1: Step1:open quick edit form Step2: Step2:edit formfield, recalculate related field and confirm Step3: Confirmed update

Custom module approach :

function some_form_alter(&$form, &$form_state, &$product) {
 ... 
 $form['#submit'][] = 'some_form_submit';
 $form['#validate'][] = 'some_form_validate';
 ...
}
function some_form_validate(&$form, &$form_state) {
 ...
 $field1_changed = (!empty($form['field1']['#value']) && $form['field1']['#value'] != $form['field1']['#default_value']);
 $field2_changed = (!empty($form['field2']['#value']) && $form['field2']['#value'] != $form['field2']['#default_value']);
 if($field1_changed || $field2_changed) {
  $result = _calculate_field3();
  if($result == $form['field3']['#value']) {
   return;
  }
  $form['field3']['#value'] = $result;
  $form['field3']['#default_value'] = $result;
  form_set_error('field3', t('Field3 has been recalculated. Please confirm with [SAVE]'));
  return;
 }
}
function some_form_submit(&$form, &$form_state) {
 $product = $form_state['product'];
 //apply $product changes
 commerce_product_save($product);
}

The idea is to simulate an update confirm with form_set_error(), which will be skipped on the second validation run when calculation equals previous calculation, saved in $form['field3].

My client needed a flexible solution allowing him to either enter net sale price or have sale price determined by wholesale price + profit margin + VAT.

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.