You are here

function commerce_cart_add_to_cart_form_attributes_refresh in Commerce Core 7

Ajax callback: returns AJAX commands when an attribute widget is changed.

1 string reference to 'commerce_cart_add_to_cart_form_attributes_refresh'
commerce_cart_add_to_cart_form in modules/cart/commerce_cart.module
Builds an Add to Cart form for a set of products.

File

modules/cart/commerce_cart.module, line 2409
Implements the shopping cart system and add to cart features.

Code

function commerce_cart_add_to_cart_form_attributes_refresh($form, $form_state) {
  $commands = array();

  // Render the form afresh to capture any changes to the available widgets
  // based on the latest selection.
  $commands[] = ajax_command_replace('.' . drupal_html_class($form['#form_id']), drupal_render($form));

  // Then render and return the various product fields that might need to be
  // updated on the page.
  if (!empty($form_state['context'])) {
    $product = commerce_product_load($form_state['default_product_id']);
    $form_state['default_product'] = $product;
    $product->display_context = $form_state['context'];

    // First render the actual fields attached to the referenced product.
    foreach (field_info_instances('commerce_product', $product->type) as $product_field_name => $product_field) {

      // Rebuild the same array of classes used when the field was first rendered.
      $replacement_class = drupal_html_class(implode('-', array(
        $form_state['context']['class_prefix'],
        'product',
        $product_field_name,
      )));
      $classes = array(
        'commerce-product-field',
        drupal_html_class('commerce-product-field-' . $product_field_name),
        drupal_html_class('field-' . $product_field_name),
        $replacement_class,
      );
      $element = field_view_field('commerce_product', $product, $product_field_name, $form_state['context']['view_mode']);

      // Add an extra class to distinguish empty product fields.
      if (empty($element)) {
        $classes[] = 'commerce-product-field-empty';
      }

      // Append the prefix and suffix around existing values if necessary.
      $element += array(
        '#prefix' => '',
        '#suffix' => '',
      );
      $element['#prefix'] = '<div class="' . implode(' ', $classes) . '">' . $element['#prefix'];
      $element['#suffix'] .= '</div>';
      $commands[] = ajax_command_replace('.' . $replacement_class, drupal_render($element));
    }

    // Then render the extra fields defined for the referenced product.
    foreach (field_info_extra_fields('commerce_product', $product->type, 'display') as $product_extra_field_name => $product_extra_field) {
      $display = field_extra_fields_get_display('commerce_product', $product->type, $form_state['context']['view_mode']);

      // Only include extra fields that specify a theme function and that
      // are visible on the current view mode.
      if (!empty($product_extra_field['theme']) && !empty($display[$product_extra_field_name]['visible'])) {

        // Rebuild the same array of classes used when the field was first rendered.
        $replacement_class = drupal_html_class(implode('-', array(
          $form_state['context']['class_prefix'],
          'product',
          $product_extra_field_name,
        )));
        $classes = array(
          'commerce-product-extra-field',
          drupal_html_class('commerce-product-extra-field-' . $product_extra_field_name),
          $replacement_class,
        );

        // Build the product extra field to $element.
        $element = array(
          '#theme' => $product_extra_field['theme'],
          '#' . $product_extra_field_name => $product->{$product_extra_field_name},
          '#label' => $product_extra_field['label'] . ':',
          '#product' => $product,
          '#attached' => array(
            'css' => array(
              drupal_get_path('module', 'commerce_product') . '/theme/commerce_product.theme.css',
            ),
          ),
          '#prefix' => '<div class="' . implode(' ', $classes) . '">',
          '#suffix' => '</div>',
        );

        // Add an extra class to distinguish empty fields.
        if (empty($element['#markup'])) {
          $classes[] = 'commerce-product-extra-field-empty';
        }
        $commands[] = ajax_command_replace('.' . $replacement_class, drupal_render($element));
      }
    }
  }

  // Allow other modules to add arbitrary AJAX commands on the refresh.
  drupal_alter('commerce_cart_attributes_refresh', $commands, $form, $form_state);
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}