it-swarm-id.com

Bagaimana cara saya membuat entitas dengan bidangnya secara terprogram?

Saya menggunakan Entity API untuk bekerja dengan beberapa entitas, tetapi saya tidak tahu cara membuat entitas baru dengan bidang yang dilampirkan. Saya membuat bundel dan menambahkan beberapa bidang ke bundel itu. Saya ingin melakukan ini:

$a = entity_create(...);

mengembalikan array:

isNew => TRUE,
title => '',
attachedField1 => '',
attachedField2 => '',
etc...

Saya kemudian ingin cukup memperbarui nilai-nilai dan kemudian entity_save () mereka:

isNew => TRUE,
title => 'Title of my entity',
attachedField1 => 'data for attached field 1',
attachedField2 => 'data for attached field 2',
etc...

Apakah ini bisa dilakukan? Jika ya, bagaimana caranya? Saya mengalami kesulitan bekerja dengan entitas.

Inilah info entitas saya:

  $return['k_profile'] = array(
      'label' => t('Profile'),
      'entity class' => 'Entity',
      'controller class' => 'EntityAPIController',
      'base table' => 'k_profile',
      'fieldable' => TRUE,
      'entity keys' => array(
        'id' => 'pid',
        'bundle' => 'type',
        ),
      'bundles' => array(), //added later in hook_entity_info_alter()
      'bundle keys' => array(
        'bundle' => 'type',
        ),
      'label callback' => 'entity_class_label',
      'uri callback' => 'entity_class_uri',
      'access callback' => TRUE,
      'module' => 'k_profile',
    );
5
user1750

Berikut adalah beberapa kode yang membuat produk secara terprogram, termasuk bidang dan properti.

/**
 * Create a product programmatically.
 *
 * This is stolen shamelessly from commerce_bpc. However, I'm not comfortable
 * with the field saving using form api. Seems quite odd.
 *
 * @param $product_type
 *   (string) The name of the product type for which products should be created.
 * @param $values
 *   Keyed array with
 *   - 'price' => actual amount owed on this installment; decimal text like '1.50'
 *   - 'amount_paid' => price amount already paid as a decimal text like '1.50';
 *   - 'original_order' => order id of the original order
 *   - 'original_line_item' => line item id of original line item
 *   - 'original_product => product id of the original product from which the
 *     new product is being created.
 * @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 commerce_installments_create_product($product_type, $values, $extras) {
  $form_state = array();
  $form_state['values'] = $values;
  $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();

  // field_original_order[und][0][target_id]
  $order = array(LANGUAGE_NONE => array(0 => array('target_id' => $values['original_order'])));
  $form_state['values']['field_original_order'] = $order;

  // field_original_line_item[und][0][target_id]
  $line_item = array(LANGUAGE_NONE => array(0 => array('target_id' => $values['original_line_item'])));
  $form_state['values']['field_original_line_item'] = $line_item;

  $product = array(LANGUAGE_NONE => array(0 => array('target_id' => $values['original_product'])));
  $form_state['values']['field_original_product'] = $product;

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

  // field_due_date[und][0][value][date]
  $due_date = array(LANGUAGE_NONE => array(0 => array('date' => $values['due_date'])));
  $form_state['values']['field_due_date'] = $due_date;

  // 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;
}
4
rfay

Bagi mereka yang membuat entitas mereka dengan eck, penjelasannya ada di sini: http://drupal.org/node/1377614

sederhananya, itu sudah cukup untuk menulis

$e = entity_create('type',array());
$e->field_a = ...;
$e->save();
2
mojzis

Jika saya tidak salah, Node Contoh dalam Contoh Proyek tidak hanya apa yang Anda inginkan.

2
rfay

Entitas Model adalah yang Anda butuhkan. Ini adalah starter kit untuk entitas. Anda harus membaca deskripsi modul dan file README sebelum menginstal ini.

Muncul dengan semua yang Anda butuhkan sehingga Anda benar-benar dapat menggunakannya apa adanya. Anda dapat menambahkan bidang, mengelola tampilan, dan yang lainnya.

0
Marius Ilie

Dengan modul API entitas, gunakan entity_create , misalnya:

try {
  $entity_type = 'entity_type';
  $entity = entity_create($entity_type, array('type' => 'type_or_bundle')); 
  $wrapper = entity_metadata_wrapper($entity_type, $entity);
  $wrapper->type = 'some_type'; // When entity type has bundles, you can specify a type.
  $wrapper->isNew = TRUE;
  $wrapper->title = '';
  $wrapper->attachedField1 = '';
  $wrapper->attachedField2 = '';
  $wrapper->some_other_property = 'foo';
  $wrapper->field_some_field = 'bar';
  $wrapper->field_multiple_field = array('foo', 'bar');
  $wrapper->save();
}
catch (EntityMetadataWrapperException $e) {
  watchdog_exception('my_module', $e); 
}

Lihat juga: Drupal 7 - Buat halaman Node dalam Kode yang memberikan contoh berikut:

function my_create_a_node() {
  global $user;

  // entity_create replaces the procedural steps in the first example of
  // creating a new object $node and setting its 'type' and uid property
  $values = array(
    'type' => 'YOUR_NODE_TYPE',
    'uid' => $user->uid,
    'status' => 1,
    'comment' => 1,
    'promote' => 0,
  );
  $entity = entity_create('node', $values);

  // The entity is now created, but we have not yet simplified use of it.
  // Now create an entity_metadata_wrapper around the new node entity
  // to make getting and setting values easier
  $ewrapper = entity_metadata_wrapper('node', $entity);

  // Using the wrapper, we do not have to worry about telling Drupal
  // what language we are using. The Entity API handles that for us.
  $ewrapper->title->set('YOUR TITLE');

  // Setting the body is a bit different from other properties or fields
  // because the body can have both its complete value and its
  // summary
  $my_body_content = 'A bunch of text about things that interest me';
  $ewrapper->body->set(array('value' => $my_body_content));
  $ewrapper->body->summary->set('Things that interest me');

  // Setting the value of an entity reference field only requires passing
  // the entity id (e.g., nid) of the entity to which you want to refer
  // The nid 15 here is just an example.
  $ref_nid = 15;
  // Note that the entity id (e.g., nid) must be passed as an integer not a
  // string
  $ewrapper->field_my_entity_ref->set(intval($ref_nid));

  // Entity API cannot set date field values so the 'old' method must
  // be used
  $my_date = new DateTime('January 1, 2013');
  $entity->field_my_date[LANGUAGE_NONE][0] = array(
     'value' => date_format($my_date, 'Y-m-d'),
     'timezone' => 'UTC',
     'timezone_db' => 'UTC',
   );

  // Now just save the wrapper and the entity
  // There is some suggestion that the 'true' argument is necessary to
  // the entity save method to circumvent a bug in Entity API. If there is
  // such a bug, it almost certainly will get fixed, so make sure to check.
  $ewrapper->save();
}

Untuk membuat entitas baru menggunakan Drupal entitas inti - coba Contoh modul, gunakan contrib Entity API - coba Entitas Model modul.

Untuk lebih banyak contoh, cari di GitHub .

0
kenorb