Post Update en Drupal
Un ejemplo donde se implementa un POST UPDATE mediante el hook hook_post_update_NAME()
Código de ejemplo sobre cómo usar hook_post_update_NAME
/**
* Implements hook_post_update_NAME().
*/
function MODULE_post_update_api_data_value(&$sandbox) {
if (!isset($sandbox['total'])) {
// Get API Data content type nid's.
$nids = Drupal::entityQuery('node')
->condition('type', 'apidata')
->execute();
$sandbox['total'] = count($nids);
$sandbox['current'] = 0;
}
$nodes_per_batch = 50;
$nids = Drupal::entityQuery('node')
->condition('type', 'apidata')
->range($sandbox['current'], $nodes_per_batch)
->execute();
foreach ($nids as $nid) {
$node = Drupal::entityTypeManager()
->getStorage('node')
->load($nid);
// Check if the field is empty.
if (empty($node->field_api_target->value)) {
// If is empty, set default value (0).
$node->field_api_target->value = 0;
}
$node->save();
$sandbox['current']++;
}
if ($sandbox['total'] === 0) {
// Until it is finished, the batch does not stop.
$sandbox['#finished'] = 1;
}
else {
// Indicates the percentage of the completed.
$sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']);
}
}
Obtener listado de post updates ejecutados previamente
drush eval '
$key_value = \Drupal::keyValue("post_update");
$update_list = $key_value->get("existing_updates");
print_r($update_list);
'