Language.php 2.61 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\language\Entity\Language.
6 7
 */

8
namespace Drupal\language\Entity;
9 10

use Drupal\Core\Config\Entity\ConfigEntityBase;
11
use Drupal\Core\Entity\EntityStorageControllerInterface;
12
use Drupal\language\Exception\DeleteDefaultLanguageException;
13 14 15 16 17
use Drupal\language\LanguageInterface;

/**
 * Defines the Language entity.
 *
18
 * @ConfigEntityType(
19 20 21
 *   id = "language_entity",
 *   label = @Translation("Language"),
 *   controllers = {
22
 *     "list" = "Drupal\language\LanguageListController",
23 24
 *     "access" = "Drupal\language\LanguageAccessController",
 *     "form" = {
25 26
 *       "add" = "Drupal\language\Form\LanguageAddForm",
 *       "edit" = "Drupal\language\Form\LanguageEditForm",
27 28
 *       "delete" = "Drupal\language\Form\LanguageDeleteForm"
 *     }
29
 *   },
30
 *   admin_permission = "administer languages",
31
 *   config_prefix = "entity",
32 33 34
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
35
 *     "weight" = "weight",
36
 *     "uuid" = "uuid"
37 38
 *   },
 *   links = {
39
 *     "delete-form" = "language.delete",
40
 *     "edit-form" = "language.edit"
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
 *   }
 * )
 */
class Language extends ConfigEntityBase implements LanguageInterface {

  /**
   * The language ID (machine name).
   *
   * @var string
   */
  public $id;

  /**
   * The language UUID.
   *
   * This is assigned automatically when the language is created.
   *
   * @var string
   */
  public $uuid;

  /**
   * The human-readable label for the language.
   *
   * @var string
   */
  public $label;

  /**
   * The direction of language, either DIRECTION_LTR or DIRECTION_RTL.
   *
   * @var integer
   */
  public $direction = '';

  /**
   * The weight of the language, used in lists of languages.
   *
   * @var integer
   */
  public $weight = 0;

  /**
   * Locked languages cannot be edited.
   *
   * @var bool
   */
  public $locked = FALSE;

90 91 92 93 94 95 96 97 98 99 100
  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageControllerInterface $storage_controller) {
    parent::preSave($storage_controller);
    // Languages are picked from a predefined list which is given in English.
    // For the uncommon case of custom languages the label should be given in
    // English.
    $this->langcode = 'en';
  }

101 102 103 104 105 106 107 108 109 110 111 112 113
  /**
   * {@inheritdoc}
   *
   * @throws \RuntimeException
   */
  public static function preDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
    $default_language = \Drupal::service('language.default')->get();
    foreach ($entities as $entity) {
      if ($entity->id() == $default_language->id) {
        throw new DeleteDefaultLanguageException('Can not delete the default language');
      }
    }
  }
114
}