You are here

function uc_product_add_default_image_field in Ubercart 7.3

Same name and namespace in other branches
  1. 8.4 uc_product/uc_product.module \uc_product_add_default_image_field()
  2. 6.2 uc_product/uc_product.module \uc_product_add_default_image_field()

Creates a file field with an image field widget, and attach it to products.

This field is used by default on the product page, as well as on the cart and catalog pages to represent the products they list. Instances are added to new product classes, and other node types that claim product-ness should call this function for themselves.

Parameters

$type: The content type to which the image field is to be attached. This may be a a single type as a string, or an array of types. If NULL, all product types get an instance of the field.

4 calls to uc_product_add_default_image_field()
uc_product_class_form_submit in uc_product/uc_product.admin.inc
Form submission handler for uc_product_class_form().
uc_product_enable in uc_product/uc_product.install
Implements hook_enable().
uc_product_image_defaults in uc_product/uc_product.admin.inc
Sets up image field for products.
uc_product_kit_enable in uc_product_kit/uc_product_kit.install
Implements hook_enable().

File

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

Code

function uc_product_add_default_image_field($type = NULL) {
  $field = field_info_field('uc_product_image');

  // Set up field if it doesn't exist.
  if (!$field) {
    $field = array(
      'field_name' => 'uc_product_image',
      'type' => 'image',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    );
    field_create_field($field);

    // Initialize this because field_info_field() would have set it.
    $field['bundles'] = array();
  }
  $label = t('Image');
  $new_instance = array(
    'entity_type' => 'node',
    'label' => $label,
    'weight' => -2,
    'widget' => array(
      'type' => 'image_image',
    ),
    'display' => array(
      'full' => array(
        'label' => 'hidden',
        'type' => 'uc_product_image',
      ),
      'teaser' => array(
        'label' => 'hidden',
        'type' => 'uc_product_image',
      ),
    ),
  );
  if ($type) {

    // Accept single or multiple types as input.
    $types = (array) $type;
  }
  else {
    $types = uc_product_types();
  }
  foreach ($types as $type) {
    $new_instance['bundle'] = $type;
    $field_name = variable_get('uc_image_' . $type, '');
    if (empty($field_name)) {
      $field_name = 'uc_product_image';
    }
    $new_instance['field_name'] = $field_name;
    $instance = field_info_instance('node', $field_name, $type);

    // Only add the instance if it doesn't exist. Don't overwrite any changes.
    if (!$instance) {
      field_create_instance($new_instance);
      variable_set('uc_image_' . $type, $field_name);
    }
  }
}