You are here

trait LangcodeTrait in Coder 8.2

Same name and namespace in other branches
  1. 8.3 tests/DrupalPractice/good/good.php \LangcodeTrait
  2. 8.3.x tests/DrupalPractice/good/good.php \LangcodeTrait

A trait containing helper methods for language handling.

Hierarchy

File

coder_sniffer/DrupalPractice/Test/good/good.php, line 78

View source
trait LangcodeTrait {

  /**
   * Name of language.
   *
   * @var string
   */
  protected $langcode;

  /**
   * A fully-populated language object.
   *
   * @var \Drupal\core\Language\LanguageInterface|null
   */
  protected $lang;

  /**
   * Select language.
   *
   * @param string $langcode
   *   Language code.
   *
   * @return $this
   *   Current instance.
   */
  public function setLangcode($langcode) {
    $this->langcode = $langcode;
    $this->lang = \Drupal::languageManager()
      ->getLanguage($this->langcode);
    return $this;
  }

  /**
   * Get code of currently active language.
   *
   * @return string
   *   Language code.
   */
  public function getLangcode() {
    if (!isset($this->langcode)) {
      $lang = \Drupal::languageManager()
        ->getCurrentLanguage();
      $this
        ->setLangcode($lang
        ->getId());
    }
    return $this->langcode;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LangcodeTrait::$lang protected property A fully-populated language object.
LangcodeTrait::$langcode protected property Name of language.
LangcodeTrait::getLangcode public function Get code of currently active language.
LangcodeTrait::setLangcode public function Select language.