FeaturesBundle.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace Drupal\features\Entity;
  3. use Drupal\Core\Config\Entity\ConfigEntityBase;
  4. use Drupal\features\FeaturesAssignmentMethodInterface;
  5. use Drupal\features\FeaturesBundleInterface;
  6. /**
  7. * Defines a features bundle.
  8. * @todo Better description
  9. *
  10. * @ConfigEntityType(
  11. * id = "features_bundle",
  12. * label = @Translation("Features bundle"),
  13. * handlers = {
  14. * },
  15. * admin_permission = "administer site configuration",
  16. * config_prefix = "bundle",
  17. * entity_keys = {
  18. * "id" = "machine_name",
  19. * "label" = "name"
  20. * },
  21. * links = {
  22. * },
  23. * config_export = {
  24. * "name",
  25. * "machine_name",
  26. * "description",
  27. * "assignments",
  28. * "profile_name",
  29. * "is_profile",
  30. * }
  31. * )
  32. */
  33. class FeaturesBundle extends ConfigEntityBase implements FeaturesBundleInterface {
  34. /**
  35. * @var string
  36. */
  37. protected $name;
  38. /**
  39. * @var
  40. */
  41. protected $machine_name;
  42. /**
  43. * @var string
  44. */
  45. protected $description;
  46. /**
  47. * @var string[]
  48. */
  49. protected $assignments = [];
  50. /**
  51. * @var string
  52. */
  53. protected $profile_name;
  54. /**
  55. * @var bool
  56. */
  57. protected $is_profile;
  58. public function id() {
  59. // @todo Convert it to $this->id in the long run.
  60. return $this->getMachineName();
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function isDefault() {
  66. return $this->machine_name == static::DEFAULT_BUNDLE;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getMachineName() {
  72. return $this->machine_name;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function setMachineName($machine_name) {
  78. $this->machine_name = $machine_name;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getName() {
  84. return $this->name;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function setName($name) {
  90. $this->name = $name;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function getFullName($short_name) {
  96. if ($this->isDefault() || $this->inBundle($short_name)) {
  97. return $short_name;
  98. }
  99. else {
  100. return $this->machine_name . '_' . $short_name;
  101. }
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function getShortName($machine_name) {
  107. if (!$this->isProfilePackage($machine_name) && $this->inBundle($machine_name)) {
  108. return substr($machine_name, strlen($this->getMachineName()) + 1, strlen($machine_name) - strlen($this->getMachineName()) - 1);
  109. }
  110. return $machine_name;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function inBundle($machine_name) {
  116. return ($this->isProfilePackage($machine_name) || strpos($machine_name, $this->machine_name . '_') === 0);
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function isProfilePackage($machine_name) {
  122. return ($this->isProfile() && $machine_name == $this->getProfileName());
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getDescription() {
  128. return $this->description;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function setDescription($description) {
  134. $this->description = $description;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function isProfile() {
  140. return $this->is_profile;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function setIsProfile($value) {
  146. $this->is_profile = $value;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function getProfileName() {
  152. $name = $this->isProfile() ? $this->profile_name : '';
  153. return !empty($name) ? $name : drupal_get_profile();
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function setProfileName($machine_name) {
  159. $this->profile_name = $machine_name;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function getEnabledAssignments() {
  165. $list = array();
  166. foreach ($this->assignments as $method_id => $method) {
  167. if ($method['enabled']) {
  168. $list[$method_id] = $method_id;
  169. }
  170. }
  171. return $list;
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function setEnabledAssignments(array $assignments) {
  177. // Add any new assignments that we don't yet know about.
  178. $new_assignments = array_diff($assignments, array_keys($this->assignments));
  179. foreach ($new_assignments as $method_id) {
  180. $this->assignments[$method_id] = $this->getAssignmentSettings($method_id);
  181. }
  182. foreach ($this->assignments as $method_id => &$method) {
  183. $method['enabled'] = in_array($method_id, $assignments);
  184. }
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function getAssignmentWeights() {
  190. $list = array();
  191. foreach ($this->assignments as $method_id => $method) {
  192. $list[$method_id] = $method['weight'];
  193. }
  194. return $list;
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function setAssignmentWeights(array $assignments) {
  200. foreach ($this->assignments as $method_id => &$method) {
  201. if (isset($assignments[$method_id])) {
  202. $method['weight'] = $assignments[$method_id];
  203. }
  204. }
  205. }
  206. /**
  207. * Return array of default settings for the given plugin method
  208. *
  209. * @param $method_id
  210. * @return array
  211. */
  212. protected function getDefaultSettings($method_id) {
  213. $settings = ['enabled' => FALSE, 'weight' => 0];
  214. $manager = \Drupal::service('plugin.manager.features_assignment_method');
  215. $definition = $manager->getDefinition($method_id);
  216. if (isset($definition['weight'])) {
  217. $settings['weight'] = $definition['weight'];
  218. }
  219. if (isset($definition['default_settings'])) {
  220. $settings += $definition['default_settings'];
  221. }
  222. return $settings;
  223. }
  224. /**
  225. * {@inheritdoc}
  226. */
  227. public function getAssignmentSettings($method_id = NULL) {
  228. if (isset($method_id)) {
  229. if (isset($this->assignments[$method_id])) {
  230. return $this->assignments[$method_id];
  231. }
  232. else {
  233. // Use defaults.
  234. return $this->getDefaultSettings($method_id);
  235. }
  236. }
  237. else {
  238. $list = array();
  239. foreach (array_keys($this->assignments) as $method_id) {
  240. $list[$method_id] = $this->getAssignmentSettings($method_id);
  241. }
  242. return $list;
  243. }
  244. }
  245. /**
  246. * {@inheritdoc}
  247. */
  248. public function setAssignmentSettings($method_id, array $settings) {
  249. if (isset($method_id)) {
  250. $this->assignments[$method_id] = $settings;
  251. }
  252. else {
  253. foreach ($settings as $method_id => $method_settings) {
  254. if (!empty($method_settings)) {
  255. $this->setAssignmentSettings($method_id, $method_settings);
  256. }
  257. }
  258. }
  259. return $this;
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. public function remove() {
  265. $this->delete();
  266. }
  267. }