Skip to content

Instantly share code, notes, and snippets.

@liberatr
Forked from lipcpro/setlangcode.drush.inc
Last active September 22, 2017 17:08
Show Gist options
  • Save liberatr/579bf26e7f09391ec22a to your computer and use it in GitHub Desktop.
Save liberatr/579bf26e7f09391ec22a to your computer and use it in GitHub Desktop.
D7: drush command to list content types available and set the language code for a certain type
<?php
/**
* Implements hook_drush_command().
*/
function setlangcode_drush_command() {
$items = array();
// The 'setlangcode' command.
$items['setlangcode'] = array(
'description' => "Sets the language code for certain types of entities.",
'arguments' => array(
'langcode' => 'The language code to be set.',
'bundle' => 'The bundle or content type to set the language code on.',
'entity_type' => 'The entity type to act on',
),
'examples' => array(
'drush slc EG article node' => 'Set the language code to EG for all article nodes.',
),
'aliases' => array('slc'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
// The 'setlangsimple' command.
$items['setlangsimple'] = array(
'description' => "Sets the language code for a table.",
'arguments' => array(
'langold' => 'The language code to be replaced.',
'langcode' => 'The language code to be set.',
'table' => 'The table to set the language code on.',
),
'examples' => array(
'drush slcs de en redirect' => 'Set the language code to en for all redirects in de.',
),
'aliases' => array('slcs'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
$items['list-content-types'] = array(
'description' => "List the avaiable bundles or content types for a given entity.",
'arguments' => array(
'entity_type' => 'The entity type to act on',
),
'examples' => array(
'drush list-contents-types node' => 'Show the node content types we have.',
),
'aliases' => array('lct'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
return $items;
}
/**
I placed this file here in order to have it run in Drush:
~/.composer/vendor/drush/drush/setlangcode.drush.inc
It should be inside your drush directory, or the Drupal root.
Running this can help you find entities:
https://pfproaustriadev3.prod.acquia-sites.com/devel/php
dsm(entity_get_info());
http://pfizerpro.at.localhost/admin/config/regional/language/add
Custom Language
Langauge Code: de-AT
Language Name in English: Austrian German
Native Language Name: Deutsch
Prefix Path Language Code: de-AT
Language Domain: n/a
Direction: Left to Right
Set new language to default
drush lct node (just to see how many nodes there are)
drush setlangcode 'de-AT' hcp_edetail node
drush setlangcode 'de-AT' hcp_article node
drush setlangcode 'de-AT' hcp_basic_page node
drush setlangcode 'de-AT' hcp_contact_us node
drush setlangcode 'de-AT' hcp_event node
drush setlangcode 'de-AT' hcp_indications node
# drush setlangcode 'de-AT' hcp22_pre_event_survey node
drush setlangcode 'de-AT' hcp_support node
drush setlangcode 'de-AT' hcp_tab_article node
drush setlangcode 'de-AT' hcp_tabs_support node
drush setlangcode 'de-AT' hcp_products node
drush setlangcode 'de-AT' hcp_reference node
drush setlangcode 'de-AT' online_webinar node
drush setlangcode 'de-AT' hcp_login node
drush setlangcode 'de-AT' hcp_conditions node
# drush setlangcode 'de-AT' poll node
drush setlangcode 'de-AT' on_demand_webinar node
# drush setlangcode 'de-AT' webform node
drush setlangsimple 'de' 'de-AT' entity_translation
drush setlangsimple 'de' 'de-AT' hcp_brand
drush setlangsimple 'de' 'de-AT' redirect
drush setlangsimple 'de' 'de-AT' users
drush setlangsimple 'de' 'de-AT' menu_links
drush setlangsimple 'de' 'de-AT' menu_links_revision
drush setlangsimple 'de' 'de-AT' locales_target
# drush setlangsimple 'de' 'de-AT' url_alias
drush cc all
Export all translations from an existing site:
https://pfproaustriadev4.prod.acquia-sites.com/admin/config/regional/translate/export
Import languages to the new site:
http://pfizerpro.at.localhost/admin/config/regional/translate/import
*/
function drush_setlangcode_setlangsimple($langold, $langcode, $table) {
if(!isset($langcode) || preg_match('@[^a-zA-Z_-]@', $langcode) || preg_match('@[^a-zA-Z_-]@', $langold)) {
drush_print("Language codes should just contain letters, underscores and hyphens.");
return;
}
if(!isset($table) || preg_match('@[^a-zA-Z0-9_-]@', $table)) {
drush_print("Table names should just contain letters, numbers, underscores and hyphens.");
return;
}
$update = db_update($table);
$update->fields(array('language' => $langcode,))
->condition('language', $langold);
$count = $update->execute();
drush_print($count ." rows updated in ". $table ." table");
}
function drush_setlangcode($langcode, $bundle, $entity_type = 'node') {
$entity = entity_get_info($entity_type);
//which table to query
$table = $entity['base table'];
//what is the key of the table
$key = $entity['entity keys']['id'];
//match this value for a bundle
$bundle_key = $entity['bundle keys']['bundle'];
//how to sort by bundle
$has_types = (count($entity['bundles']) > 1);
if(count($entity['bundles']) == 0) {
drush_print("Entity type ". $entity_type ." has no bundles");
return;
}
$count = 0;
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', $bundle);
$result = $query->execute();
if (isset($result[$entity_type])) {
$entities = entity_load($entity_type, array_keys($result[$entity_type]));
}
else {
drush_print("Nothing found for ". $entity_type ." and ". $bundle .".");
return;
}
// Loop through results.
foreach ($entities as $row) {
$row->language = $langcode;
entity_save($entity_type, $row);
$count++;
drush_print($row->$key . ' - ' . $bundle . ' - ' . $row->title . ' the language code has been set to ' . $langcode);
}
drush_print($count . " ". $entity['label'] ."s of type " . $bundle . " have been set to " . $langcode);
}
function drush_setlangcode_list_content_types($entity_type = 'node') {
$entity = entity_get_info($entity_type);
//which table to query
$table = $entity['base table'];
//what is the key of the table
$key = $entity['entity keys']['id'];
//match this value for a bundle
$bundle_key = $entity['bundle keys']['bundle'];
//how to sort by bundle
$has_types = (count($entity['bundles']) > 1);
if(count($entity['bundles']) == 0) {
drush_print("Entity type ". $entity_type ." has no bundles");
return;
}
// Loop through results.
foreach (array_keys($entity['bundles']) as $bundle) {
//count number of entities of this bundle
$q2 = db_select($table, 'b')
->fields('b');
if($entity_type != 'taxonomy_term') { ///really only works for nodes and beans
$q2->condition($bundle_key, $bundle, '=');
if($has_types) {
$q2->addExpression('COUNT(b.'. $bundle_key .')', 'bundle_count');
}
else {
$q2->addExpression('COUNT(1)', 'bundle_count');
}
}
else {
drush_print("Taxonomy terms are language neutral, we hope.");
return;
}
$type = $q2->execute()->fetchAll();
$subq = db_select($table, 'b')
->fields('b')
->condition($bundle_key, $bundle, '=');
if(in_array('language', $entity['schema_fields_sql']['base table'])) {
$subq->groupBy('b.language');
$subq->addExpression('COUNT(b.language)', 'language_count');
}
else {
$subq->join('entity_translation', 'et', 'b.'. $key .' = et.entity_id');
$subq->fields('et');
$subq->groupBy('et.language');
$subq->addExpression('COUNT(et.language)', 'language_count');
}
$count = $subq->execute();
if($type[0]->bundle_count > 0) {
drush_print($bundle .": ". $type[0]->bundle_count . " ". $entity['label'] ."s found - " . $entity['bundles'][$bundle]['label'] . ".");
if($count->rowCount() > 0) {
foreach($count as $language) {
drush_print(" ". $language->language .": " . $language->language_count . " ". $entity['label'] ."s of type " . $bundle . ".");
}
}
else {
drush_print(" The ". $bundle ." ". $entity['label'] ."s have not been translated.");
}
}
else {
drush_print("No ". $entity['label'] ."s of type " . $bundle . ' - ' . $entity['bundles'][$bundle]['label']);
drush_print(" 0 ". $bundle ." ". $entity['label'] ."s");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment