public function PartyController::setPrimaryFields in Party 7
Same name and namespace in other branches
- 8.2 includes/party.entity.inc \PartyController::setPrimaryFields()
Set the primary fields for the party.
Parameters
$party: A party object.
bool|int $store: Whether we should store any changes. TRUE means yes, FALSE means no. An integer comprised of the update flags (see return) can be used to specify whether to run specific stores.
Return value
int Whether changes requiring a store were made. This is a flag which can contain:
1 call to PartyController::setPrimaryFields()
- PartyController::invoke in includes/
party.entity.inc - Overrides EntityAPIController::invoke().
File
- includes/
party.entity.inc, line 96 - Contains the controller classes for Party entities.
Class
- PartyController
- The API controller class for the Party entity.
Code
public function setPrimaryFields($party, $store = TRUE) {
$needs_store = 0;
// Convert a TRUE into all our flags.
if ($store === TRUE) {
$store = PartyController::UPDATE_PROPERTY | PartyController::UPDATE_FIELDS | PartyController::UPDATE_OTHER;
}
// Find all primary fields and get their values.
$party_wrapper = entity_metadata_wrapper('party', $party);
foreach (PartyPrimaryFields::getFields() as $target => $sources) {
// Get the original value.
$original_value = $party_wrapper->{$target}
->value();
$final_value = NULL;
// Find the first one with a valid value.
foreach ($sources as $source) {
// Get hold of our source wrapper.
$wrapper = NULL;
// The party data set is a special case.
if ($source['data_set'] == 'party') {
$wrapper = $party_wrapper;
}
else {
$controller = $party
->getDataSetController($source['data_set']);
if ($entity = $controller
->getEntity()) {
$wrapper = entity_metadata_wrapper($controller
->getDataInfo('entity type'), $entity);
}
}
// If we have a wrapper, get hold of the value.
if ($wrapper && isset($wrapper->{$source['property']})) {
if (!empty($source['value'])) {
$new_value = $wrapper->{$source['property']}
->value() ? $wrapper->{$source['property']}->{$source['value']}
->value() : NULL;
}
else {
$new_value = $wrapper->{$source['property']}
->value();
}
// Pass on for any callbacks.
$new_value = PartyPrimaryFields::executeCallback($new_value, $source, $target);
if (!empty($new_value)) {
// Set final value and end loop.
$final_value = $new_value;
break;
}
}
}
// Update party primary field with new value.
if ($final_value !== $original_value) {
$party_wrapper->{$target} = $final_value;
// Check what kind of save we need.
$target_info = $party_wrapper
->getPropertyInfo($target);
if (!empty($target_info['field'])) {
$needs_store = $needs_store | PartyController::UPDATE_FIELDS;
}
elseif (!empty($target_info['schema field'])) {
$needs_store = $needs_store | PartyController::UPDATE_PROPERTY;
}
else {
$needs_store = $needs_store | PartyController::UPDATE_OTHER;
}
}
}
// Allow modules to respond in case they need to do anything.
drupal_alter('party_primary_fields', $party, $needs_store);
// Check whether we need to store the changes.
if ($store && $needs_store) {
// If we need to do a full save, set a flag to prevent recursion and run.
if ($store & $needs_store & PartyController::UPDATE_OTHER) {
$party->primary_fields_updated = TRUE;
$party
->save();
}
else {
// Load up the original party.
$party->original = entity_load_unchanged('party', $party->pid);
// If we need to do a write record, do that.
if ($store & $needs_store & PartyController::UPDATE_PROPERTY) {
drupal_write_record($this->entityInfo['base table'], $party, $this->idKey);
}
// If we need to invoke a field update, do that.
if ($store & $needs_store & PartyController::UPDATE_FIELDS) {
$this
->invoke('update', $party);
}
else {
module_invoke_all('entity_update', $party, 'party');
}
}
}
elseif ($store && module_exists('search_api')) {
search_api_track_item_change('party', array(
$party->pid,
));
}
return $needs_store;
}