I'm making an existing site with a few thousand nodes multiligual. Source is English. How can I set all of the existing content to langauge:English instead of neutral all in one bulk command?
5 Answers
Here's one way to do this that doesn't involve writing any code.
- Install the VBO module.
- Create a new view showing Content of whatever content types you need to convert. Use the Fields display method.
- Add a field Bulk operations: content.
- Check Modify entity values and select Language.
- Choose Apply.
- Save the view (add any other filters if necessary to limit the selection) and go to the created view page.
- From the view page, choose the option to select all items on all pages and execute the Modify entity values action.
- Choose the language and click next.
If you have an extremely large number of nodes, you may need to play around with the queuing settings so that not everything is executed at once, but I would try doing it in one pass first because hey, if it works, you're done (note: always test on a backup first!)
Here's how I bulk set the language on my site:
Using Views Bulk Optimization, select all nodes you want to change, use the 'Change value' option and then change the value to language you want.
After you've done this, the field values will not appear when editing the nodes, so you need to run a script, e.g.
UPDATE database.field_data_body SET language='en' WHERE bundle='page'
(use your db name, field name, and bundle as appropriate).
IMPORTANT: If you have any custom url aliases, they will be overwritten with this approach, due to a problem with pathauto, but there's a work-around: first go to /admin/config/search/path/settings, and change the 'Update action' option to 'Do nothing. Leave the old alias intact.', then change this value back once you've run your Views Bulk Operations action.
(I've now answered three Stack questions with this answer, hopefully it won't be considered spam).
In reference to the accepted answer here is a views export that does just that. https://gist.github.com/dasginganinja/7bfa862f379ade6e0f0f
Do keep in mind that this just changes the node's language. You will also need to update the affected database fields' language (i.e. body, tablefield, etc).
I wrote this little script that replaces all langcode fields with a new language code across all tables in your Drupal 9 (and possibly 8) installation.
- Place it in your Drupal ROOT/web folder
- Change the
$newlang
and$oldlang
variables. - Optionally add more conditions
- Execute it:
php myscript.php
Be aware that this does not do any checking or confirmations so be sure that you know what you are doing :-)
<?php
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
$kernel = DrupalKernel::createFromRequest(Request::createFromGlobals(), $autoloader, 'prod');
$kernel->boot();
/** @var \Drupal\Core\Database\Driver\mysql\Connection $connection */
$connection = \Drupal::service('database');
$tables = $connection->query('SHOW TABLES');
$newlang = 'en';
$oldlang = 'nl';
foreach ($tables as $table) {
$tableName = $table->Tables_in_db;
$cols = $connection->query('SHOW COLUMNS FROM '. $tableName);
foreach ($cols as $col) {
if ($col->Field == 'langcode') {
echo 'Found langcode field in '. $tableName .'. Updating...' . "\n";
try {
$connection->update($tableName)
->fields([
'langcode' => $newlang
])
->condition('langcode', $oldlang, '=')
->execute();
}
catch (\Exception $e) {
echo 'Got exception: '. $e->getMessage() . "\n";
}
}
}
}
In drupal 9, here is my solution : Here is what I done : in sql :
UPDATE node SET langcode='fr'
UPDATE node_field_data SET langcode='fr'
UPDATE node_field_revision SET langcode='fr'
now you have to update all the fields contains in the node type.
UPDATE node__body SET langcode='fr'
UPDATE node__comment SET langcode='fr'
UPDATE node__field_background_image SET langcode='fr'
UPDATE node__field_category SET langcode='fr'
UPDATE node__field_client_name SET langcode='fr'
UPDATE node__field_date SET langcode='fr'
UPDATE node__field_image SET langcode='fr'
UPDATE node__field_images SET langcode='fr'
UPDATE node__field_intro SET langcode='fr'
UPDATE node__field_paragraph SET langcode='fr'
UPDATE node__field_position SET langcode='fr'
UPDATE node__field_tags SET langcode='fr'
UPDATE node__field_video SET langcode='fr'
UPDATE node__simplenews_issue SET langcode='fr'
Then i redefine all the pattern in FR in pathauto.
I installed VBO modules, I created a view for listing all nodes, I added a vbo action field and run update url alias. Done !