You are here

class DashboardSettingsService in Content Planner 8

Class DashboardSettingsService.

Hierarchy

Expanded class hierarchy of DashboardSettingsService

3 files declare their use of DashboardSettingsService
DashboardBlockConfigForm.php in src/Form/DashboardBlockConfigForm.php
DashboardController.php in src/Controller/DashboardController.php
DashboardSettingsForm.php in src/Form/DashboardSettingsForm.php
1 string reference to 'DashboardSettingsService'
content_planner.services.yml in ./content_planner.services.yml
content_planner.services.yml
1 service uses DashboardSettingsService
content_planner.dashboard_settings_service in ./content_planner.services.yml
Drupal\content_planner\DashboardSettingsService

File

src/DashboardSettingsService.php, line 10

Namespace

Drupal\content_planner
View source
class DashboardSettingsService {

  /**
   * Config name.
   */
  const CONFIG_NAME = 'content_planner.dashboard_settings';

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

  /**
   * Constructs a new DashboardSettingsService object.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * Gets the complete settings config of the module.
   *
   * @return \Drupal\Core\Config\ImmutableConfig
   *   The complete settings config of the module.
   */
  public function getSettings() {
    return $this->configFactory
      ->get(self::CONFIG_NAME);
  }

  /**
   * Get Block configurations.
   *
   * @return array
   *   All block configurations.
   */
  public function getBlockConfigurations() {
    if ($settings = $this
      ->getSettings()) {
      if ($block_configurations = $settings
        ->get('blocks')) {
        return $block_configurations;
      }
    }
    return [];
  }

  /**
   * Get the configuration of a specific block.
   *
   * @param string $block_id
   *   The block id to retrieve the config for.
   *
   * @return array|mixed
   *   The block configuration.
   */
  public function getBlockConfiguration($block_id) {
    if ($block_configurations = $this
      ->getBlockConfigurations()) {
      if (array_key_exists($block_id, $block_configurations)) {
        return $block_configurations[$block_id];
      }
    }
    return [];
  }

  /**
   * Save configuration of a specific block.
   *
   * @param string $block_id
   *   The block id to save the config for.
   * @param array $configuration
   *   The config to save.
   *
   * @return bool
   *   TRUE if config was found and saved, FALSE otherwise.
   */
  public function saveBlockConfiguration($block_id, array $configuration) {
    if ($block_configurations = $this
      ->getBlockConfigurations()) {
      if (array_key_exists($block_id, $block_configurations)) {
        $block_configurations[$block_id] = $configuration;
        $this
          ->saveBlockConfigurations($block_configurations);
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Save all block configurations.
   *
   * @param array $configuration
   *   The config to save.
   */
  public function saveBlockConfigurations(array $configuration) {
    $this->configFactory
      ->getEditable(self::CONFIG_NAME)
      ->set('blocks', $configuration)
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DashboardSettingsService::$configFactory protected property Drupal\Core\Config\ConfigFactoryInterface definition.
DashboardSettingsService::CONFIG_NAME constant Config name.
DashboardSettingsService::getBlockConfiguration public function Get the configuration of a specific block.
DashboardSettingsService::getBlockConfigurations public function Get Block configurations.
DashboardSettingsService::getSettings public function Gets the complete settings config of the module.
DashboardSettingsService::saveBlockConfiguration public function Save configuration of a specific block.
DashboardSettingsService::saveBlockConfigurations public function Save all block configurations.
DashboardSettingsService::__construct public function Constructs a new DashboardSettingsService object.