`

entity_metadata_wrapper()

 
阅读更多


https://www.drupal.org/documentation/entity-metadata-wrappers

$user_wrapper = &entity_metadata_wrapper('user', $user);
        $status = $user_wrapper->field_status->value(); //取值
        array_push($status,'b');
        $user_wrapper->field_status->set($status);  // set
        $user_wrapper->save();

 

<?php
$wrapper = entity_metadata_wrapper('node', $node);
   $wrapper->author->mail->value();
  $wrapper->author->mail->set('sepp@example.com') 
?>

 删除数据:

<?php
// Using an empty ->set(NULL) removes the value - without NULL you'll get a PHP notice that set($value) requires 1 parameter.
$wrapper->field_data->set(NULL);

// And handles correctly the deltas when using multiple values fields
$wrapper->field_data[$delta]->set(NULL);
?>

 

多值字段取值:

<?php
$first_name
= $wrapper->field_tags[0]->name->value();
?>

Example using field collections

<?php
   
// Populate the fields.
   
$ewrapper = entity_metadata_wrapper('node', $node);
   
$ewrapper->field_lead_contact_name->set($contact_name);
   
$ewrapper->field_lead_contact_phone->set($contact_phone);
   
$ewrapper->field_lead_contact_email->set($contact_email); 
 
   
// Create the collection entity and set it's "host".
   
$collection = entity_create('field_collection_item', array('field_name' => 'field_facilities_requested'));
   
$collection->setHostEntity('node', $node); 

   

// Now define the collection parameters.
   
$cwrapper = entity_metadata_wrapper('field_collection_item', $collection);
   
$cwrapper->field_facility->set(intval($offset));
   
$cwrapper->save(); 

   

// Save.
   
$ewrapper->save();
?>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics