Convert nodes from one panelizer settings to another

Public

Say you desided to change all nodes programatically to use another panelizer settings. I have used this after I desided to delete basic_page Content type and used only Page (new content type). Running the updatehook managed to convert all those basic_page panelizer configuration into page's one. See http://dropbucket.org/node/1768 if you need to convert the content types too.

</> CopyGet raw version
php
  1. /**
  2.  * Move all nodes with node:basic_page:default panelizer settings to node:page:default
  3.  */
  4. function mymodule_dependent_update_7001() {
  5. //Destination panelizer to convert nodes into
  6. $p_name = 'node:page:default';
  7.  
  8. //Get the panel-info
  9. $p_defaults = db_select('panelizer_defaults', 'd')
  10. ->fields('d')
  11. ->condition('d.name', $p_name, '=')
  12. ->execute()
  13. ->fetchAssoc();
  14. //Remove fields that are node in the panelizer_entity
  15. unset($p_defaults['pnid'], $p_defaults['title'], $p_defaults['panelizer_type'], $p_defaults['panelizer_key'], $p_defaults['access']);
  16.  
  17. /*
  18.   * Test with smaller query range first
  19.   * $result = db_query_range("SELECT * FROM panelizer_entity WHERE name='node:basic_page:default'", 0, 5);
  20.   */
  21. $result = db_query("SELECT * FROM panelizer_entity WHERE name='node:basic_page:default'");
  22. foreach ($result as $record) {
  23. $nid = $record->entity_id;
  24. //Update panelizer_entity
  25. $u_query = db_update('panelizer_entity')
  26. ->fields($p_defaults)
  27. ->condition('entity_type', 'node')
  28. ->condition('entity_id', $nid);
  29.  
  30. //dpm($u_query->arguments());
  31. //dpq($u_query,FALSE,'Update');
  32.  
  33. $u_query->execute();
  34. watchdog('mymodule', t('!nid updated to node:page:default.', array('!nid' => $nid)));
  35. }
  36. }