Skip to content

Instantly share code, notes, and snippets.

@naeluh
Created March 27, 2015 19:57
Show Gist options
  • Save naeluh/430e35d133f5175005bd to your computer and use it in GitHub Desktop.
Save naeluh/430e35d133f5175005bd to your computer and use it in GitHub Desktop.
<?php
$app = "/app_1/";
$preTitle = 'a';
function seoUrl($string)
{
$string = strtolower($string); // Lower case everything
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string); // Make alphanumeric (removes all other characters)
$string = preg_replace("/[\s-]+/", " ", $string); // Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s_]/", "-", $string); // Convert whitespaces and underscore to dash
return $string;
}
function hook_edit_node_prepare($node)
{
global $user;
global $preTitle;
switch ($node->type) {
case 'beacon_builder':
$_SESSION['pre'] = "$node->title";
$preTitle = $_SESSION['pre']; // passing the node title
drupal_set_message('a'. $_SESSION['pre'] , 'status');
break;
}
}
function hook_edit_node_update($node)
{
global $user;
global $preTitle;
switch ($node->type) {
case 'beacon_builder':
$title = $node->title; // passing the node title
// $node->path['alias'] = "app_1/" . $node->type . "/" . seoUrl(strtolower($title)); //Sets path alias for node
$node_body = $node->body[$node->language][0]['value'];
// Passing Multifield array to new node
if (isset($node->field_hint_text_and_image[$node->language])) {
$hintObjects = $node->field_hint_text_and_image[$node->language];
}
// Regular and Blurred Images
if (isset($node->field_found_beacon_regular_image[$node->language][0]['fid'])) {
$fid = $node->field_found_beacon_regular_image[$node->language][0]['fid'];
$img = file_load($fid);
$filepath = $img->uri;
}
if (isset($node->field_found_beacon_blurred_image[$node->language][0]['fid'])) {
$fid_blur = $node->field_found_beacon_blurred_image[$node->language][0]['fid'];
$img_blur = file_load($fid_blur);
$filepath_blur = $img_blur->uri;
}
$types = _node_types_build()->types;
$nodelist = array();
foreach($types as $t) {
$nodelist[] = $t->type;
}
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')->entityCondition('bundle', array_values($nodelist));
$result = $query->execute();
$nodes = array();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$nodes = node_load_multiple($nids);
foreach($nodes as $key => $node) {
if ($node->type === 'found' || $node->type === 'info' || $node->type === 'share' || $node->type === 'hint_gallery_page') {
$node_title = $node->title;
if ($node_title == $title) {
// $nodeTitle = $node->title;
// $node_alias = drupal_get_path_alias('node/' . $node->nid);
$node_nid = $node->nid;
// $node_full_path = $_SERVER["SERVER_NAME"] . base_path() . $node_alias;
drupal_set_message('b'. $preTitle , 'status');
multi_node_update($node_nid, $title, $node_body, $filepath, $filepath_blur, $hintObjects);
}
}
}
}
break;
}
}
function hook_edit_node_insert($node)
{
global $user;
switch ($node->type) {
case 'beacon_builder':
$title = "$node->title"; // passing the node title
$node->path['alias'] = "app_1/" . $node->type . "/" . seoUrl(strtolower($title)); //Sets path alias for node
if (isset($node->body[$node->language][0]['value'])) {
$body = $node->body[$node->language][0]['value']; // passing the body html
}
// Passing Multifield array to new node
if (isset($node->field_hint_text_and_image[$node->language])) {
$hintObjects = $node->field_hint_text_and_image[$node->language];
}
// Regular and Blurred Images
if (isset($node->field_found_beacon_regular_image[$node->language][0]['fid'])) {
$fid = $node->field_found_beacon_regular_image[$node->language][0]['fid'];
$img = file_load($fid);
$filepath = $img->uri;
}
if (isset($node->field_found_beacon_blurred_image[$node->language][0]['fid'])) {
$fid_blur = $node->field_found_beacon_blurred_image[$node->language][0]['fid'];
$img_blur = file_load($fid_blur);
$filepath_blur = $img_blur->uri;
}
// content-types to be passed to node creation function
$ctype_a = "info";
$ctype_b = "share";
$ctype_c = "hint_gallery_page";
$ctype_d = "found";
// function calls to create new node
multi_node_hook_create_node($title, $body, $filepath, $ctype_a);
multi_node_hook_create_node($title, $body, $filepath, $ctype_b);
multi_node_hook_create_node_extra($title, $body, $filepath, $filepath_blur, $ctype_d);
multi_node_hook_create_hint_gallery($title, $hintObjects, $ctype_c);
drupal_set_message("Created:" . $_SERVER['SERVER_NAME'] . "/app_1/found/" . seoUrl(strtolower($title)) . "\n", 'status');
drupal_set_message("Created:" . $_SERVER['SERVER_NAME'] . "/app_1/info/" . seoUrl(strtolower($title)) . "\n", 'status');
drupal_set_message("Created:" . $_SERVER['SERVER_NAME'] . "/app_1/share/" . seoUrl(strtolower($title)) . "\n", 'status');
drupal_set_message("Created:" . $_SERVER['SERVER_NAME'] . "/app_1/hints/" . seoUrl(strtolower($title)) . "\n", 'status');
break;
}
}
function multi_node_update($nodeNid, $title, $body, $filepath, $extraFilepath, $updateHints)
{
if (isset($nodeNid)) {
$node = node_load($nodeNid); // ...where $nid is the node id
}
if (isset($title)) {
$node->title = $title;
}
if (isset($body)) {
$node->body[$node->language][0]['value'] = $body;
}
if (isset($updateHints)) {
$node->field_hints[$node->language] = $updateHints;
}
if (isset($filepath)) {
// Sets image from node above
$file = (object)array(
'uid' => 1,
'uri' => $filepath,
'filemime' => file_get_mimetype($filepath) ,
'status' => 1,
'display' => 1
);
$file = file_copy($file, 'public://');
$node->field_unlocked_beacon_hero_image[$node->language][0] = (array)$file;
// Sets image from node above
}
if (isset($extraFilepath)) {
// Sets image from node above
$file = (object)array(
'uid' => 1,
'uri' => $extraFilepath,
'filemime' => file_get_mimetype($extraFilepath) ,
'status' => 1,
'display' => 1
);
$file = file_copy($file, 'public://');
$node->field_unlocked_beacon_hero_image[$node->language][0] = (array)$file;
// Sets image from node above
}
node_save($node);
echo "Node with nid " . $node->nid . " updated!\n";
}
function multi_node_hook_create_node($title, $body, $filepath, $content_type)
{
global $user;
$node = new stdClass();
$node->title = $title; // Sets node title
$node->type = $content_type; // Sets node content type
node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$node->body[$node->language][0]['value'] = $body; // Sets body text
// $node->body[$node->language][0]['summary'] = text_summary($body); // Sets body summary text this will create it if it does not exist
$node->body[$node->language][0]['format'] = 'filtered_html'; // Sets format of the html in the body
// Sets image from node above
$file = (object)array(
'uid' => 1,
'uri' => $filepath,
'filemime' => file_get_mimetype($filepath) ,
'status' => 1,
'display' => 1
);
$file = file_copy($file, 'public://');
$node->field_unlocked_beacon_hero_image[$node->language][0] = (array)$file;
// Sets image from node above
$node->uid = $user->uid; //user who created the node
$node->status = 1; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page
$node->comment = 0;
// 0 = comments disabled, 1 = read only, 2 = read/write
$node->path['alias'] = "app_1/" . $content_type . "/" . seoUrl(strtolower($node->title)); //Sets path alias for node
$node = node_submit($node); // Prepare node for saving
node_save($node); // node save
}
function multi_node_hook_create_node_extra($title, $body, $filepath, $filepathExtra, $content_type)
{
global $user;
$node = new stdClass();
$node->title = $title; // Sets node title
$node->type = $content_type; // Sets node content type
node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$node->body[$node->language][0]['value'] = $body; // Sets body text
// $node->body[$node->language][0]['summary'] = text_summary($body); // Sets body summary text this will create it if it does not exist
$node->body[$node->language][0]['format'] = 'filtered_html'; // Sets format of the html in the body
// Sets image from node above
$file = (object)array(
'uid' => 1,
'uri' => $filepath,
'filemime' => file_get_mimetype($filepath) ,
'status' => 1,
'display' => 1
);
$file = file_copy($file, 'public://');
$node->field_found_beacon_regular_image[$node->language][0] = (array)$file;
// Sets image from node above
// Sets image from node above
$fileExtra = (object)array(
'uid' => 1,
'uri' => $filepathExtra,
'filemime' => file_get_mimetype($filepathExtra) ,
'status' => 1,
'display' => 1
);
$fileExtra = file_copy($fileExtra, 'public://');
$node->field_found_beacon_blurred_image[$node->language][0] = (array)$fileExtra;
// Sets image from node above
$node->uid = $user->uid; //user who created the node
$node->status = 1; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page
$node->comment = 0;
// 0 = comments disabled, 1 = read only, 2 = read/write
$node->path['alias'] = "app_1/" . $content_type . "/" . seoUrl(strtolower($node->title)); //Sets path alias for node
$node = node_submit($node); // Prepare node for saving
node_save($node); // node save
}
function multi_node_hook_create_hint_gallery($title, $hints, $content_type)
{
global $user;
$node = new stdClass();
$node->title = $title; // Sets node title
$node->type = $content_type; // Sets node content type
node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$node->field_hints[$node->language] = $hints;
$node->uid = $user->uid; //user who created the node
$node->status = 1; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page
$node->comment = 0;
// 0 = comments disabled, 1 = read only, 2 = read/write
$node->path['alias'] = "app_1/" . "hints/" . seoUrl(strtolower($node->title)); //Sets path alias for node
$node = node_submit($node); // Prepare node for saving
node_save($node); // node save
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment