BehaviorSettingsManager.php in Rabbit Hole 2.x
File
src/BehaviorSettingsManager.php
View source
<?php
namespace Drupal\rabbit_hole;
use Drupal\Core\Config\ConfigFactory;
use Drupal\rabbit_hole\Entity\BehaviorSettings;
class BehaviorSettingsManager implements BehaviorSettingsManagerInterface {
protected $configFactory;
public function __construct(ConfigFactory $config_factory) {
$this->configFactory = $config_factory;
}
public function saveBehaviorSettings(array $settings, $entity_type_id, $entity_id = NULL) {
$id = $this
->generateBehaviorSettingsFullId($entity_type_id, $entity_id);
$entity = BehaviorSettings::load($id);
if ($entity === NULL) {
$entity_array = [
'id' => $id,
];
$entity_array += $settings;
$entity = BehaviorSettings::create($entity_array);
}
else {
foreach ($settings as $key => $setting) {
$entity
->set($key, $setting);
}
}
$entity
->set('entity_type_id', $entity_type_id);
$entity
->set('entity_id', $entity_id);
$entity
->save();
}
public function loadBehaviorSettingsAsConfig($entity_type_id, $entity_id = NULL) {
$actual = $this->configFactory
->get('rabbit_hole.behavior_settings.' . $this
->generateBehaviorSettingsFullId($entity_type_id, $entity_id));
if (!$actual
->isNew()) {
return $actual;
}
else {
return $this->configFactory
->get('rabbit_hole.behavior_settings.default');
}
}
public function loadBehaviorSettingsAsEditableConfig($entity_type_id, $entity_id, $is_bundle = FALSE) {
$actual = $this->configFactory
->getEditable('rabbit_hole.behavior_settings.' . $this
->generateBehaviorSettingsFullId($entity_type_id, $entity_id));
return !$actual
->isNew() ? $actual : NULL;
}
private function generateBehaviorSettingsFullId($entity_type_id, $entity_id = '') {
return $entity_type_id . (isset($entity_id) ? '_' . $entity_id : '');
}
}