Drupal 8/9 Replacement Services for variable_get and variable_set

lakshmi , Credit to  volkotech-solutions Dec 12

Drupal 7 has core features for customizing, including functions variable_get & variable_set. Drupal 8 doesn't have these functions but offers replacement services.

In this blog, we'll go over how to use these services with an example.

In Drupal 7:

The variable can be set by using the variable_set() function.

variable_set('your_settings_name', 'values you want to save ');

The variable can be obtained by using the variable_get() function.

$data= variable_get('your_settings_name');

In Drupal 8/9:

Variable can be set by using some API services.

$config = \Drupal::service('config.factory')->getEditable('variable_get_set.api');
$config->set('cilent_name', 'value for client_name');
$config->save();

Variable can be obtained by using some API services.

$config = \Drupal::service('config.factory')->getEditable('variable_get_set.api');
$variable_get_set_cilent_name =  $config->get('cilent_name');

Any number of variables can be set and obtained. See the example below to see how it functions in the actual d8/d9 system. For a better understanding, we create a form and save data when it is submitted.

Create a .info.yml file:

Let's name our custom module "getsetvariable". Create the folder /modules/custom/getsetvariable. In this folder, we'll need to create a getsetvariable.info.yml file and add the following code.

name: Get and Set Variables
type: module
description: You can set and get as many variables as you like.
package: Custom
core_version_requirement: ^8.8.0 || ^9

Enable a module:

For all possible ways to enable a module click here.

Using the Manage administrative menu, navigate to the Extend page in the list of modules, search the Get and Set Variables module and then select its checkbox. Scroll down to the bottom of the webpage, and then click Install, then the Custom module getsetvariable has been enabled successfully.

Declaring a route:

The routing information is saved in getsetvariable/getsetvariable.routing.yml.

getsetvariable.get_set_variable_form:
  path: '/getsetvariable/form'
  defaults:
    _form: '\Drupal\getsetvariable\Form\GetSetVariablesForm'
    _title: 'GetSetVariablesForm'
  requirements:
    _access: 'TRUE'

Create a form:

Create a new subfolder "modules/custom/getsetvariable/src/Form." Create a file called "GetSetVariablesForm.php" in this folder with the following content and in this, just a submit button is required on the form to process the request.

<?php
namespace Drupal\getsetvariable\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class GetSetVariablesForm extends FormBase {
 /**
  * {@inheritdoc}
  */
 public function getFormId() {
   return 'get_set_variable_form';
 }

  /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state) {
     $config = \Drupal::service('config.factory')->getEditable('variable_get_set.api');
     $variable_get_set_user_name =  $config->get('user_name');
     $variable_get_set_user_secret =  $config->get('user_secret');

     $form['user_key'] = array(
         '#type' => 'textfield',
         '#title' => t('User Name:'),
         '#required' => TRUE,
         '#default_value' => $variable_get_set_user_name
     );
     $form['user_secret'] = array(
         '#type' => 'textfield',
         '#title' => t('User Secret'),
         '#required' => TRUE,
         '#default_value' => $variable_get_set_user_secret
     );
     $form['actions']['#type'] = 'actions';
     $form['actions']['submit'] = array(
         '#type' => 'submit',
         '#value' => $this->t('Save'),
         '#button_type' => 'primary',
     );
     return $form;
 }

 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state) {

 }

 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state) {
     $user_key = $form_state->getValue('user_key');
     $user_secret = $form_state->getValue('user_secret');

     $config = \Drupal::service('config.factory')->getEditable('variable_get_set.api');
     $config->set('user_name', $user_key);
     $config->set('user_secret', $user_secret);
     $config->save();

     if($config->save()){
      \Drupal::messenger()->addMessage("updated");
     }       
 }

}

After clearing the cache, then navigate to the '/getsetvariable/form', the output should be,

getsetvariables

Above, we've create a custom module with a form. When the form is submitted, values are stored in a variable, and when the form is loaded, the values are retrieved similarly.

Comments