Share icon
variable_get and variable_set in Drupal 8

As we all know drupal 7 has some very usefull core feature which can we use to our customization of custom or core configraution, In variable_get and variable_set is also an important function to use small piece of data with create any new table in drupal.

In drupal 8 that function is removed, but some equivalent services are provided by drupal for the developer to use when its required, so let's see how you can use these in this tutorial.

In Drupal 7:

- Variable can set by using :

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

- Variable can be get by using :

$data= variable_get('your_settings_name');

In Drupal 8 :

- Set any variable like D7

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

- Get the setted variable 

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

You can set and get as many variables you like

For the better understanding we create a form and save data on submit see example below , how it works in actual d8 system

<?php

/** 
 * @file
 * Contains \Drupal\variable_get_set\Form\VariableForm
 */
namespace Drupal\variable_get_set\Form;

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

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

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

        $form['cilent_key'] = array(
            '#type' => 'textfield',
            '#title' => t('Account Name:'),
            '#required' => TRUE,
            '#default_value' => $variable_get_set_cilent_name
        );
        $form['cilent_secret'] = array(
            '#type' => 'textfield',
            '#title' => t('Account Secret'),
            '#required' => TRUE,
            '#default_value' => $variable_get_set_cilent_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) {
        $cilent_key = $form_state->getValue('cilent_key');
        $cilent_secret = $form_state->getValue('cilent_secret');

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

        if($config->save()){
            drupal_set_message("updated");
        }       
    }

}

In above we have created a custom module with form, and on submit we store values on variable and similar on form load we getting the values.

you can also access the module using GITHUB

Hopes this helps to you

thanks

Add new comment

Restricted HTML