Skip to content

Commit

Permalink
Fixed local IDs loss on message type revert.
Browse files Browse the repository at this point in the history
  • Loading branch information
RoSk0 committed May 15, 2016
1 parent 363eff2 commit 047f578
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions includes/message.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ class MessageTypeUIController extends EntityDefaultUIController {
field_attach_submit('message_type', $entity, $form['message_type_fields'], $form_state);
return $entity;
}

/**
* @inheritdoc
*
* Overridden in order to prevent message types from loosing local IDs.
*/
public function applyOperation($op, $message_type) {
if ($op == 'revert' && !empty($message_type->module)) {
$function = $message_type->module . '_default_message_type';
if (function_exists($function)) {
$defaults = $function();
$default = $defaults[$message_type->name];
unset($default->is_new);
$default->id = $message_type->id;
$default->status = ENTITY_IN_CODE;
$default->is_rebuild = TRUE;
entity_save('message_type', $default);
$label = entity_label($this->entityType, $message_type);
$vars = array('%entity' => $this->entityInfo['label'], '%label' => $label);
return t('Reverted %entity %label to the defaults.', $vars);
}
}
else {
return parent::applyOperation($op, $message_type);
}
}

}

/**
Expand Down
23 changes: 23 additions & 0 deletions includes/message.message_type.inc
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,26 @@ class MessageType extends Entity {
return $property;
}
}

/**
* Class MessageTypeFeaturesController.
*/
class MessageTypeFeaturesController extends EntityDefaultFeaturesController {

/**
* Overridden to update message type instead of deleting.
*
* Deleting leads to new local IDs which leads to data loss when message type
* is reference with entity reference module.
*/
public function revert($module = NULL) {
if ($defaults = features_get_default($this->type, $module)) {
foreach ($defaults as $name => $message_type) {
unset($message_type->is_new);
$modified = entity_load_single($this->type, $name);
$message_type->id = $modified->id;
entity_save($this->type, $message_type);
}
}
}
}
1 change: 1 addition & 0 deletions message.module
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ function message_entity_info() {
$items['message_type'] = array(
'label' => t('Message type'),
'controller class' => 'EntityAPIControllerExportable',
'features controller class' => 'MessageTypeFeaturesController',
'entity class' => 'MessageType',
'base table' => 'message_type',
'fieldable' => TRUE,
Expand Down

0 comments on commit 047f578

Please sign in to comment.