entity_translation_create.drush.inc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Implementation of hook_drush_command().
  4. */
  5. function entity_translation_create_drush_command() {
  6. $items['create-translations'] = array(
  7. 'description' => 'Create translations.',
  8. 'arguments' => array(
  9. 'bundle' => 'Bundle (like node type)',
  10. ),
  11. 'options' => array(
  12. 'limit' => 'limit.',
  13. ),
  14. 'aliases' => array('etcc'),
  15. );
  16. return $items;
  17. }
  18. /**
  19. * Command callback. Generate a number of users.
  20. */
  21. function drush_entity_translation_create_create_translations( $bundle = false) {
  22. if (!$bundle) {
  23. return drush_set_error(t('Invalid bundle.'));
  24. }
  25. $entity = 'node';
  26. $limit = 100; //drush_get_option('limit') ? drush_get_option('limit') : 0;
  27. drush_log(t('limit : @lim', array('@lim' => $limit)), 'notice');
  28. drush_log(t('Bundle : @bun', array('@bun' => $bundle)), 'notice');
  29. $query = db_select('entity_translation', 'et');
  30. $query->join('node', 'n', 'n.nid = et.entity_id');
  31. $query->fields('et');
  32. $query->condition('et.entity_type',$entity);
  33. $query->condition('et.source','');
  34. $query->condition('et.language','und', '<>');
  35. if($bundle){
  36. $query->condition('n.type',$bundle);
  37. }
  38. $entities = $query->execute();
  39. $num_translations = 0;
  40. $i = 0;
  41. foreach ($entities as $row) {
  42. $trans = db_select('entity_translation', 'et')
  43. ->fields('et')
  44. ->condition('et.entity_id',$row->entity_id)
  45. ->condition('et.entity_type',$entity)
  46. ->condition('et.source',$row->language)
  47. ->execute();
  48. $translations = 0;
  49. foreach ($trans as $t) {
  50. $translations++;
  51. }
  52. drush_log(t('count translations : @trans', array('@trans' => $translations)), 'notice');
  53. if(!$translations){
  54. $nid = db_insert('entity_translation') // Table name no longer needs {}
  55. ->fields(array(
  56. 'entity_id' => $row->entity_id,
  57. 'entity_type' => $row->entity_type,
  58. 'language' => $row->language == 'en' ? 'fr' : 'en',
  59. 'source' => $row->language,
  60. 'uid' => $row->uid,
  61. 'status' => $row->status,
  62. 'translate' => $row->translate,
  63. 'created' => $row->created,
  64. 'changed' => $row->changed,
  65. ))
  66. ->execute();
  67. $num_translations ++;
  68. }else{
  69. drush_log(t('Entity id : @id already have a translation', array('@id' => $row->entity_id)), 'notice');
  70. }
  71. }
  72. drush_log(t('Created @number translations', array('@number' => $num_translations)), 'success');
  73. }