FeaturesBundle.php in Features 8.3
File
src/Entity/FeaturesBundle.php
View source
<?php
namespace Drupal\features\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\features\FeaturesBundleInterface;
use Drupal\Core\Site\Settings;
class FeaturesBundle extends ConfigEntityBase implements FeaturesBundleInterface {
protected $name;
protected $machine_name;
protected $description;
protected $assignments = [];
protected $profile_name;
protected $is_profile = FALSE;
public function id() {
return $this
->getMachineName();
}
public function isDefault() {
return $this->machine_name == static::DEFAULT_BUNDLE;
}
public function getMachineName() {
return $this->machine_name;
}
public function setMachineName($machine_name) {
$this->machine_name = $machine_name;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getFullName($short_name) {
if ($this
->isDefault() || $this
->inBundle($short_name) || $this
->isProfilePackage($short_name)) {
return $short_name;
}
else {
return $this->machine_name . '_' . $short_name;
}
}
public function getShortName($machine_name) {
if (!$this
->isProfilePackage($machine_name) && $this
->inBundle($machine_name)) {
return substr($machine_name, strlen($this
->getMachineName()) + 1, strlen($machine_name) - strlen($this
->getMachineName()) - 1);
}
return $machine_name;
}
public function inBundle($machine_name) {
return $this
->isProfilePackage($machine_name) || strpos($machine_name, $this->machine_name . '_') === 0;
}
public function isProfilePackage($machine_name) {
return $this
->isProfile() && $machine_name == $this
->getProfileName();
}
public function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = $description;
}
public function isProfile() {
return $this->is_profile;
}
public function setIsProfile($value) {
$this->is_profile = $value;
}
public function getProfileName() {
$name = $this
->isProfile() ? $this->profile_name : '';
return !empty($name) ? $name : Settings::get('install_profile');
}
public function setProfileName($machine_name) {
$this->profile_name = $machine_name;
}
public function getEnabledAssignments() {
$list = [];
foreach ($this->assignments as $method_id => $method) {
if ($method['enabled']) {
$list[$method_id] = $method_id;
}
}
return $list;
}
public function setEnabledAssignments(array $assignments) {
$new_assignments = array_diff($assignments, array_keys($this->assignments));
foreach ($new_assignments as $method_id) {
$this->assignments[$method_id] = $this
->getAssignmentSettings($method_id);
}
foreach ($this->assignments as $method_id => &$method) {
$method['enabled'] = in_array($method_id, $assignments);
}
}
public function getAssignmentWeights() {
$list = [];
foreach ($this->assignments as $method_id => $method) {
$list[$method_id] = $method['weight'];
}
return $list;
}
public function setAssignmentWeights(array $assignments) {
foreach ($this->assignments as $method_id => &$method) {
if (isset($assignments[$method_id])) {
$method['weight'] = $assignments[$method_id];
}
}
}
protected function getDefaultSettings($method_id) {
$settings = [
'enabled' => FALSE,
'weight' => 0,
];
$manager = \Drupal::service('plugin.manager.features_assignment_method');
$definition = $manager
->getDefinition($method_id);
if (isset($definition['weight'])) {
$settings['weight'] = $definition['weight'];
}
if (isset($definition['default_settings'])) {
$settings += $definition['default_settings'];
}
return $settings;
}
public function getAssignmentSettings($method_id = NULL) {
if (isset($method_id)) {
if (isset($this->assignments[$method_id])) {
return $this->assignments[$method_id];
}
else {
return $this
->getDefaultSettings($method_id);
}
}
else {
$list = [];
foreach (array_keys($this->assignments) as $method_id) {
$list[$method_id] = $this
->getAssignmentSettings($method_id);
}
return $list;
}
}
public function setAssignmentSettings($method_id, array $settings) {
if (isset($method_id)) {
$this->assignments[$method_id] = $settings;
}
else {
foreach ($settings as $method_id => $method_settings) {
if (!empty($method_settings)) {
$this
->setAssignmentSettings($method_id, $method_settings);
}
}
}
return $this;
}
public function remove() {
$this
->delete();
}
}