1 language.module language_save($language)

API function to add or update a language.

Parameters

$language: Language object with properties corresponding to 'language' table columns.

File

core/modules/language/language.module, line 110
Add language handling functionality to Backdrop.

Code

function language_save($language) {
  $config = config('language.settings');
  $language->is_new = !$config->get('languages.' . $language->langcode);

  // If name was not set, we add a predefined language.
  if (!isset($language->name)) {
    include_once BACKDROP_ROOT . '/core/includes/standard.inc';
    $predefined = standard_language_list();
    $language->name = $predefined[$language->langcode][0];
    $language->direction = isset($predefined[$language->langcode][2]) ? $predefined[$language->langcode][2] : LANGUAGE_LTR;
  }

  // Set to enabled for the default language and unless specified otherwise.
  if (!empty($language->default) || !isset($language->enabled)) {
    $language->enabled = TRUE;
  }
  // Let other modules modify $language before saved.
  module_invoke_all('language_presave', $language);

  // Save the record and inform others about the change.
  $t_args = array('%language' => $language->name, '%langcode' => $language->langcode);

  // Make sure only applicable data is saved.
  $language_data = (array) $language;
  $defaults = array(
    'langcode' => '',
    'name' => '',
    'direction' => 0,
    'enabled' => TRUE,
    'weight' => 0,
  );
  $language_data += $defaults;
  $language_data = array_intersect_key($language_data, $defaults);

  // Insert the new language in order.
  $all_languages = $config->get('languages');
  $all_languages[$language->langcode] = (array) $language_data;
  backdrop_sort($all_languages);
  $config->set('languages', $all_languages);
  $config->save();

  if ($language->is_new) {
    module_invoke_all('language_insert', $language);
    watchdog('language', 'The %language (%langcode) language has been created.', $t_args);
  }
  else {
    module_invoke_all('language_update', $language);
    watchdog('language', 'The %language (%langcode) language has been updated.', $t_args);
  }

  if (!empty($language->default)) {
    // Set the new version of this language as default in a variable.
    config_set('system.core', 'language_default', $language->langcode);
    backdrop_static_reset('language_default');
  }

  // Update language count based on enabled language count.
  $current_languages = $config->get('languages');
  $language_count = 0;
  foreach ($current_languages as $language) {
    if ($language['enabled']) {
      $language_count++;
    }
  }
  config_set('system.core', 'language_count', $language_count);

  // Kill the static cache in language_list().
  backdrop_static_reset('language_list');

  return $language;
}