![]() In this case, the $sandbox variable is passed by reference to each iteration of the post_update hook. ![]() ![]() */ function _my_module_copy_field_values ( array & $sandbox, $bundle, $source_field, $dest_field, $nodes_per_batch = 20 ) * The amount of nodes to update at a given time. * Copies the value from one field to another empty field. Assuming your source and destination field types are compatible, it’s just two quick lines: The code to programmatically copy field data in Drupal 8 isn’t the hard part. What’s the best way to do this when manual GUI changes would take too long? A hook_post_update_NAME implementation was my answer. Once I’d added our field_new field to all the appropriate content types via the field UI, I needed to migrate the references out of the old fields, and into the new fields. One thing was clear: we needed the fields to be consistent across content types.īasically, I needed entity references stored in various older fields, let’s say field_old_one and field_old_two, to be copied into a single destination field we’ll call field_new. It also led to overly complex implementations of any custom functionality based on these relationships. This made it tricky to aggregate and filter content. It became apparent that similar relationships across different content types used separate fields with different machine names. ![]() My use case was straightforward: After more than a year of continual development on a client site, new patterns emerged in how content editors utilized certain entity reference fields. Hopefully the code sample below helps somebody save a few minutes of digging for solutions. The code to programmatically copy field data in Drupal 8 is pretty simple, but I wasn’t able to find any great examples for performing the operation at scale when I ran into the need myself. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |