Last active
March 10, 2016 16:31
-
-
Save Luxian/dbcf5839f18d69802876 to your computer and use it in GitHub Desktop.
Cleanup for Drupal 8 paragrah translation
This file contains 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
#!/usr/bin/env drush | |
#<?php // trigger phpStorm syntax | |
/** | |
* @revision: 2 | |
* | |
* @file | |
* Drush script to fix Drupal 8 paragraph content translation. | |
* | |
* This script was meant to be run after the following patch was applied to | |
* paragraphs: | |
* | |
* https://www.drupal.org/node/2591171#comment-10950967 | |
* | |
* This will break/remove all existing translations of paragraphs. | |
* | |
* Before running this paragraph content is usually empty when a new | |
* node translation is added | |
*/ | |
/** | |
* Parent task that loops over all paragraphs | |
*/ | |
class AmazeeParagraphFixAllTask { | |
public function run() { | |
$paragraph_ids = $this->getAllParagraphs(); | |
foreach($paragraph_ids as $paragraph) { | |
$subtask = new AmazeeParagraphFixParagraphTask($paragraph); | |
$subtask->run(); | |
} | |
} | |
private function getAllParagraphs() { | |
$q = db_select('paragraphs_item', 'pi'); | |
$q->fields('pi', array('id', 'revision_id', 'langcode', 'type')); | |
return $q->execute()->fetchAll(); | |
} | |
} | |
/** | |
* Paragraph fix task | |
*/ | |
class AmazeeParagraphFixParagraphTask { | |
private $paragraph; | |
public function __construct($paragraph) { | |
$this->paragraph = $paragraph; | |
} | |
public function run() { | |
echo "\nParagraph: {$this->paragraph->id}.v{$this->paragraph->revision_id} ({$this->paragraph->type})\n"; | |
$tables = array( | |
'paragraphs_item_field_data', | |
'paragraphs_item_revision', | |
'paragraphs_item_revision_field_data', | |
); | |
foreach($tables as $table) { | |
echo "Invalid records from `{$table}` table removed: " . $this->deletefieldData($table). "\n"; | |
} | |
} | |
private function deletefieldData($table) { | |
return db_delete($table) | |
->condition('id', $this->paragraph->id) | |
->condition('revision_id', $this->paragraph->revision_id) | |
->condition('langcode', $this->paragraph->langcode, '<>') | |
->execute(); | |
} | |
} | |
echo "Fixing paragraph translations\n\n"; | |
$task = new AmazeeParagraphFixAllTask(); | |
$task->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment