Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for creating and deleting languages #45

Merged
merged 6 commits into from
Jul 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Drupal/Driver/Cores/CoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,22 @@ public function isField($entity_type, $field_name);
*/
public function getEntityFieldTypes($entity_type);

/**
* Creates a language.
*
* @param \stdClass $language
* An object with the following properties:
* - langcode: the langcode of the language to create.
*/
public function languageCreate(\stdClass $language);

/**
* Deletes a language.
*
* @param \stdClass $language
* An object with the following properties:
* - langcode: the langcode of the language to delete.
*/
public function languageDelete(\stdClass $language);

}
14 changes: 14 additions & 0 deletions src/Drupal/Driver/Cores/Drupal6.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,18 @@ public function isField($entity_type, $field_name) {
return isset($map[$field_name]);
}

/**
* {@inheritdoc}
*/
public function languageCreate(\stdClass $language) {
throw new \Exception('Creating languages is not yet implemented for Drupal 6.');
}

/**
* {@inheritdoc}
*/
public function languageDelete(\stdClass $language) {
throw new \Exception('Deleting languages is not yet implemented for Drupal 6.');
}

}
63 changes: 63 additions & 0 deletions src/Drupal/Driver/Cores/Drupal7.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,69 @@ public function termDelete(\stdClass $term) {
return $status;
}

/**
* {@inheritdoc}
*/
public function languageCreate(\stdClass $language) {
include_once DRUPAL_ROOT . '/includes/iso.inc';
include_once DRUPAL_ROOT . '/includes/locale.inc';

// Get all predefined languages, regardless if they are enabled or not.
$predefined_languages = _locale_get_predefined_list();

// If the language code is not valid then throw an InvalidArgumentException.
if (!isset($predefined_languages[$language->langcode])) {
throw new InvalidArgumentException("There is no predefined language with langcode '{$language->langcode}'.");
}

// Enable a language only if it has not been enabled already.
$enabled_languages = locale_language_list();
if (!isset($enabled_languages[$language->langcode])) {
locale_add_language($language->langcode);
return $language;
}

return FALSE;
}

/**
* {@inheritdoc}
*/
public function languageDelete(\stdClass $language) {
$langcode = $language->langcode;
// Do not remove English or the default language.
if (!in_array($langcode, array(language_default('language'), 'en'))) {
// @see locale_languages_delete_form_submit().
$languages = language_list();
if (isset($languages[$langcode])) {
// Remove translations first.
db_delete('locales_target')
->condition('language', $langcode)
->execute();
cache_clear_all('locale:' . $langcode, 'cache');
// With no translations, this removes existing JavaScript translations
// file.
_locale_rebuild_js($langcode);
// Remove the language.
db_delete('languages')
->condition('language', $langcode)
->execute();
db_update('node')
->fields(array('language' => ''))
->condition('language', $langcode)
->execute();
if ($languages[$langcode]->enabled) {
variable_set('language_count', variable_get('language_count', 1) - 1);
}
module_invoke_all('multilingual_settings_changed');
drupal_static_reset('language_list');
}

// Changing the language settings impacts the interface:
cache_clear_all('*', 'cache_page', TRUE);
}
}

/**
* Helper function to get all permissions.
*
Expand Down
29 changes: 29 additions & 0 deletions src/Drupal/Driver/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

use Drupal\Component\Utility\Random;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Language\Language;
use Drupal\Driver\Exception\BootstrapException;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\Entity\Term;
Expand Down Expand Up @@ -345,4 +347,31 @@ public function isField($entity_type, $field_name) {
return (isset($fields[$field_name]) && $fields[$field_name] instanceof FieldStorageConfig);
}

/**
* {@inheritdoc}
*/
public function languageCreate(\stdClass $language) {
$langcode = $language->langcode;

// Enable a language only if it has not been enabled already.
if (!ConfigurableLanguage::load($langcode)) {
$created_language = ConfigurableLanguage::createFromLangcode($language->langcode);
if (!$created_language) {
throw new InvalidArgumentException("There is no predefined language with langcode '{$langcode}'.");
}
$created_language->save();
return $language;
}

return FALSE;
}

/**
* {@inheritdoc}
*/
public function languageDelete(\stdClass $language) {
$configurable_language = ConfigurableLanguage::load($language->langcode);
$configurable_language->delete();
}

}
14 changes: 14 additions & 0 deletions src/Drupal/Driver/DrupalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,18 @@ public function isField($entity_type, $field_name) {
return $this->getCore()->isField($entity_type, $field_name);
}

/**
* {@inheritdoc}
*/
public function languageCreate($language) {
return $this->getCore()->languageCreate($language);
}

/**
* {@inheritdoc}
*/
public function languageDelete($language) {
$this->getCore()->languageDelete($language);
}

}