Last active
April 18, 2017 18:42
-
-
Save robinvanemden/54facb30ae60e706eaed7bf3c515a8f1 to your computer and use it in GitHub Desktop.
Drupal 6 to Drupal 8 field conversion helper scripts. Bare bones, rough and simple - yet effective.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\node\Entity\Node; | |
$message = "Click button to start Drupal 6 DB to Drupal 8 entityreference field conversion."; | |
// D6 field machine name | |
$field = "field_name"; | |
// D8 field machine name | |
$target_field = "field_name"; | |
// Taxonomy entityreference node type machine name | |
$node_type = "node_type_name"; | |
// D6 Database | |
$host = "localhost"; | |
$user = "username"; | |
$pass = "thepassword"; | |
$db = "databasename"; | |
if (isset($_POST['convert'])) { | |
$content_field = "content_".$field; | |
// when taxonomy ref use instead: $field_value = $field."_value" | |
$field_value = $field."_nid"; | |
// connect to D6 database | |
mysql_connect($host, $user, $pass) or die("Could not connect: " . mysql_error()); | |
mysql_select_db($db); | |
// retrieve all D8 nodes | |
$nids = \Drupal::entityQuery('node')->condition('type', $node_type)->execute(); | |
// loop over all D8 nodes | |
foreach ($nids as $nid) { | |
// load D8 node | |
$node = Node::load($nid); | |
// retrieve D6 field references for nid | |
$result = mysql_query("SELECT `".$field_value."` FROM `".$content_field."` WHERE `nid` = ".$nid); | |
$references_array = array(); | |
while($row = mysql_fetch_array($result)){ | |
$references_array[] = array( | |
'target_id' => $row[0] | |
); | |
} | |
// clean up the result | |
$references_array = array_values(array_unique($references_array, SORT_REGULAR)); | |
// set and save the D6 values to the D8 node | |
$node->$target_field = $references_array; | |
$node->save(); | |
} | |
$message = "Conversion completed."; | |
} | |
?> | |
<br/><br/> | |
<?php | |
echo $message; | |
?> | |
<br/><br/> | |
<form method="post"> | |
<input type="submit" name="convert" value="convert"> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Title based sync sql<br/> | |
<pre> | |
<?php | |
use Drupal\node\Entity\Node; | |
$message = "Ready to convert!"; | |
$field = "field_name"; | |
$target_field = 'field_name'; | |
$node_type = 'main_video'; | |
$host = "localhost"; | |
$user = "username"; | |
$pass = 'password'; | |
$db = "database"; | |
if (isset($_POST['convert'])) { | |
$content_field = "content_".$field; | |
$field_value = $field."_nid"; // node ref | |
mysql_connect($host, $user, $pass) or die("Could not connect: " . mysql_error()); | |
mysql_select_db($db); | |
$nids = \Drupal::entityQuery('node')->condition('type', $node_type)->execute(); | |
foreach ($nids as $nid) { | |
print($nid.' '); | |
$node = Node::load($nid); | |
$inner_result = mysql_query("SELECT nid FROM `node` WHERE `title` = '".$node->get("title")->value."'"); | |
$inner_array = mysql_fetch_array($inner_result); | |
$result = mysql_query("SELECT `created` FROM `node` WHERE `nid` = ".$inner_array[0]); | |
$row = mysql_fetch_array($result); | |
if(isset($row[0]) && $row[0]!=0){ | |
$node->set("created",(int)$row[0]); | |
$node->save(); | |
} | |
} | |
$message = "Conversion completed!"; | |
} | |
?> | |
</pre> | |
<br/><br/> | |
<?php | |
if (isset($message)) { | |
echo $message; | |
} | |
?> | |
<br/><br/> | |
<form method="post"> | |
<input type="submit" name="convert" value="convert"> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\node\Entity\Node; | |
$message = "Ready to convert!"; | |
$node_type = "type_name"; | |
$field_name = "tax_ref_field_name"; | |
if (isset($_POST['convert'])) { | |
$message = "Conversion has been called."; | |
$nids = \Drupal::entityQuery('node')->condition('type', $node_type)->execute(); | |
foreach ($nids as $nid) { | |
$references_array = array(); | |
$node = Node::load($nid); | |
if ($node->hasTranslation("en") && $node->hasTranslation("nl")) { | |
// get [en] refs | |
$node_en = $node->getTranslation("en"); | |
$institutes = $node_en->get($field_name)->referencedEntities(); | |
foreach ($institutes as $inst) { | |
if ($inst->hasTranslation("nl")) { | |
$local_tid = $inst->getTranslation("nl")->tid->getValue()[0]["value"]; | |
$references_array[] = array( | |
'target_id' => $local_tid | |
); | |
} | |
if ($inst->hasTranslation("en")) { | |
$local_tid = $inst->getTranslation("en")->tid->getValue() [0]["value"]; | |
$references_array[] = array( | |
'target_id' => $local_tid | |
); | |
} | |
} | |
// get [nl] refs | |
$node_nl = $node->getTranslation("nl"); | |
$institutes = $node_nl->get($field_name)->referencedEntities(); | |
foreach ($institutes as $inst) { | |
if ($inst->hasTranslation("nl")) { | |
$local_tid = $inst->getTranslation("nl")->tid->getValue() [0]["value"]; | |
$references_array[] = array( | |
'target_id' => $local_tid | |
); | |
} | |
if ($inst->hasTranslation("en")) { | |
$local_tid = $inst->getTranslation("en")->tid->getValue() [0]["value"]; | |
$references_array[] = array( | |
'target_id' => $local_tid | |
); | |
} | |
} | |
$references_array = array_values(array_unique($references_array, SORT_REGULAR)); | |
// save all to both | |
$node_nl->$field_name = $references_array; | |
$node_nl->save(); | |
$node_en->$field_name = $references_array; | |
$node_en->save(); | |
} | |
} | |
} | |
?> | |
<br/><br/> | |
<?php | |
echo $message; | |
?> | |
<br/><br/> | |
<form method="post"> | |
<input type="submit" name="convert" value="convert"> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment