You are here

public function MerciBaseTestCase::attachProductReferenceField in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

Attach a product reference field to a given content type. Creates the field if the given name doesn't already exist. Automatically sets the display formatters to be the "add to cart form" for the teaser and full modes.

Parameters

$content_type: Name of the content type that should have a field instance attached.

$field_name: Only used if $attach_product_reference_field is TRUE. Sets the name for the field instance to attach. Creates the field if it doesn't exist.

Return value

An object containing the field instance that was created.

See also

createDummyProductDisplayContentType()

1 call to MerciBaseTestCase::attachProductReferenceField()
MerciBaseTestCase::createDummyProductDisplayContentType in merci_core/tests/merci_base.test
Create a dummy product display content type.

File

merci_core/tests/merci_base.test, line 456
Defines abstract base test class for the Merci module tests.

Class

MerciBaseTestCase
Abstract class for Merci testing. All Merci tests should extend this class.

Code

public function attachProductReferenceField($content_type = 'product_display', $field_name = 'field_product', $cardinality = 1) {
  if (module_exists('commerce_product')) {

    // Check if the field has already been created.
    $field_info = field_info_field($field_name);
    if (empty($field_info)) {

      // Add a product reference field to the product display node type
      $field = array(
        'field_name' => $field_name,
        'type' => 'commerce_product_reference',
        'cardinality' => $cardinality,
        'translatable' => FALSE,
      );
      field_create_field($field);
      $this
        ->pass("New field created: {$field_name}");
    }
    else {
      debug("NOTE: attachProductReferenceField attempting to create field <code>{$field_name}</code> that already exists. This is fine and this message is just for your information.");
    }

    // Check that this instance doesn't already exist
    $instance = field_info_instance('node', $field_name, $content_type);
    if (empty($insance)) {

      // Add an instance of the field to the given content type
      $instance = array(
        'field_name' => $field_name,
        'entity_type' => 'node',
        'label' => 'Product',
        'bundle' => $content_type,
        'description' => 'Choose a product to display for sale.',
        'required' => TRUE,
        'widget' => array(
          'type' => 'options_select',
        ),
        'display' => array(
          'default' => array(
            'label' => 'hidden',
            'type' => 'commerce_cart_add_to_cart_form',
          ),
          'teaser' => array(
            'label' => 'hidden',
            'type' => 'commerce_cart_add_to_cart_form',
          ),
        ),
      );
      field_create_instance($instance);
      $this
        ->pass("Create field instance of field <code>{$field_name}</code> on content type <code>{$content_type}</code>");
    }
    else {
      $this
        ->fail("Test Develoepr: You attempted to create a field that already exists. Field: {$field_name} -- Content Type: {$content_type}");
    }
    return $instance;
  }
  else {
    $this
      ->fail('Cannot create product reference field because Product module is not enabled.');
  }
}