You are here

public function LegalLanguageSettings::legalVersionsLatestGet in Legal 8

Same name and namespace in other branches
  1. 2.0.x src/Form/LegalLanguageSettings.php \Drupal\legal\Form\LegalLanguageSettings::legalVersionsLatestGet()

Get latest version for each language.

1 call to LegalLanguageSettings::legalVersionsLatestGet()
LegalLanguageSettings::buildForm in src/Form/LegalLanguageSettings.php
Languages administration form.

File

src/Form/LegalLanguageSettings.php, line 90

Class

LegalLanguageSettings
Class LegalLanguageSettings.

Namespace

Drupal\legal\Form

Code

public function legalVersionsLatestGet($language = NULL) {
  $conditions = [];
  $current_version = $this->database
    ->select('legal_conditions', 'lc')
    ->fields('lc', [
    'version',
  ])
    ->orderBy('version', 'DESC')
    ->range(0, 1)
    ->execute()
    ->fetchField();

  // Get latest version for each language.
  if (empty($language)) {
    $languages = \Drupal::languageManager()
      ->getLanguages();
    foreach ($languages as $language_id => $language) {
      $result = $this->database
        ->select('legal_conditions', 'lc')
        ->fields('lc')
        ->condition('version', $current_version)
        ->condition('language', $language_id)
        ->orderBy('revision', 'DESC')
        ->range(0, 1)
        ->execute()
        ->fetchAllAssoc('tc_id');
      $row = count($result) ? (object) array_shift($result) : FALSE;
      $conditions[$language
        ->getId()] = $this
        ->legalVersionsLatestGetData($row);
    }
  }
  else {

    // Get latest version for specific language.
    $result = $this->database
      ->select('legal_conditions', 'lc')
      ->fields('lc')
      ->condition('language', $language)
      ->groupBy('language')
      ->orderBy('version', 'DESC')
      ->range(0, 1)
      ->execute()
      ->fetchAllAssoc('tc_id');
    $row = count($result) ? (object) array_shift($result) : FALSE;
    $conditions[$language] = $this
      ->legalVersionsLatestGetData($row);
  }
  return $conditions;
}