function message_ui_entity_update in Message UI 7
Same name and namespace in other branches
- 8 message_ui.module \message_ui_entity_update()
Implements hook_entity_update().
Submit handler for updating the arguments number.
When a message type is been edited, there could be a change in the arguments of the message - added or removed. If this has been defined, we need to update the arguments of the other messages. This will be achieved by in two steps: 1. Load an instance of the message from the same type 2. Cont the number of the arguments and if there is a difference between the number of the arguments from the old message to the current one - create a batch or a queue and update the messages.
File
- ./
message_ui.module, line 461 - Main file for the message UI module.
Code
function message_ui_entity_update($entity, $type) {
if ($type != 'message_type' || !variable_get('update_tokens_update_tokens')) {
return;
}
$type = $entity->name;
$query = new entityFieldQuery();
$result = $query
->entityCondition('entity_type', 'message')
->propertyCondition('type', $type)
->range(0, 1)
->propertyOrderBy('mid', 'DESC')
->execute();
// There is no messages from this type.
if (empty($result['message'])) {
return;
}
$keys = array_keys($result['message']);
$message = message_load(reset($keys));
$new_arguments = message_ui_message_arguments($type);
$old_arguments_number = count($message->arguments);
$new_arguments_number = count($new_arguments);
$how_to_act = variable_get('update_tokens_how_to_act');
$update['when_added'] = $old_arguments_number < $new_arguments_number && $how_to_act == 'update_when_added';
$update['when_removed'] = $old_arguments_number > $new_arguments_number && $how_to_act == 'update_when_removed';
if (!($update['when_added'] || $update['when_removed'])) {
return;
}
$item_to_process = variable_get('update_tokens_number_items', 250);
if (variable_get('update_tokens_how_update') == 'update_with_batch') {
// Get all the messages.
$query = new entityFieldQuery();
$result = $query
->entityCondition('entity_type', 'message')
->propertyCondition('type', $type)
->propertyOrderBy('mid', 'DESC')
->execute();
$chunks = array_chunk(array_keys($result['message']), $item_to_process);
$operations = array();
foreach ($chunks as $chunk) {
$operations[] = array(
'message_ui_arguments_update',
array(
$chunk,
$new_arguments,
),
);
}
// Set the batch.
$batch = array(
'operations' => $operations,
'finished' => 'message_ui_message_arguments_update',
'title' => t('Updating the messages arguments.'),
'init_message' => t('Start process messages.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Example Batch has encountered an error.'),
);
batch_set($batch);
batch_process('admin/structure/messages');
}
elseif (variable_get('update_tokens_how_update') == 'update_when_item') {
// Define the queue item data.
$data = array(
'type' => $type,
'last_mid' => 0,
'new_arguments' => $new_arguments,
'item_to_process' => $item_to_process,
);
// Set the queue worker.
$queue = DrupalQueue::get('message_ui_arguments');
return $queue
->createItem($data);
}
}