You are here

function uc_product_view_ajax_commands in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 uc_product/uc_product.module \uc_product_view_ajax_commands()

Dynamically replaces parts of a product view based on form input.

If a module adds an input field to the add-to-cart form which affects some aspect of a product (e.g. display price or weight), it should attach an #ajax callback to that form element, and use this function in the callback to build updated content for the affected fields.

Parameters

\Drupal\Core\Ajax\AjaxResponse $response: The response object to add the Ajax commands to.

$form_state: The current form state. This must contain a 'variant' entry in the 'storage' array which represents the product as configured by user input data. In most cases, this is provided automatically by AddToCartForm::validateForm().

$keys: An array of keys in the built product content which should be replaced (e.g. 'display_price').

1 call to uc_product_view_ajax_commands()
uc_attribute_option_ajax in uc_attribute/uc_attribute.module
Ajax callback for attribute selection form elements.

File

uc_product/uc_product.module, line 245
The product module for Ubercart.

Code

function uc_product_view_ajax_commands(AjaxResponse $response, $form_state, $keys) {
  if (\Drupal::config('uc_product.settings')
    ->get('update_node_view') && $form_state
    ->has('variant')) {
    $node_div = '.uc-product-' . $form_state
      ->get('variant')->nid;
    $build = node_view($form_state
      ->get('variant'));
    foreach ($keys as $key) {
      if (isset($build[$key])) {
        $id = $node_div . '.' . str_replace('_', '-', $key);
        $response
          ->addCommand(new ReplaceCommand($id, drupal_render($build[$key])));
      }
    }
  }
}