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; }
/** * @see LTIToolProviderOutcomesResourceEntityControllerInterface::save */ public function save($entity) { $transaction = db_transaction(); try { $entity->is_new = empty($enity->lti_tool_provider_outcomes_resource_id); if (empty($entity->lti_tool_provider_outcomes_resource_timestamp_created)) { $entity->lti_tool_provider_outcomes_resource_timestamp_created = REQUEST_TIME; } field_attach_presave('lti_tool_provider_outcomes_resource', $entity); $primary_key = $entity->lti_tool_provider_outcomes_resource_id ? 'lti_tool_provider_outcomes_resource_id' : array(); if (empty($primary_key)) { drupal_write_record('lti_tool_provider_outcomes_resource', $entity); field_attach_insert('lti_tool_provider_outcomes_resource', $entity); $op = 'insert'; } else { drupal_write_record('lti_tool_provider_outcomes_resource', $entity, $primary_key); $op = 'update'; } $function = 'field_attach_' . $op; $function('lti_tool_provider_outcomes_resource', $entity); module_invoke_all('entity_' . $op, $entity, 'lti_tool_provider_outcomes_resource'); unset($entity->is_new); db_ignore_slave(); return $entity; } catch (Exception $e) { $transaction->rollback(); drupal_set_message(t('%e', array('%e' => $entity->{$e}))); watchdog_exception('lti_tool_provider_outcomes_resource', $e, NULL, WATCHDOG_ERROR); return FALSE; } }
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; }
/** * 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'); } } }
/** * Implements edit_form_submit(). */ function edit_form_submit(&$form, &$form_state) { // Reformat the variables, attachments and concat args to fit the storage. $form_state['values']['variables'] = HeartbeatMessageTemplate::getVariablesFromMessage($form_state['values']['message'], $form_state['values']['message_concat']); $form_state['values']['attachments'] = empty($form_state['values']['attachments']) ? array() : $form_state['values']['attachments']; $form_state['values']['concat_args']['roles'] = $form_state['values']['roles']; $form_state['item']->message_id = $form_state['values']['message_id']; field_attach_presave('heartbeat_activity', $form_state['item']); module_invoke_all('entity_presave', $form_state['item'], 'heartbeat_activity'); // Let CTools prepare the "item" variable as normal. parent::edit_form_submit($form, $form_state); // Main task it's doing: // $form_state['item']->{$key} = $form_state['values'][$key]; drupal_set_message(t('Heartbeat streams cache has been cleared and menu is rebuild.')); }
function api_node_save($node) { $transaction = db_transaction(); try { // Load the stored entity, if any. if (!empty($node->nid) && !isset($node->original)) { $node->original = entity_load_unchanged('node', $node->nid); } field_attach_presave('node', $node); global $user; // Determine if we will be inserting a new node. if (!isset($node->is_new)) { $node->is_new = empty($node->nid); } /* // Set the timestamp fields. if (empty($node->created)) { $node->created = REQUEST_TIME; } // The changed timestamp is always updated for bookkeeping purposes, // for example: revisions, searching, etc. $node->changed = REQUEST_TIME; $node->timestamp = REQUEST_TIME; */ $update_node = TRUE; // Let modules modify the node before it is saved to the database. module_invoke_all('node_presave', $node); module_invoke_all('entity_presave', $node, 'node'); if ($node->is_new || !empty($node->revision)) { // When inserting either a new node or a new node revision, $node->log // must be set because {node_revision}.log is a text column and therefore // cannot have a default value. However, it might not be set at this // point (for example, if the user submitting a node form does not have // permission to create revisions), so we ensure that it is at least an // empty string in that case. // @todo: Make the {node_revision}.log column nullable so that we can // remove this check. if (!isset($node->log)) { $node->log = ''; } } elseif (!isset($node->log) || $node->log === '') { // If we are updating an existing node without adding a new revision, we // need to make sure $node->log is unset whenever it is empty. As long as // $node->log is unset, drupal_write_record() will not attempt to update // the existing database column when re-saving the revision; therefore, // this code allows us to avoid clobbering an existing log entry with an // empty one. unset($node->log); } // When saving a new node revision, unset any existing $node->vid so as to // ensure that a new revision will actually be created, then store the old // revision ID in a separate property for use by node hook implementations. if (!$node->is_new && !empty($node->revision) && $node->vid) { $node->old_vid = $node->vid; unset($node->vid); } // Save the node and node revision. if ($node->is_new) { // For new nodes, save new records for both the node itself and the node // revision. drupal_write_record('node', $node); _node_save_revision($node, $user->uid); $op = 'insert'; } else { // For existing nodes, update the node record which matches the value of // $node->nid. drupal_write_record('node', $node, 'nid'); // Then, if a new node revision was requested, save a new record for // that; otherwise, update the node revision record which matches the // value of $node->vid. if (!empty($node->revision)) { _node_save_revision($node, $user->uid); } else { _node_save_revision($node, $user->uid, 'vid'); $update_node = FALSE; } $op = 'update'; } if ($update_node) { db_update('node')->fields(array('vid' => $node->vid))->condition('nid', $node->nid)->execute(); } // Call the node specific callback (if any). This can be // node_invoke($node, 'insert') or // node_invoke($node, 'update'). node_invoke($node, $op); // Save fields. $function = "field_attach_{$op}"; $function('node', $node); module_invoke_all('node_' . $op, $node); module_invoke_all('entity_' . $op, $node, 'node'); // Update the node access table for this node. node_access_acquire_grants($node); // Clear internal properties. unset($node->is_new); unset($node->original); // Clear the static loading cache. entity_get_controller('node')->resetCache(array($node->nid)); // Ignore slave server temporarily to give time for the // saved node to be propagated to the slave. db_ignore_slave(); } catch (Exception $e) { $transaction->rollback(); watchdog_exception('node', $e); throw $e; } }
/** * 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; }
<?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); } } } }