annotate core/modules/language/src/Entity/ContentLanguageSettings.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\language\Entity;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\Entity\ConfigEntityBase;
Chris@0 6 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 7 use Drupal\Core\Language\LanguageInterface;
Chris@0 8 use Drupal\language\ContentLanguageSettingsException;
Chris@0 9 use Drupal\language\ContentLanguageSettingsInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Defines the ContentLanguageSettings entity.
Chris@0 13 *
Chris@0 14 * @ConfigEntityType(
Chris@0 15 * id = "language_content_settings",
Chris@0 16 * label = @Translation("Content Language Settings"),
Chris@17 17 * label_collection = @Translation("Content Language Settings"),
Chris@17 18 * label_singular = @Translation("content language setting"),
Chris@17 19 * label_plural = @Translation("content languages settings"),
Chris@17 20 * label_count = @PluralTranslation(
Chris@17 21 * singular = "@count content language setting",
Chris@17 22 * plural = "@count content languages settings",
Chris@17 23 * ),
Chris@0 24 * admin_permission = "administer languages",
Chris@0 25 * config_prefix = "content_settings",
Chris@0 26 * entity_keys = {
Chris@0 27 * "id" = "id"
Chris@0 28 * },
Chris@18 29 * config_export = {
Chris@18 30 * "id",
Chris@18 31 * "target_entity_type_id",
Chris@18 32 * "target_bundle",
Chris@18 33 * "default_langcode",
Chris@18 34 * "language_alterable",
Chris@18 35 * },
Chris@0 36 * list_cache_tags = { "rendered" }
Chris@0 37 * )
Chris@0 38 */
Chris@0 39 class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguageSettingsInterface {
Chris@0 40
Chris@0 41 /**
Chris@0 42 * The id. Combination of $target_entity_type_id.$target_bundle.
Chris@0 43 *
Chris@0 44 * @var string
Chris@0 45 */
Chris@0 46 protected $id;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * The entity type ID (machine name).
Chris@0 50 *
Chris@0 51 * @var string
Chris@0 52 */
Chris@0 53 protected $target_entity_type_id;
Chris@0 54
Chris@0 55 /**
Chris@0 56 * The bundle (machine name).
Chris@0 57 *
Chris@0 58 * @var string
Chris@0 59 */
Chris@0 60 protected $target_bundle;
Chris@0 61
Chris@0 62 /**
Chris@0 63 * The default language code.
Chris@0 64 *
Chris@0 65 * @var string
Chris@0 66 */
Chris@0 67 protected $default_langcode = LanguageInterface::LANGCODE_SITE_DEFAULT;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Indicates if the language is alterable or not.
Chris@0 71 *
Chris@0 72 * @var bool
Chris@0 73 */
Chris@0 74 protected $language_alterable = FALSE;
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Constructs a ContentLanguageSettings object.
Chris@0 78 *
Chris@0 79 * In most cases, Field entities are created via
Chris@0 80 * FieldConfig::create($values), where $values is the same
Chris@0 81 * parameter as in this constructor.
Chris@0 82 *
Chris@0 83 * @param array $values
Chris@0 84 * An array of the referring entity bundle with:
Chris@0 85 * - target_entity_type_id: The entity type.
Chris@0 86 * - target_bundle: The bundle.
Chris@0 87 * Other array elements will be used to set the corresponding properties on
Chris@0 88 * the class; see the class property documentation for details.
Chris@0 89 *
Chris@0 90 * @see entity_create()
Chris@0 91 */
Chris@0 92 public function __construct(array $values, $entity_type = 'language_content_settings') {
Chris@0 93 if (empty($values['target_entity_type_id'])) {
Chris@0 94 throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_entity_type_id.');
Chris@0 95 }
Chris@0 96 if (empty($values['target_bundle'])) {
Chris@0 97 throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_bundle.');
Chris@0 98 }
Chris@0 99 parent::__construct($values, $entity_type);
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * {@inheritdoc}
Chris@0 104 */
Chris@0 105 public function id() {
Chris@0 106 return $this->target_entity_type_id . '.' . $this->target_bundle;
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * {@inheritdoc}
Chris@0 111 */
Chris@0 112 public function getTargetEntityTypeId() {
Chris@0 113 return $this->target_entity_type_id;
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * {@inheritdoc}
Chris@0 118 */
Chris@0 119 public function getTargetBundle() {
Chris@0 120 return $this->target_bundle;
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * {@inheritdoc}
Chris@0 125 */
Chris@0 126 public function setTargetBundle($target_bundle) {
Chris@0 127 $this->target_bundle = $target_bundle;
Chris@0 128
Chris@0 129 return $this;
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * {@inheritdoc}
Chris@0 134 */
Chris@0 135 public function setDefaultLangcode($default_langcode) {
Chris@0 136 $this->default_langcode = $default_langcode;
Chris@0 137
Chris@0 138 return $this;
Chris@0 139 }
Chris@0 140
Chris@0 141 /**
Chris@0 142 * {@inheritdoc}
Chris@0 143 */
Chris@0 144 public function getDefaultLangcode() {
Chris@0 145 return $this->default_langcode;
Chris@0 146 }
Chris@0 147
Chris@0 148 /**
Chris@0 149 * {@inheritdoc}
Chris@0 150 */
Chris@0 151 public function setLanguageAlterable($language_alterable) {
Chris@0 152 $this->language_alterable = $language_alterable;
Chris@0 153
Chris@0 154 return $this;
Chris@0 155 }
Chris@0 156
Chris@0 157 /**
Chris@0 158 * {@inheritdoc}
Chris@0 159 */
Chris@0 160 public function isLanguageAlterable() {
Chris@0 161 return $this->language_alterable;
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * {@inheritdoc}
Chris@0 166 */
Chris@0 167 public function preSave(EntityStorageInterface $storage) {
Chris@0 168 $this->id = $this->id();
Chris@0 169 parent::preSave($storage);
Chris@0 170 }
Chris@0 171
Chris@0 172 /**
Chris@0 173 * {@inheritdoc}
Chris@0 174 */
Chris@0 175 public function isDefaultConfiguration() {
Chris@0 176 return (!$this->language_alterable && $this->default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT);
Chris@0 177 }
Chris@0 178
Chris@0 179 /**
Chris@0 180 * Loads a content language config entity based on the entity type and bundle.
Chris@0 181 *
Chris@0 182 * @param string $entity_type_id
Chris@0 183 * ID of the entity type.
Chris@0 184 * @param string $bundle
Chris@0 185 * Bundle name.
Chris@0 186 *
Chris@0 187 * @return $this
Chris@0 188 * The content language config entity if one exists. Otherwise, returns
Chris@0 189 * default values.
Chris@0 190 */
Chris@0 191 public static function loadByEntityTypeBundle($entity_type_id, $bundle) {
Chris@0 192 if ($entity_type_id == NULL || $bundle == NULL) {
Chris@0 193 return NULL;
Chris@0 194 }
Chris@0 195 $config = \Drupal::entityManager()->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle);
Chris@0 196 if ($config == NULL) {
Chris@0 197 $config = ContentLanguageSettings::create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]);
Chris@0 198 }
Chris@0 199 return $config;
Chris@0 200 }
Chris@0 201
Chris@0 202 /**
Chris@0 203 * {@inheritdoc}
Chris@0 204 */
Chris@0 205 public function calculateDependencies() {
Chris@0 206 parent::calculateDependencies();
Chris@0 207
Chris@0 208 // Create dependency on the bundle.
Chris@0 209 $entity_type = \Drupal::entityManager()->getDefinition($this->target_entity_type_id);
Chris@0 210 $bundle_config_dependency = $entity_type->getBundleConfigDependency($this->target_bundle);
Chris@0 211 $this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
Chris@0 212
Chris@0 213 return $this;
Chris@0 214 }
Chris@0 215
Chris@0 216 }