You are here

function uc_product_add_default_image_field in Ubercart 6.2

Same name and namespace in other branches
  1. 8.4 uc_product/uc_product.module \uc_product_add_default_image_field()
  2. 7.3 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.module
Implements hook_enable().
uc_product_image_defaults in uc_product/uc_product.admin.inc
Sets up imagefield and imagecache for products.
uc_product_kit_enable in uc_product_kit/uc_product_kit.install
Implements hook_enable().

File

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

Code

function uc_product_add_default_image_field($type = NULL) {
  module_load_include('inc', 'imagefield', 'imagefield_widget');
  module_load_include('inc', 'filefield', 'filefield_widget');
  module_load_include('inc', 'content', 'includes/content.crud');
  $label = t('Image');
  $field = array(
    'label' => $label,
    'type' => 'filefield',
    'widget_type' => 'imagefield_widget',
    'weight' => -2,
    'file_extensions' => 'gif jpg png',
    'custom_alt' => 1,
    'custom_title' => 1,
    'description' => '',
    'required' => 0,
    'multiple' => '1',
    'list_field' => '0',
    'list_default' => 1,
    'description_field' => '0',
    'module' => 'filefield',
    'widget_module' => 'imagefield',
    'columns' => array(
      'fid' => array(
        'type' => 'int',
        'not null' => FALSE,
      ),
      'list' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => FALSE,
      ),
      'data' => array(
        'type' => 'text',
        'serialize' => TRUE,
      ),
    ),
    'display_settings' => array(
      'label' => array(
        'format' => 'hidden',
      ),
      'teaser' => array(
        'format' => 'hidden',
      ),
      'full' => array(
        'format' => 'hidden',
      ),
      4 => array(
        'format' => 'hidden',
      ),
    ),
  );
  if (module_exists('imagefield_tokens')) {
    $field['alt'] = '[title]';
    $field['title'] = '[title]';
  }
  if ($type) {

    // Accept single or multiple types as input.
    $types = (array) $type;
  }
  else {
    $types = uc_product_types();
  }
  foreach ($types as $type) {
    $field_name = variable_get('uc_image_' . $type, '');
    if (empty($field_name)) {
      $field_name = 'field_image_cache';
    }

    // Only add the field if it doesn't exist. Don't overwrite any changes.
    $instances = content_field_instance_read(array(
      'field_name' => $field_name,
      'type_name' => $type,
    ));
    if (count($instances) < 1) {
      $prior_instances = content_field_instance_read(array(
        'field_name' => $field_name,
      ));
      if ($prior_instances) {

        // Copy the prior instance.
        content_field_instance_create(array(
          'field_name' => $field_name,
          'type_name' => $type,
        ));
      }
      else {

        // Create a new field and instance.
        $field['field_name'] = $field_name;
        $field['type_name'] = $type;
        content_field_instance_create($field);
      }
      variable_set('uc_image_' . $type, $field_name);
    }
  }
}