ContentLanguageSettings.php 5.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

namespace Drupal\language\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\ContentLanguageSettingsException;
use Drupal\language\ContentLanguageSettingsInterface;

/**
 * Defines the ContentLanguageSettings entity.
 *
 * @ConfigEntityType(
 *   id = "language_content_settings",
 *   label = @Translation("Content Language Settings"),
17 18 19 20 21 22 23
 *   label_collection = @Translation("Content Language Settings"),
 *   label_singular = @Translation("content language setting"),
 *   label_plural = @Translation("content languages settings"),
 *   label_count = @PluralTranslation(
 *     singular = "@count content language setting",
 *     plural = "@count content languages settings",
 *   ),
24 25 26 27 28
 *   admin_permission = "administer languages",
 *   config_prefix = "content_settings",
 *   entity_keys = {
 *     "id" = "id"
 *   },
29 30 31 32 33 34 35
 *   config_export = {
 *     "id",
 *     "target_entity_type_id",
 *     "target_bundle",
 *     "default_langcode",
 *     "language_alterable",
 *   },
36
 *   list_cache_tags = { "rendered" }
37 38 39 40 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
 * )
 */
class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguageSettingsInterface {

  /**
   * The id. Combination of $target_entity_type_id.$target_bundle.
   *
   * @var string
   */
  protected $id;

  /**
   * The entity type ID (machine name).
   *
   * @var string
   */
  protected $target_entity_type_id;

  /**
   * The bundle (machine name).
   *
   * @var string
   */
  protected $target_bundle;

  /**
   * The default language code.
   *
   * @var string
   */
  protected $default_langcode = LanguageInterface::LANGCODE_SITE_DEFAULT;

  /**
   * Indicates if the language is alterable or not.
   *
   * @var bool
   */
  protected $language_alterable = FALSE;

  /**
   * Constructs a ContentLanguageSettings object.
   *
   * In most cases, Field entities are created via
80
   * FieldConfig::create($values), where $values is the same
81 82 83 84 85 86 87 88
   * parameter as in this constructor.
   *
   * @param array $values
   *   An array of the referring entity bundle with:
   *   - target_entity_type_id: The entity type.
   *   - target_bundle: The bundle.
   *   Other array elements will be used to set the corresponding properties on
   *   the class; see the class property documentation for details.
89 90 91
   * @param string $entity_type
   *   (optional) The entity type ID that is being created. Defaults to
   *   "language_content_settings".
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
   */
  public function __construct(array $values, $entity_type = 'language_content_settings') {
    if (empty($values['target_entity_type_id'])) {
      throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_entity_type_id.');
    }
    if (empty($values['target_bundle'])) {
      throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_bundle.');
    }
    parent::__construct($values, $entity_type);
  }

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->target_entity_type_id . '.' . $this->target_bundle;
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetEntityTypeId() {
    return $this->target_entity_type_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetBundle() {
    return $this->target_bundle;
  }

  /**
   * {@inheritdoc}
   */
  public function setTargetBundle($target_bundle) {
    $this->target_bundle = $target_bundle;

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setDefaultLangcode($default_langcode) {
    $this->default_langcode = $default_langcode;

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultLangcode() {
    return $this->default_langcode;
  }

  /**
   * {@inheritdoc}
   */
  public function setLanguageAlterable($language_alterable) {
    $this->language_alterable = $language_alterable;

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isLanguageAlterable() {
    return $this->language_alterable;
  }

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    $this->id = $this->id();
    parent::preSave($storage);
  }

  /**
   * {@inheritdoc}
   */
  public function isDefaultConfiguration() {
    return (!$this->language_alterable && $this->default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT);
  }

  /**
   * Loads a content language config entity based on the entity type and bundle.
   *
   * @param string $entity_type_id
   *   ID of the entity type.
   * @param string $bundle
   *   Bundle name.
   *
   * @return $this
   *   The content language config entity if one exists. Otherwise, returns
   *   default values.
   */
  public static function loadByEntityTypeBundle($entity_type_id, $bundle) {
    if ($entity_type_id == NULL || $bundle == NULL) {
      return NULL;
    }
196
    $config = \Drupal::entityTypeManager()->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle);
197 198 199 200 201 202 203 204 205 206 207
    if ($config == NULL) {
      $config = ContentLanguageSettings::create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]);
    }
    return $config;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();
208 209

    // Create dependency on the bundle.
210
    $entity_type = \Drupal::entityTypeManager()->getDefinition($this->target_entity_type_id);
211 212 213
    $bundle_config_dependency = $entity_type->getBundleConfigDependency($this->target_bundle);
    $this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);

214
    return $this;
215 216 217
  }

}