Configuring a site (variable-set and variable-get)

Once logged in as the administrator in our newly created site, it is time to configure its basic behavior. We could navigate through each of the configuration pages, choosing what best fits our purpose. However, there are some scenarios where you know which settings you want to set and do not want to go log in and go through each of the configuration pages, where they are located, to change them. For example, imagine that you have just downloaded a production database, and every time you do this you have to change a few settings to block unwanted reactions while working with the site (such as disabling SMTP e-mail submission). The commands variable-set, variable-get, and variable-delete allow you to configure, view, and delete Drupal configuration variables stored at the variable table.

Listing available variables

First of all, let's see what variables are already set in the variable table of our site's database:

$ cd /home/juampy/drupal
$ drush variable-get
admin_theme: "seven"
clean_url: TRUE
comment_page: 0
cron_key: "zRL-wZQLsgnh0JXkjs3tylQkmvFxRwF-tnH88nUWWs8"
cron_last: 1324731815
css_js_query_string: "lwpllz"
date_default_timezone: "Europe/London"
drupal_http_request_fails: FALSE
...

The list is not too long, but, that is because Drupal only sets the most important ones during installation. The rest will use their default values, which are provided when calling the function variable_get(). If you list the variables of a mature site, you will see many more.

You can filter which variables you want to see by giving part of the variable name as an argument to the command. For example, we could list all the user-related variables such as this:

$ drush variable-get user
user_admin_role: "3"
user_email_verification: "0"
user_pictures: "1"
user_picture_dimensions: "1024x1024"
user_picture_file_size: "800"
user_picture_style: "thumbnail"
user_register: "1"

Setting new values to variables

A very common use case with the variable-set command is to use it to turn on and off Drupal's Maintenance mode. This mode is set through a variable and it is very easy to manipulate its value without having to log in as the administrator and set it in the administration interface. Here is how we can turn on maintenance mode in a Drupal site with a command:

$ cd /home/juampy/projects/drupal
$ drush variable-set maintenance_mode 1
Enter a number to choose which variable to set.
[0] : Cancel
[1] : maintenance_mode (new variable)
1
maintenance_mode was set to 1. [success]

The command printed two options: one for canceling the action and other for creating a new variable called maintenance_mode with a value of 1. The fact is that if a site has never been set to maintenance mode, the variable does not exist and that is why the new variable text appears. Try to open your Drupal site to verify that it shows a maintenance message. If you want to turn it off, set the value of the variable to 0:

$ drush variable-set maintenance_mode 0
Enter a number to choose which variable to set.
[0] : Cancel
[1] : maintenance_mode
1
maintenance_mode was set to 0. [success]

Note

Drupal 6 users should use the variable name site_offline instead of maintenance_mode, as the latter is for Drupal 7.

Now let's set up a few more variables with new values. We will set up the following:

  • The default time zone to be Madrid
  • The default country to be Spain
  • The administrator's e-mail to be a different e-mail account

Here is an example that illustrates how to achieve these requirements:

$ drush variable-set --always-set date_default_timezone "Europe/Madrid"
date_default_timezone was set to Europe/Madrid. [success]
$ drush variable-set --always-set date_default_country "ES"
date_default_country was set to ES. [success]
$ drush variable-set --always-set site_mail "[email protected]"
site_mail was set to [email protected]. [success]

As you can see, it's a matter of providing the variable name and the desired value to the command. In order to bypass confirmation messages to change the variables, we have added the --always-set option.

Now, we are going to set variables that have not been set up yet (hence, they currently do not exist in the variables table). We first need to identify each variable name and the correct value, that we want from the page where it can be set in the administration panel of our Drupal site. Instead of filling it there, we are going to grab its name and do it from the command line. This way will teach you how to set other variables in the future. For this example, we are going to let anyone register on our site, and will not require the Administrator's approval for registered accounts. First, of all we need to identify the name and desired value of these two settings. Here is how:

  1. Log in to your site as the administrator and open the account configuration at Configuration | Account Settings.
  2. With a development tool such as Firefox's Firebug (https://addons.mozilla.org/en-US/firefox/addon/firebug/) or the built-in Chrome Developer Tools, inspect the radio option of Visitors, which is under REGISTRATION AND CANCELLATION | Who can register accounts?

    A frame with this piece of HTML will appear at the bottom as shown in the following screenshot:

    Setting new values to variables
  3. The following highlighted HTML code contains what we are looking for:
    <input type="radio" class="form-radio" checked="checked" value="1" name="user_register" id="edit-user-register-1">
    
  4. You can see in the previous code that the attribute name has a value of user_register and the attribute value has a value of 1. However, the checked radio is the third one (Visitors, but administration approval is required), which means that currently user_register has a value of 2.
  5. For the case of the Require e-mail verification when a visitor creates an account. checkbox, the following HTML code represents it:
    <input type="checkbox" class="form-checkbox" value="1" name="user_email_verification" id="edit-user-email-verification">
    
  6. As we can see the attribute name has a value of user_email_verification and the attribute value has a value of 1. This means that currently user_email_verification has a value of 1.
  7. We have identified the variables that we want to set. Now, we are going to change them: user_register will have a value of 1 in order to allow visitors to register in the site, and user_email_verification will have a value of 0 in order to disable e-mail verification on user registration. Let's see the values in the command line, update them, and then print their new values:
    $ drush variable-get user_register
    user_register: 2
    $ drush variable-get user_email_verification
    No matching variable found. [error]
    $ drush variable-set --yes user_register 1
    user_register was set to 1. [success]
    $ drush variable-set --yes user_email_verification 0
    user_email_verification was set to 0. [success]
    $ drush variable-get user_register
    user_register: "1"
    $ drush variable-get user_email_verification
    user_email_verification: "0"
    
    
  8. In the previous example, we have first checked the values of these variables. Note that user_email_verification did not even exist yet. This means that it was using the default value given by the source code and not from the database. Then, we set their values and printed them again to verify that they were changed. If you open the same page again in your browser, you will see that these settings have been updated accordingly in the configuration form. As you see, some knowledge of HTML forms is needed in order to find out the correct value of each variable.

Tip

Features module and installation profiles are good strategies to configure Drupal sites, they are both covered in Chapter 4,Extending Drush.

Deleting variables

If we ever wanted to completely delete the value of a variable, we would use variable-delete. For example, if we need to delete the variable user_email_verification so it uses its default value again, then this would mean executing the following command:

$ drush variable-delete --yes user_email_verification
user_email_verification was deleted. [success]

Finding variables by name

All variable-x commands will help you to find the desired variable if you just type part of its name. This means that you do not need to enter the exact variable name, as Drush will try to find all the variables that contain part of the argument given. For example, imagine that we want to change the user_picture dimensions to a smaller scale, but we are not sure which is the exact variable name:

$ drush variable-set user_picture "800x600"
Enter a number to choose which variable to set.
[0] : Cancel
[1] : user_picture (new variable)
[2] : user_picture_dimensions
[3] : user_picture_file_size
[4] : user_picture_style
2
user_picture_dimensions was set to 800x600 [success]

This example demonstrates how Drush lists a set of options where you can either Cancel the operation (option 0), set the value to a new variable called user_picture (option 1), or set it to any of the others listed (options 2, 3, and 4). In this case, option 2 was the one we were looking for. Typing 2 and then pressing Enter, committed the variable-set command.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.100.42