You are here

function commerce_views_display_add_to_cart_form_attributes_refresh in Commerce Views Display 7

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

1 string reference to 'commerce_views_display_add_to_cart_form_attributes_refresh'
commerce_views_display_form_commerce_cart_add_to_cart_form_alter in ./commerce_views_display.module
Implements hook_form_FORM_ID_alter()

File

./commerce_views_display.module, line 59
Provides a views display plugin to render an add to cart form

Code

function commerce_views_display_add_to_cart_form_attributes_refresh($form, $form_state) {
  $commands = array();
  if (empty($form_state['context']['commerce_views_display']['field_displays'])) {
    return commerce_cart_add_to_cart_form_attributes_refresh($form, $form_state);
  }
  $field_displays = $form_state['context']['commerce_views_display']['field_displays'];

  // 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 = $form_state['default_product'];
    $product->display_context = $form_state['context'];

    // First render the actual fields attached to the referenced product.
    $product_field_instances = field_info_instances('commerce_product', $product->type);
    foreach ($field_displays as $field_id => $field_display) {
      if (empty($field_display['is_field']) || empty($field_display['field_name'])) {
        continue;
      }
      $product_field_name = $field_display['field_name'];
      if (!isset($product_field_instances[$product_field_name])) {
        continue;
      }

      // Rebuild the same array of classes used when the field was first rendered.
      $replacement_class = commerce_views_display_field_wrapper_replacement_class('commerce_product', $product, $product_field_name, $field_display);
      $element = commerce_views_display_field_view_field('commerce_product', $product, $product_field_name, $field_display);
      $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,
        );

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