You are here

public function Language::save in Little helpers 7.2

Same name and namespace in other branches
  1. 7 src/Locale/Language.php \Drupal\little_helpers\Locale\Language::save()

Save language to the database and call hooks as needed.

Return value

$this for chaining

File

src/Locale/Language.php, line 77

Class

Language
Model object for the {languages} table.

Namespace

Drupal\little_helpers\Locale

Code

public function save() {
  $count_up = 0;
  $data = (array) $this;
  $update = FALSE;
  if (empty($this->native)) {
    $this->native = $this->name;
  }
  if ($old = self::load($this->language)) {
    unset($data['language']);
    db_update('languages')
      ->fields($data)
      ->condition('language', $this->language)
      ->execute();
    if ($this->enabled != $old->enabled) {
      $count_up = $this->enabled ? 1 : -1;
    }
    if (\language_default()->language == $this->language) {
      $this
        ->setAsDefault();
    }
    $update = TRUE;
  }
  else {
    \db_insert('languages')
      ->fields($data)
      ->execute();
    $count_up = $this->enabled ? 1 : 0;
  }
  if ($count_up != 0) {
    \variable_set('language_count', \variable_get('language_count', 1) + $count_up);
  }
  if (!$update) {

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

    // Force JavaScript translation file creation for the newly added language.
    \_locale_invalidate_js($this->language);
    \watchdog('locale', 'The %language language (%code) has been created.', array(
      '%language' => $this->name,
      '%code' => $this->language,
    ));
    \module_invoke_all('multilingual_settings_changed');
  }
  return $this;
}