You are here

class BehaviorSettingsManager in Rabbit Hole 8

Same name and namespace in other branches
  1. 2.x src/BehaviorSettingsManager.php \Drupal\rabbit_hole\BehaviorSettingsManager

Provides operations for bundles configuration.

Hierarchy

Expanded class hierarchy of BehaviorSettingsManager

1 string reference to 'BehaviorSettingsManager'
rabbit_hole.services.yml in ./rabbit_hole.services.yml
rabbit_hole.services.yml
1 service uses BehaviorSettingsManager
rabbit_hole.behavior_settings_manager in ./rabbit_hole.services.yml
Drupal\rabbit_hole\BehaviorSettingsManager

File

src/BehaviorSettingsManager.php, line 11

Namespace

Drupal\rabbit_hole
View source
class BehaviorSettingsManager implements BehaviorSettingsManagerInterface {

  /**
   * Drupal\Core\Config\ConfigFactory definition.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Constructor.
   */
  public function __construct(ConfigFactory $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  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');
    }
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Generate a full ID based on entity type label, bundle label and entity id.
   *
   * @param string $entity_type_id
   *   The entity type (e.g. node) as a string.
   * @param string $entity_id
   *   The entity ID as a string.
   *
   * @return string
   *   The full id appropriate for a BehaviorSettings config entity.
   */
  private function generateBehaviorSettingsFullId($entity_type_id, $entity_id = '') {
    return $entity_type_id . (isset($entity_id) ? '_' . $entity_id : '');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BehaviorSettingsManager::$configFactory protected property Drupal\Core\Config\ConfigFactory definition.
BehaviorSettingsManager::generateBehaviorSettingsFullId private function Generate a full ID based on entity type label, bundle label and entity id.
BehaviorSettingsManager::loadBehaviorSettingsAsConfig public function Load behaviour settings for an entity or bundle, or load the defaults. Overrides BehaviorSettingsManagerInterface::loadBehaviorSettingsAsConfig
BehaviorSettingsManager::loadBehaviorSettingsAsEditableConfig public function Load behaviour settings for an entity or bundle, or return NULL. Overrides BehaviorSettingsManagerInterface::loadBehaviorSettingsAsEditableConfig
BehaviorSettingsManager::saveBehaviorSettings public function Save behavior settings for an entity or bundle. Overrides BehaviorSettingsManagerInterface::saveBehaviorSettings
BehaviorSettingsManager::__construct public function Constructor.