Skip to content

Instantly share code, notes, and snippets.

@lipcpro
Last active August 29, 2015 14:03
Show Gist options
  • Save lipcpro/b3e546fc84627c7bd1c2 to your computer and use it in GitHub Desktop.
Save lipcpro/b3e546fc84627c7bd1c2 to your computer and use it in GitHub Desktop.
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 nodes.",
'arguments' => array(
'langcode' => 'The language code to be set.',
'content_type' => 'The content type to set the language code on.',
),
'examples' => array(
'drush slc EG article' => 'Set the language code to EG for all articles.',
),
'aliases' => array('slc'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
$items['list-content-types'] = array(
'description' => "List the avaiable content types.",
'examples' => array(
'drush list-contents-types' => 'Show the content types we have.',
),
'aliases' => array('lct'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
return $items;
}
function drush_setlangcode($langcode, $content_type) {
//$langcode = strtolower($langcode);
$count = 0;
$query = db_select('node', 'n')
->condition('type', $content_type, '=')
->fields('n');
$result = $query->execute();
// Loop through results.
foreach ($result as $row) {
$node = node_load($row->nid);
$node->language = $langcode;
node_save($node);
$count++;
drush_print($row->nid . ' - ' . $row->type . ' - ' . $row->title . ' the language code has been set to ' . $langcode);
}
drush_print($count . " nodes of type " . $content_type . " have been set to " . $langcode);
}
function drush_setlangcode_list_content_types() {
$query = db_select('node_type', 'nt')
->orderBy('nt.type', 'ASC')
->fields('nt');
$result = $query->execute();
// Loop through results.
foreach ($result as $row) {
$q2 = db_select('node', 'n')
->fields('n')
//->groupBy('n.type')
->condition('type', $row->type, '=');
$q2->addExpression('COUNT(n.type)', 'type_count');
$type = $q2->execute()->fetchAll();
$subq = db_select('node', 'n')
->fields('n')
->groupBy('n.language')
->condition('type', $row->type, '=');
$subq->addExpression('COUNT(n.language)', 'language_count');
$count = $subq->execute();
if($type[0]->type_count >= 2){
drush_print($type[0]->type_count . " nodes of type " . $row->type . " - " . $row->name . " were found");
foreach($count as $language){
drush_print("There are " . $language->language_count . " nodes of type " . $row->type . " in " . $language->language . " ");
}
}elseif($type[0]->type_count == 1){
drush_print($type[0]->type_count . " node of type " . $row->type . ' - ' . $row->name . " was found");
foreach($count as $language){
drush_print("There are " . $language->language_count . " " . $row->type . " in " . $language->language . " ");
}
}else{
drush_print("No nodes of type " . $row->type . ' - ' . $row->name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment