Skip to content

Instantly share code, notes, and snippets.

@Quevin
Last active August 29, 2015 14:25
Show Gist options
  • Save Quevin/d76b30800fdb4fbee157 to your computer and use it in GitHub Desktop.
Save Quevin/d76b30800fdb4fbee157 to your computer and use it in GitHub Desktop.
hook_update_N() for entity_translation
/**
* First, manually disable translation on the title field.
* This script copies title from limbo back into the node.
* See https://www.drupal.org/node/2107829#comment-8132759.
*/
function entity_translation_update_7021() {
// Why aren't we using db_query/select here?
$nodes = array();
$untitled_nodes = array();
$con = mysqli_connect('localhost', 'drupaluser', '', 'pharm');
$query = "SELECT * FROM node WHERE 1";
$result = mysqli_query($con, $query);
function addTitle($con, $info) {
$q1 = FALSE;
$q2 = FALSE;
$entity_type = 'node';
$bundle = $info['type'];
$deleted = 0;
$entity_id = $info['nid'];
$revision_id = $info['vid'];
$language = $info['language'];
$delta = 0;
$title_field_value = addslashes($info['title']);
$query = "INSERT IGNORE INTO field_data_title_field(entity_type, bundle, deleted, entity_id, revision_id, language, delta, title_field_value) VALUES('$entity_type', '$bundle', $deleted, $entity_id, $revision_id, '$language', $delta, '$title_field_value')";
echo $query . "\r\n\r\n";
$result = mysqli_query($con, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}
else // The query was successful
{
$q1 = TRUE;
}
if ($q1) {
$query = "INSERT INTO field_revision_title_field(entity_type, bundle, deleted, entity_id, revision_id, language, delta, title_field_value) VALUES('$entity_type', '$bundle', $deleted, $entity_id, $revision_id, '$language', $delta, '$title_field_value')";
echo
$query . "\r\n\r\n";
$result = mysqli_query($con, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}
else // The query was successful
{
$q2 = TRUE;
}
}
return $q1 AND $q2;
}
function hasTitle($con, $nid) {
$q1 = FALSE;
$q2 = FALSE;
$query = "SELECT entity_id FROM field_data_title_field WHERE entity_id=$nid";
$result = mysqli_query($con, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}
else // The query was successful
{
while ($row = mysqli_fetch_assoc($result)) {
if ($row['entity_id']) {
$q1 = TRUE;
}
}
}
if ($q1) {
$query = "SELECT entity_id FROM field_revision_title_field WHERE entity_id=$nid";
$result = mysqli_query($con, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}
else // The query was successful
{
while ($row = mysqli_fetch_assoc($result)) {
if ($row['entity_id']) {
$q2 = TRUE;
}
}
}
}
return $q1 AND $q2;
}
function changeLanguage($con, $lang, $nid, $rid) {
$q1 = FALSE;
$q2 = FALSE;
$q3 = FALSE;
$query = "UPDATE node SET language='$lang' WHERE nid=$nid and vid=$rid";
$result = mysqli_query($con, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}
else // The query was successful
{
echo mysqli_affected_rows($con);
$q1 = TRUE;
}
if (
$q1
) {
echo " node was updated\r\n";
$query = "UPDATE field_data_title_field SET language='$lang' WHERE entity_id=$nid AND revision_id=$rid";
$result = mysqli_query($con, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}
else // The query was successful
{
echo mysqli_affected_rows($con);
$q2 = TRUE;
}
}
if (
$q1 AND $q2
) {
echo " field_data_title_field was updated\r\n";
$query = "UPDATE field_revision_title_field SET language='$lang' WHERE entity_id=$nid AND revision_id=$rid";
$result = mysqli_query($con, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}
else // The query was successful
{
echo mysqli_affected_rows($con);
$q3 = TRUE;
}
}
if (
$q1 AND $q2 AND $q3
) {
echo " field_revision_title_field was updated\r\n";
return TRUE;
}
}
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}
else // The query was successful
{
while ($row = mysqli_fetch_assoc($result)) {
$nid = $row['nid'];
$vid = $row['vid'];
$type = $row['type'];
$language = $row['language'];
$title = $row['title'];
$nodes[$nid] = array(
"nid" => $nid,
"vid" => $vid,
"type" => $type,
"language" => $language,
"title" => $title,
);
}
}
$num_nodes = count($nodes);
echo "There are " . $num_nodes . " nodes total\r\n";
foreach (
$nodes as $nid => $info) {
if (!hasTitle($con, $nid)) {
$untitled_nodes[$nid] = $info;
}
}
echo
"There are " . count($untitled_nodes) . " untitled nodes\r\n";
// Give back the titles to the untitled nodes
foreach ($untitled_nodes as $nid => $info) {
if (addTitle($con, $info)) {
echo "Title " . $info['title'] . "\r\nAdded for node " . $nid . "\r\n\r\n";
}
else {
echo "FAILED Title " . $info['title'] . "\r\nFAILED to add for node " . $nid . "\r\n\r\n";
}
}
// change all node languages to 'und'
foreach ($nodes as $nid => $info) {
if (changeLanguage($con, 'und', $nid, $info['vid'])) {
echo "nid:" . $nid . "updated\r\n\r\n";
}
else {
echo "nid:" . $nid . "FAILED\r\n\r\n";
}
}
}
/**
* Disable the title module after you've manually disabled translation on the field.
*/
function entity_translation_update_7022() {
module_disable(array('title'), TRUE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment