public function save($entity) { $entity = (object) $entity; // Determine if we will be inserting a new entity. $entity->is_new = !(isset($entity->bid) && is_numeric($entity->bid)); $transaction = db_transaction(); // Set the timestamp fields. if (empty($entity->created)) { $entity->created = REQUEST_TIME; } $entity->changed = REQUEST_TIME; field_attach_presave('command_button', $entity); module_invoke_all('entity_presave', $entity, 'command_button'); try { if (!$entity->is_new) { drupal_write_record('command_buttons', $entity, 'bid'); field_attach_update('command_button', $entity); module_invoke_all('entity_update', $entity, 'command_button'); } else { drupal_write_record('command_buttons', $entity); field_attach_insert('command_button', $entity); module_invoke_all('entity_insert', $entity, 'command_button'); } return $entity; } catch (Exception $e) { $transaction->rollback(); watchdog_exception('command_button', $e); } return FALSE; }
/** * Update field */ function _updateFields($nid = NULL) { $node = node_load($nid); if (isset($node->field_product_description['zh-hans'][0]['value'])) { dpm($node->nid . ' have CN'); } else { if (isset($node->field_product_description['en'][0]['value'])) { $node->field_product_description['zh-hans'][0]['value'] = $node->field_product_description['en'][0]['value']; // It's good to call field_attach_presave() field_attach_presave('node', $node); field_attach_update('node', $node); dpm($node->nid . ' Update CN'); } else { dpm($node->nid . ' No English'); } } }
public function save($entity) { $entity = (object) $entity; // Determine if we will be inserting a new entity. $entity->is_new = !(isset($entity->fpid) && is_numeric($entity->fpid)); $transaction = db_transaction(); // Set the timestamp fields. if (empty($entity->created)) { $entity->created = REQUEST_TIME; } // Only change revision timestamp if it doesn't exist. if (empty($entity->timestamp)) { $entity->timestamp = REQUEST_TIME; } $entity->changed = REQUEST_TIME; field_attach_presave('fieldable_panels_pane', $entity); module_invoke_all('entity_presave', $entity, 'fieldable_panels_pane'); // When saving a new entity revision, unset any existing $entity->vid // to ensure a new revision will actually be created and store the old // revision ID in a separate property for entity hook implementations. if (!$entity->is_new && !empty($entity->revision) && $entity->vid) { $entity->old_vid = $entity->vid; unset($entity->vid); $entity->timestamp = REQUEST_TIME; } module_invoke_all('entity_presave', $entity, 'fieldable_panels_pane'); try { if (!$entity->is_new) { // Since we already have an fpid, write the revision to ensure the // vid is the most up to date, then write the record. $this->saveRevision($entity); drupal_write_record('fieldable_panels_panes', $entity, 'fpid'); field_attach_update('fieldable_panels_pane', $entity); module_invoke_all('entity_update', $entity, 'fieldable_panels_pane'); } else { // If this is new, write the record first so we have an fpid, // then save the revision so that we have a vid. This means we // then have to write the vid again. drupal_write_record('fieldable_panels_panes', $entity); $this->saveRevision($entity); db_update('fieldable_panels_panes')->fields(array('vid' => $entity->vid))->condition('fpid', $entity->fpid)->execute(); field_attach_insert('fieldable_panels_pane', $entity); module_invoke_all('entity_insert', $entity, 'fieldable_panels_pane'); } return $entity; } catch (Exception $e) { $transaction->rollback(); watchdog_exception('fieldable_panels_pane', $e); } return FALSE; }
/** * Called to save the final product from the edit form. */ function edit_save_form($form_state) { $item =& $form_state['item']; $export_key = $this->plugin['export']['key']; $result = ctools_export_crud_save($this->plugin['schema'], $item); field_attach_submit('ms_products_plan', $item, $form_state['saved_form'], $form_state); field_attach_update('ms_products_plan', $item); if ($result) { $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['success']); drupal_set_message($message); } else { $message = str_replace('%title', check_plain($item->{$export_key}), $this->plugin['strings']['confirmation'][$form_state['op']]['fail']); drupal_set_message($message, 'error'); } }
/** * _entity_create_node */ function _entity_create_node($row = NULL, $brand_tid = NULL, $depend_chassis) { global $user; $type = NULL; $depend_chassis_boolean = FALSE; switch ($row[1]) { case 'U': $type = 'chassis'; break; case 'C': $type = 'card'; $depend_chassis_boolean = TRUE; break; case 'S': $type = 'software'; $depend_chassis_boolean = TRUE; break; default: break; } // creating a new object $node and setting its 'type' and uid property $values = array('type' => $type, 'uid' => $user->uid, 'status' => 1, 'promote' => 0); $entity = entity_create('node', $values); // Using the wrapper $node_wrapper = entity_metadata_wrapper('node', $entity); $node_wrapper->language->set('en'); $node_wrapper->title->set($row[0]); if ($brand_tid) { $node_wrapper->field_product_brand->set($brand_tid); // brand term } if ($row[4]) { $node_wrapper->field_product_description->set($row[4]); // English description } if ($row[5]) { $node_wrapper->language('zh-hans')->field_product_description->set($row[5]); // zh-hans Chinese description } else { if ($row[4]) { $node_wrapper->language('zh-hans')->field_product_description->set($row[4]); // English description } } if ($row[6]) { $node_wrapper->field_product_reference_price->set(trim($row[6])); } if ($row[7]) { $node_wrapper->field_product_unit_price->set($row[7]); } if ($depend_chassis_boolean) { if ($depend_chassis) { $node_wrapper->field_product_depend_chassis->set($depend_chassis); } } $node_wrapper->save(); dpm('save node - ' . $row[0]); // get node nid we just created $nid = $node_wrapper->getIdentifier(); /** * update translations->data['zh-hans'] */ $translation = array('translate' => 0, 'status' => 1, 'language' => 'zh-hans', 'source' => 'en'); $node = node_load($nid); // If you've called node_save($node) before, you'll have the node object already and you can omit this $handler = entity_translation_get_handler('node', $node); // $values['name_field']['sv']['0']['value'] = $sv_name; // You can write the array like this, I find it easier to read $handler->setTranslation($translation); field_attach_update('node', $node); }
/** * given a drupal account, provision an ldap entry if none exists. if one exists do nothing * * @param object $account drupal account object with minimum of name property * @param array $ldap_user as prepopulated ldap entry. usually not provided * * @return array of form: * array('status' => 'success', 'fail', or 'conflict'), * array('ldap_server' => ldap server object), * array('proposed' => proposed ldap entry), * array('existing' => existing ldap entry), * array('description' = > blah blah) * */ public function provisionLdapEntry($account, $ldap_user = NULL, $test_query = FALSE) { //debug('provisionLdapEntry account'); //debug($account); $watchdog_tokens = array(); $result = array('status' => NULL, 'ldap_server' => NULL, 'proposed' => NULL, 'existing' => NULL, 'description' => NULL); if (is_scalar($account)) { $username = $account; $account = new stdClass(); $acount->name = $username; } list($account, $user_entity) = ldap_user_load_user_acct_and_entity($account->name); if (is_object($account) && property_exists($account, 'uid') && $account->uid == 1) { $result['status'] = 'fail'; $result['error_description'] = 'can not provision drupal user 1'; return $result; // do not provision or synch user 1 } if ($account == FALSE || $account->uid == 0) { $result['status'] = 'fail'; $result['error_description'] = 'can not provision ldap user unless corresponding drupal account exists first.'; return $result; } if (!$this->ldapEntryProvisionServer || !$this->ldapEntryProvisionServer) { $result['status'] = 'fail'; $result['error_description'] = 'no provisioning server enabled'; return $result; } $ldap_server = ldap_servers_get_servers($this->ldapEntryProvisionServer, NULL, TRUE); $params = array('direction' => LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY, 'prov_events' => array(LDAP_USER_EVENT_CREATE_LDAP_ENTRY), 'module' => 'ldap_user', 'function' => 'provisionLdapEntry', 'include_count' => FALSE); list($proposed_ldap_entry, $error) = $this->drupalUserToLdapEntry($account, $ldap_server, $params, $ldap_user); $proposed_dn = is_array($proposed_ldap_entry) && isset($proposed_ldap_entry['dn']) && $proposed_ldap_entry['dn'] ? $proposed_ldap_entry['dn'] : NULL; $proposed_dn_lcase = drupal_strtolower($proposed_dn); $existing_ldap_entry = $proposed_dn ? $ldap_server->dnExists($proposed_dn, 'ldap_entry') : NULL; if ($error == LDAP_USER_PROV_RESULT_NO_PWD) { $result['status'] = 'fail'; $result['description'] = 'Can not provision ldap account without user provided password.'; $result['existing'] = $existing_ldap_entry; $result['proposed'] = $proposed_ldap_entry; $result['ldap_server'] = $ldap_server; } elseif (!$proposed_dn) { $result['status'] = 'fail'; $result['description'] = t('failed to derive dn and or mappings'); return $result; } elseif ($existing_ldap_entry) { $result['status'] = 'conflict'; $result['description'] = 'can not provision ldap entry because exists already'; $result['existing'] = $existing_ldap_entry; $result['proposed'] = $proposed_ldap_entry; $result['ldap_server'] = $ldap_server; } elseif ($test_query) { $result['status'] = 'fail'; $result['description'] = 'not created because flagged as test query'; $result['proposed'] = $proposed_ldap_entry; $result['ldap_server'] = $ldap_server; } else { // stick $proposed_ldap_entry in $ldap_entries array for drupal_alter call $ldap_entries = array($proposed_dn_lcase => $proposed_ldap_entry); $context = array('action' => 'add', 'corresponding_drupal_data' => array($proposed_dn_lcase => $account), 'corresponding_drupal_data_type' => 'user'); drupal_alter('ldap_entry_pre_provision', $ldap_entries, $ldap_server, $context); // remove altered $proposed_ldap_entry from $ldap_entries array $proposed_ldap_entry = $ldap_entries[$proposed_dn_lcase]; $ldap_entry_created = $ldap_server->createLdapEntry($proposed_ldap_entry, $proposed_dn); if ($ldap_entry_created) { module_invoke_all('ldap_entry_post_provision', $ldap_entries, $ldap_server, $context); $result['status'] = 'success'; $result['description'] = 'ldap account created'; $result['proposed'] = $proposed_ldap_entry; $result['created'] = $ldap_entry_created; $result['ldap_server'] = $ldap_server; // need to store <sid>|<dn> in ldap_user_prov_entries field, which may contain more than one $ldap_user_prov_entry = $ldap_server->sid . '|' . $proposed_ldap_entry['dn']; if (!isset($user_entity->ldap_user_prov_entries['und'])) { $user_entity->ldap_user_prov_entries = array('und' => array()); } $ldap_user_prov_entry_exists = FALSE; foreach ($user_entity->ldap_user_prov_entries['und'] as $i => $field_value_instance) { if ($field_value_instance == $ldap_user_prov_entry) { $ldap_user_prov_entry_exists = TRUE; } } if (!$ldap_user_prov_entry_exists) { $user_entity->ldap_user_prov_entries['und'][] = array('value' => $ldap_user_prov_entry); // Save the field without calling user_save() field_attach_presave('user', $user_entity); field_attach_update('user', $user_entity); } } else { $result['status'] = 'fail'; $result['proposed'] = $proposed_ldap_entry; $result['created'] = $ldap_entry_created; $result['ldap_server'] = $ldap_server; $result['existing'] = NULL; } } $tokens = array('%dn' => isset($result['proposed']['dn']) ? $result['proposed']['dn'] : NULL, '%sid' => isset($result['ldap_server']) && $result['ldap_server'] ? $result['ldap_server']->sid : 0, '%username' => @$account->name, '%uid' => @$account->uid, '%description' => @$result['description']); if (!$test_query && isset($result['status'])) { if ($result['status'] == 'success') { if ($this->detailedWatchdog) { watchdog('ldap_user', 'LDAP entry on server %sid created dn=%dn. %description. username=%username, uid=%uid', $tokens, WATCHDOG_INFO); } } elseif ($result['status'] == 'conflict') { if ($this->detailedWatchdog) { watchdog('ldap_user', 'LDAP entry on server %sid not created because of existing ldap entry. %description. username=%username, uid=%uid', $tokens, WATCHDOG_WARNING); } } elseif ($result['status'] == 'fail') { watchdog('ldap_user', 'LDAP entry on server %sid not created because error. %description. username=%username, uid=%uid', $tokens, WATCHDOG_ERROR); } } return $result; }
/** * Given information about an entity, sets a field value. * * @param string $entity_type The entity type. * @param object $stub_entity The stub entity. * @param string $field_name The name of the field to set. * @param int $delta The delta (index) of the value to set. * @param mixed $value The value itself. */ protected function setFieldValue($entity_type, $stub_entity, $field_name, $delta, $value) { $language = field_language($entity_type, $stub_entity, $field_name); $stub_entity->{$field_name}[$language][$delta] = $value; field_attach_update($entity_type, $stub_entity); }
<?php // Get all nodes of 'ereference' and 'databases' type, populate the proxy field with a value of '1' if there is no field currently. $eresource = db_query("SELECT nid, vid, type FROM {node} WHERE type = 'ereference' OR type = 'databases'")->fetchAllAssoc('nid'); foreach ($eresource as $nid => $values) { $key = array('entity_id' => $values->nid); // fields based on existing nodes $fields = array('entity_type' => 'node', 'bundle' => $values->type, 'deleted' => 0, 'entity_id' => $values->nid, 'revision_id' => $values->vid, 'language' => 'und', 'delta' => 0, 'field_proxy_value' => '1'); // only insert missing fields, don't update existing fields. db_merge('field_data_field_proxy')->key($key)->insertFields($fields)->execute(); db_merge('field_revision_field_proxy')->key($key)->insertFields($fields)->execute(); // Save fields, use actual node object $node = node_load($values->nid); field_attach_presave('node', $node); field_attach_update('node', $node); entity_get_controller('node')->resetCache($key); }
public function save($entity) { $entity = (object) $entity; // Determine if we will be inserting a new entity. $entity->is_new = !(isset($entity->fpid) && is_numeric($entity->fpid)); $transaction = db_transaction(); // Load the stored entity, if any. if (!empty($entity->fpid) && !isset($entity->original)) { $entity->original = entity_load_unchanged('fieldable_panels_pane', $entity->fpid); } // Set the timestamp fields. if (empty($entity->created)) { $entity->created = REQUEST_TIME; } // Only change revision timestamp if it doesn't exist. if (empty($entity->timestamp)) { $entity->timestamp = REQUEST_TIME; } $entity->changed = REQUEST_TIME; field_attach_presave('fieldable_panels_pane', $entity); // Trigger hook_fieldable_panels_pane_presave(). module_invoke_all('fieldable_panels_pane_presave', $entity); // Trigger hook_entity_presave_update(). module_invoke_all('entity_presave', $entity, 'fieldable_panels_pane'); // When saving a new entity revision, unset any existing $entity->vid // to ensure a new revision will actually be created and store the old // revision ID in a separate property for entity hook implementations. if (!$entity->is_new && !empty($entity->revision) && $entity->vid) { $entity->old_vid = $entity->vid; unset($entity->vid); $entity->timestamp = REQUEST_TIME; } try { // Since we already have an fpid, write the revision to ensure the vid is // the most up to date, then write the record. if (!$entity->is_new) { $this->saveRevision($entity); drupal_write_record('fieldable_panels_panes', $entity, 'fpid'); field_attach_update('fieldable_panels_pane', $entity); // Trigger hook_fieldable_panels_pane_update(). module_invoke_all('fieldable_panels_pane_update', $entity); // Trigger hook_entity_update(). module_invoke_all('entity_update', $entity, 'fieldable_panels_pane'); } else { drupal_write_record('fieldable_panels_panes', $entity); $this->saveRevision($entity); db_update('fieldable_panels_panes')->fields(array('vid' => $entity->vid))->condition('fpid', $entity->fpid)->execute(); field_attach_insert('fieldable_panels_pane', $entity); // Trigger hook_fieldable_panels_pane_insert(). module_invoke_all('fieldable_panels_pane_insert', $entity); // Trigger hook_entity_insert(). module_invoke_all('entity_insert', $entity, 'fieldable_panels_pane'); } // Clear the appropriate caches for this object. entity_get_controller('fieldable_panels_pane')->resetCache(array($entity->fpid)); return $entity; } catch (Exception $e) { $transaction->rollback(); watchdog_exception('fieldable_panels_pane', $e); } return FALSE; }
/** * A lightweight version of entity save for field values only. * * @param string $entity_type * The entity type of $entity. * @param object $entity * The entity object to update. */ public static function updateFieldValues($entity_type, $entity) { list($id) = entity_extract_ids($entity_type, $entity); if (empty($id)) { throw new InvalidArgumentException(t('Cannot call EntityHelper::updateFieldValues() on an unsaved entity.')); } // Some modules use the original property. if (!isset($entity->original)) { $entity->original = $entity; } // Ensure that file_field_update() will not trigger additional usage. unset($entity->revision); // Invoke the field presave and update hooks. field_attach_presave($entity_type, $entity); field_attach_update($entity_type, $entity); // Clear the cache for this entity now. entity_get_controller($entity_type)->resetCache(array($id)); }
public function save() { field_attach_presave('ms_products_purchase', $this); // Update or insert a new purchase. if (!is_null($this->id)) { drupal_write_record('ms_products_purchases', $this, 'id'); field_attach_update('ms_products_purchase', $this); } else { drupal_write_record('ms_products_purchases', $this); field_attach_insert('ms_products_purchase', $this); } // Next, save the options. if (!empty($this->options)) { foreach ($this->options as $option_info) { if (isset($option_info->id)) { if ($option_info == 'inactive') { db_delete('ms_products_purchase_options')->condition('id', $option_info->id)->execute(); } else { // Update the existing record. drupal_write_record('ms_products_purchase_options', $option_info, 'id'); } } elseif ($option_info->status != 'inactive') { // Create a new record. $option_info->pid = $this->id; drupal_write_record('ms_products_purchase_options', $option_info); } } } }
function smv_sitecron_archive_payment_details($ponumber, $vendorno) { if (!isset($ponumber) || !isset($vendorno)) { return FALSE; } $nid = smv_sitefeeds_paymentdetails_nid($ponumber); if ($nid) { $node = node_load($nid); $fids = db_query('SELECT csv.field_pd_csv_fid as csv_fid FROM {node} n LEFT JOIN {field_data_field_pd_csv} csv ON csv.entity_id = n.nid WHERE n.nid = :nid', array('nid' => $nid))->fetchAll(); if ($fids) { foreach ($fids as $fid) { if ($fid->csv_fid) { smv_sitecron_move_to_vendor_archive_folder($fid->csv_fid, $vendorno); $node->field_pd_csv_filepath[LANGUAGE_NONE][0]['value'] = 'sm_vendor/vendors/' . $vendorno . '/archive'; } } } $node->field_pd_archive[LANGUAGE_NONE][0]['value'] = 1; field_attach_update('node', $node); entity_get_controller('node')->resetCache(array($node->nid)); } }