Created
April 4, 2019 13:37
-
-
Save mattschaff/9059218e42597d8041d27a3f932332e5 to your computer and use it in GitHub Desktop.
Drupal 8: Custom service that gets and sets admin variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\my_module\Service; | |
use Drupal\Core\Database\Driver\mysql\Connection; | |
/** | |
* Service that peforms administration work for My Module | |
* | |
* This feature allows you to have settings outside of standard Drupal config | |
*/ | |
class CustomAdminSettingService { | |
/** | |
* database service | |
* | |
* @var \Drupal\Core\Database\Driver\mysql\Connection | |
*/ | |
protected $databaseService; | |
/** | |
* Constructs Admin object. | |
* | |
* @param \Drupal\Core\Database\Driver\mysql\Connection | |
*/ | |
public function __construct(Connection $databaseService) { | |
$this->databaseService = $databaseService; | |
} | |
/** | |
* Returns a setting from database | |
* | |
* @return string $setting | |
*/ | |
public function getAdminSettingDB($name){ | |
$setting = $this->databaseService->select('my_module_settings_table', 'm') | |
->fields('m', ['data']) | |
->condition('m.name', $name) | |
->execute()->fetchField(); | |
$setting = unserialize($setting); | |
return $setting; | |
} | |
/** | |
* Inserts or updates a variables in My Module settings table | |
* | |
* @return string $auth_header | |
*/ | |
public function setAdminSettingDB($name, $value){ | |
// get setting | |
$setting = $this->getAdminSettingDB($name); | |
// set setting | |
if (!empty($setting)){ // update if it is set | |
$query = $this->databaseService->update('my_module_settings_table') | |
->fields([ | |
'data' => serialize($value), | |
]) | |
->condition('name', $name) | |
->execute(); | |
} | |
else { // insert if it is NOT set | |
$query = $this->databaseService->insert('my_module_settings_table') | |
->fields([ | |
'data' => serialize($value), | |
'name' => $name, | |
]) | |
->execute(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Installation routins for My Module | |
*/ | |
/** | |
* Implements hook_schema(). | |
* | |
* Defines the database-only table used by My Module | |
* | |
* @see hook_schema() | |
* | |
* @ingroup ti_sync | |
*/ | |
function my_module_schema() { | |
$schema['my_module_settings_table'] = [ | |
'description' => 'Database-only settings for My Module.', | |
'fields' => [ | |
'id' => [ | |
'type' => 'serial', | |
'not null' => TRUE, | |
'description' => 'Primary Key: Unique ID of the setting.', | |
], | |
'name' => [ | |
'type' => 'varchar', | |
'length' => 255, | |
'not null' => TRUE, | |
'description' => 'Name of the setting.', | |
], | |
'data' => [ | |
'type' => 'text', | |
'description' => 'Data of the setting.', | |
], | |
], | |
'primary key' => ['id'], | |
'indexes' => [ | |
'name' => ['name'], | |
], | |
]; | |
return $schema; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment