Advertisement
nicklewis

sync panelizer nodes using services

Nov 21st, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. <?php
  2. // sync panelizer settings using services
  3. function sync_panelizer_node($nid) {
  4.   $context = stream_context_create(array(
  5.     'http' => array(
  6.         'header'  => "Authorization: Basic " . base64_encode("u:p")
  7.     )
  8.   ));
  9.   $contents = file_get_contents(variable_get('web_services_end_point', 'http://somesite.dev.zivtech.com') . '/rest/node/' . $nid . '.json', false, $context);
  10.   $json = drupal_json_decode($contents);
  11.   $node = node_load($nid);
  12.   $node->title = $json['title'];
  13.   $node->title_field = $json['title_field'];
  14.   $node->body = $json['body'];
  15.   foreach($json as $key => $value) {
  16.     if(strpos($key,'field') !== false) {
  17.       $node->$key = $value;
  18.     }
  19.   }
  20.   // adjust the decoded json into the correct data
  21.   $panes = array();
  22.   foreach($json['panelizer']['page_manager']['display']['content'] as $key => $pane) {
  23.     $pane = (object)$pane;
  24.     $query = db_query("SELECT pid FROM {panels_pane} WHERE pid = :pid AND did = :did", array(
  25.       ':pid' => $pane->pid,
  26.       ':did' => $pane->did,
  27.     ));
  28.     if(!$query->fetch()) {
  29.       // replace the name of the pid if it doesn't yet exist on the site
  30.       $pid = $pane->pid;
  31.       $pane->pid = 'new-' . $pane->uuid;
  32.       $replace_pids[$pid] = $pane->pid;
  33.     }
  34.     // sync the did with the existing one (since it may differ)
  35.     $pane->did = $node->panelizer['page_manager']->did;
  36.     $panes[$pane->pid] = $pane;
  37.     $panes[$pane->pid]->configuration = (array)$panes[$pane->pid]->configuration;
  38.     $panes[$pane->pid]->style = (array)$panes[$pane->pid]->style;
  39.   }
  40.   // trick panelizer into thinking we are saving this from the UI.
  41.   $node->panelizer['page_manager']->display_is_modified = TRUE;
  42.   $node->panelizer['page_manager']->display->content = $panes;
  43.   $node->panelizer['page_manager']->display->panels = $json['panelizer']['page_manager']['display']['panels'];
  44.   // replace pids with new-uuid values if needed
  45.   foreach($node->panelizer['page_manager']->display->panels as $region => $pids) {
  46.     foreach($pids as $key => $pid) {
  47.       if($replace_pids[$pid]) {
  48.         $node->panelizer['page_manager']->display->panels[$region][$key] = $replace_pids[$pid];
  49.       }
  50.     }
  51.   }
  52.   node_save($node);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement