0

I have a custom content entity for which I needed to change the type of one of its fields.

Following the example on Updating a base field type, I wrote code to update the base entity, which updated perfectly. However, all revisions have null values, including the current revision. Even though the data is stored in the base_entity table, it's not stored in the base_entity_revision table.

How do I modify hook_update_N() to also update the revision tables?

1 Answer 1

1

In addition to updating the main table, you also need to update the revision table, so the following changes should be made:

  1. $id_key = $entity_definition->getKey('id'); becomes $id_key = $entity_definition->getKey('id'); $revision_id_key = $entity_definition->getKey('revision');
  2. $table_name = $storage->getDataTable() ?: $storage->getBaseTable(); becomes $table_name = $storage->getDataTable() ?: $storage->getBaseTable(); $revision_table_name = $storage->getRevisionDataTable() ?: $storage->getRevisionTable();
  3. $status_values = $database->select($table_name) ->fields($table_name, [$id_key, 'status_field']) ->execute() ->fetchAllKeyed(); becomes $status_values = $database->select($table_name) ->fields($table_name, [$id_key, 'status_field']) ->execute() ->fetchAllKeyed(); $status_revision_values = $database->select($revision_table_name) ->fields($revision_table_name, [$revision_id_key, 'status_field']) ->execute() ->fetchAllKeyed();
  4. $database->update($table_name) ->fields(['status_field' => NULL]) ->execute(); becomes $database->update($table_name) ->fields(['status_field' => NULL]) ->execute(); $database->update($revision_table_name) ->fields(['status_field' => NULL]) ->execute();
  5. foreach ($status_values as $id => $value) { $database->update($table_name) ->fields(['status_field' => $value]) ->condition($id_key, $id) ->execute(); } becomes foreach ($status_values as $id => $value) { $database->update($table_name) ->fields(['status_field' => $value]) ->condition($id_key, $id) ->execute(); }

    foreach ($status_revision_values as $id => $value) { $database->update($revision_table_name) ->fields(['status_field' => $value]) ->condition($revision_id_key, $id) ->execute(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.