You are here

function commerce_cart_form_field_ui_field_edit_form_alter in Commerce Core 7

Implements hook_form_FORM_ID_alter().

Alters the Field UI field edit form to add per-instance settings for fields on product types governing the use of product fields as attribute selection fields on the Add to Cart form.

File

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

Code

function commerce_cart_form_field_ui_field_edit_form_alter(&$form, &$form_state) {

  // Extract the instance info from the form.
  $instance = $form['#instance'];

  // If the current field instance is not locked, is attached to a product type,
  // and of a field type that defines an options list...
  if (empty($form['locked']) && $instance['entity_type'] == 'commerce_product' && function_exists($form['#field']['module'] . '_options_list')) {

    // Get the current instance's attribute settings for use as default values.
    $commerce_cart_settings = commerce_cart_field_instance_attribute_settings($instance);
    $form['instance']['commerce_cart_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Attribute field settings'),
      '#description' => t('Single value fields attached to products can function as attribute selection fields on Add to Cart forms. When an Add to Cart form contains multiple products, attribute field data can be used to allow customers to select a product based on the values of the field instead of just from a list of product titles.'),
      '#weight' => 5,
      '#collapsible' => FALSE,
    );
    $form['instance']['commerce_cart_settings']['attribute_field'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable this field to function as an attribute field on Add to Cart forms.'),
      '#default_value' => $commerce_cart_settings['attribute_field'],
    );
    $form['instance']['commerce_cart_settings']['attribute_widget'] = array(
      '#type' => 'radios',
      '#title' => t('Attribute selection widget'),
      '#description' => t('The type of element used to select an option if used on an Add to Cart form.'),
      '#options' => array(
        'select' => t('Select list'),
        'radios' => t('Radio buttons'),
      ),
      '#default_value' => $commerce_cart_settings['attribute_widget'],
      '#states' => array(
        'visible' => array(
          ':input[name="instance[commerce_cart_settings][attribute_field]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );

    // Determine the default attribute widget title.
    $attribute_widget_title = $commerce_cart_settings['attribute_widget_title'];
    if (empty($attribute_widget_title)) {
      $attribute_widget_title = $instance['label'];
    }
    $form['instance']['commerce_cart_settings']['attribute_widget_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Attribute widget title'),
      '#description' => t('Specify the title to use for the attribute widget on the Add to Cart form.'),
      '#default_value' => $attribute_widget_title,
      '#states' => array(
        'visible' => array(
          ':input[name="instance[commerce_cart_settings][attribute_field]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['field']['cardinality']['#description'] .= '<br />' . t('Must be 1 for this field to function as an attribute selection field on Add to Cart forms.');
  }

  // If the current field instance is not locked and is attached to a product
  // line item type...
  if (empty($form['locked']) && $instance['entity_type'] == 'commerce_line_item' && in_array($instance['bundle'], commerce_product_line_item_types())) {

    // Get the current instance's line item form settings for use as default values.
    $commerce_cart_settings = commerce_cart_field_instance_access_settings($instance);
    $form['instance']['commerce_cart_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Add to Cart form settings'),
      '#description' => t('Fields attached to product line item types can be included in the Add to Cart form to collect additional information from customers in conjunction with their purchase of particular products.'),
      '#weight' => 5,
      '#collapsible' => FALSE,
    );
    $form['instance']['commerce_cart_settings']['field_access'] = array(
      '#type' => 'checkbox',
      '#title' => t('Include this field on Add to Cart forms for line items of this type.'),
      '#default_value' => $commerce_cart_settings['field_access'],
    );
  }
}