Image 使用Drupal Api将图像附加到产品

Image 使用Drupal Api将图像附加到产品,image,api,drupal,product,Image,Api,Drupal,Product,我正试图通过编程将产品添加到我的drupal商业商店。到目前为止,我已经能够添加包含基本信息(如sku、标题和价格)的产品。如何使用drupal的api添加字段数据,如图像和其他字段类型 我用来添加产品的代码来自commerce\u examples/product\u example模块 <?php /** * @file product_example.module * Demonstrates pricing hooks, etc. */ /** * Implements

我正试图通过编程将产品添加到我的drupal商业商店。到目前为止,我已经能够添加包含基本信息(如sku、标题和价格)的产品。如何使用drupal的api添加字段数据,如图像和其他字段类型

我用来添加产品的代码来自commerce\u examples/product\u example模块

<?php
/**
 * @file product_example.module
 * Demonstrates pricing hooks, etc.
 */

/**
 * Implements hook_menu().
 *
 * Simply presents a page that will explain what this module is for.
 * hook_menu() has nothing to do with the checkout page functionality.
 */
function product_example_menu() {
  $items['commerce_examples/product_example'] = array(
    'title' => 'Product Example',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('product_example_info_page'),
    'access callback' => TRUE,
  );
  return $items;
}

/**
 * This form provides a way to interact with some of the example functions
 * provided here.
 */
function product_example_info_page($form, &$form_state) {
  $form['explanation'] = array(
    '#type' => 'item',
    '#markup' => t('This example demonstrates product creation and manipulation.'),
  );

    $form['product_creation'] = array(
      '#type' => 'fieldset',
      '#title' => t('Please create a product for use with this example'),
    );
    $types = commerce_product_types();
    $form['product_creation']['product_type'] = array(
      '#type' => 'select',
      '#title' => t('Product type for product to be created'),
      '#options' => drupal_map_assoc(array_keys($types)),
    );
    $form['product_creation']['title'] = array(
      '#title' => t('Product title'),
      '#type' => 'textfield',
      '#default_value' => t('A dummy product for use with product_example'),
    );
    $form['product_creation']['price'] = array(
      '#title' => t('Product price'),
      '#type' => 'textfield',
      '#description' => t('A price in decimal format, without a currency symbol'),
      '#default_value' => '100.00',
    );
    $form['product_creation']['product_creation_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Create product'),
      '#submit' => array('product_example_product_creation_submit')
    );

  return $form;
}

/**
 * Submit handler for creating a product.
 */
function product_example_product_creation_submit($form, &$form_state) {
  $extras = array(
    'sku' => 'product_example_' . drupal_hash_base64(microtime()),
    'status' => TRUE,
    'uid' => $GLOBALS['user']->uid,
    'title' => $form_state['values']['title'],
  );
  // Use the product example's creation function to create a product.
  $product_id = product_example_create_product($form_state['values']['product_type'], $form_state['values']['price'], $extras);
  drupal_set_message(t('Created sample product with title !title and sku !sku', array('!title' => l($extras['title'], 'admin/commerce/products/' . $product_id), '!sku' => $extras['sku'])));
}

/**
 * Create a product programmatically.
 *
 * This is stolen shamelessly from commerce_bpc. Thanks for the help here!
 *
 * @param $product_type
 *   (string) The name of the product type for which products should be created.
 * @param $price
 *   Decimal amount of price. If additional fields need to be populated they
 *   can be populated in exactly the same way as the commerce_price field.
 * @param $extras
 *   An array for the values of  'extra fields' defined for the product type
 *   entity, or patterns for these. Recognized keys are:
 *   - status
 *   - uid
 *   - sku
 *   - title
 *   Note that the values do NOT come in the form of complex arrays (as they
 *   are not translatable, and can only have single values).
 * @return
 *   The ID of the created product.
 */
function product_example_create_product($product_type, $price, $extras) {
  $form_state = array();
  $form_state['values'] = array();
  $form = array();
  $form['#parents'] = array();

  // Generate a new product object
  $new_product = commerce_product_new($product_type);

  $new_product->status = $extras['status'];
  $new_product->uid = $extras['uid'];

  $new_product->sku = $extras['sku'];
  $new_product->title = $extras['title'];
  $new_product->created = $new_product->changed = time();

  //commerce_price[und][0][amount]
  $price = array(LANGUAGE_NONE => array(0 => array(
    'amount' => $price * 100,
    'currency_code' => commerce_default_currency(),
  )));
  $form_state['values']['commerce_price'] = $price;

  // Notify field widgets to save their field data
  field_attach_submit('commerce_product', $new_product, $form, $form_state);

  commerce_product_save($new_product);
  return $new_product->product_id;
}

我可以添加其他字段数据,但是我仍然无法将数据添加到所有商业产品默认附带的字段\u productimage字段。

我可以向您建议其他字段数据,例如与产品相关的额外信息。您可以从UI将这些字段添加到产品类型中,或者如果您正在以编程方式创建产品类型,则也可以这样做

在表单中添加字段以收集数据,例如-

$form['product_creation']['values'] = array(
      '#title' => t('Some data'),
      '#type' => 'textfield',
      '#description' => t('Dummy data'),
    );
通过
$form\u state()
在函数
product\u示例\u create\u product()
中获取这些数据

将它们添加到$new_产品对象,如

$new_product->field_name['und'][0]['value'] = $extras['values'];
保存产品,就像您正在做的一样

对于图像,您必须遵循文件附件的方法。像-

$file_path = drupal_realpath('image/product_image.png');
  $file = (object) array(
    'uid' => 1,
    'uri' => $file_path,
    'filemime' => file_get_mimetype($file_path),
    'status' => 1,
  );

  // We save the file to the root of the files directory.
  $file = file_copy($file, 'public://');

  $new_product->product_image[LANGUAGE_NONE][0] = (array)$file;

使用此文件附件方法,图像文件将显示在files_managed表中,因此我知道drupal正在拾取它。然而,它并没有附在我的产品上。我相信问题在于最后一行:$new\u product->product\u image[LANGUAGE\u NONE][0]=(array)$file;将图像上传到drupal的公共目录后,如何将图像附加到uc_产品_图像数据字段?
$file_path = drupal_realpath('image/product_image.png');
  $file = (object) array(
    'uid' => 1,
    'uri' => $file_path,
    'filemime' => file_get_mimetype($file_path),
    'status' => 1,
  );

  // We save the file to the root of the files directory.
  $file = file_copy($file, 'public://');

  $new_product->product_image[LANGUAGE_NONE][0] = (array)$file;