Replace Tokens Module for Drupal 7

A simple module used to add variables to main Drupal 'variable' table and use tokens in text that will be replaced with the content of those variables.

Download module replace_vars-7.x-1.0.zip

Files content

replace_vars.info

name = Replace Variables
description = "Replace variables used all over the site"
package = "Custom"

core = 7.x

files[] = replace_vars.module
files[] = replace_vars.admin.inc

configure = admin/config/user-interface/replace_vars/settings

replace_vars.module

<?php
// $Id: replace_vars.module,v 1.0 2012-10-09 $

/**
 * @file
 * Replace variables used in text all over the site
 * you may add new variables
 * all these custom variables have a prefix before
 * @author
 */


DEFINE('REPLACE_VARS_PREFIX', 'rvars_');

/**
 * Implementation of hook_help().
 */
function replace_vars_help($path, $arg) {
    switch ($path) {
        case 'admin/help#replace_vars':
        return t('<p>Replace variables, use {{variable_name}} for data that will be replaced by values stored in the configuration table.</p>');
    }
}

// add module to configuration menu
function replace_vars_menu() {

    $items = array();

    $items['admin/config/user-interface/replace_vars'] = array(
    'title' => 'Replace vars list',
    'description' => '',
    'page callback' => 'replace_vars_page',
    'access arguments' => array('administer replace_vars'),
    'file' => 'replace_vars.admin.inc',
    );

    $items['admin/config/user-interface/replace_vars/add'] = array(
    'title' => 'Add vars',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('replace_vars_form'),
    'access arguments' => array('administer replace_vars'),
    'file' => 'replace_vars.admin.inc',
    );

    $items['admin/config/user-interface/replace_vars/edit'] = array(
    'title' => 'Edit vars',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('replace_vars_form'),
    'access callback' => 'user_access',
    'access arguments' => array('administer replace_vars'),
    'file' => 'replace_vars.admin.inc',
    );

    return $items;
}

// registering our the module as filter
function replace_vars_filter_info() {
    $filters['replace_vars'] = array(
        'title' => t('Replace variables'),
        //'description' => t('Replace variables'),
        'process callback' => '_replace_vars',
        //'cache' => FALSE,
        //'settings callback' => '_replace_variables_settings',
    );
    return $filters;
}

function _replace_vars($str) {

    if(empty($str)) return $str;

    // get variables list
    $data = _replace_vars_get_vars();

    foreach($data as $v) {
        $value = unserialize($v->value);
        // we use strings and expect the same (ignore other types of vars)
        if(is_string($value) or is_numeric($value)) {
            $search[] = '|{'._replace_vars_strip_prefix($v->name).'}|';
            $replace[] = $value;
        }
    }

    return preg_replace($search, $replace, $str);
}

// get the list of variables from the database
function _replace_vars_get_vars() {
    // get all the custom variables set up with this module (based on the module variable prefix)
    $result = db_query('SELECT * FROM {variable} WHERE name LIKE :name', array(':name' => REPLACE_VARS_PREFIX.'%'));
    $data = $result->fetchAll();
    return $data;
}

// strip the prefix of the variable
function _replace_vars_strip_prefix($v) {
    $v = preg_replace('|^'.REPLACE_VARS_PREFIX.'|', '', $v);
    return $v;
}

replace_vars.admin.inc

<?php
// $Id: replace_vars.admin.inc,v 0.0.0.1 2012/09/26 $

/**
 * Lists all current custom variables and provides a link to the edit page.
 */
function replace_vars_page() {

    // get variables list
    $data = _replace_vars_get_vars();

    $header = array('', t('Key'), t('Value'), '');
    $rows = array();

    $i=1;
    foreach($data as $v) {
        $value = unserialize($v->value);
        // we use strings and expect the same (ignore other types of vars)
        if(is_string($value) or is_numeric($value)) {
            $key = _replace_vars_strip_prefix($v->name);
            $url_edit = l(t('edit'), 'admin/config/user-interface/replace_vars/edit/' . $key);
            $rows[] = array($i, $key, $value, $url_edit);
            $i++;
        }
    }
    $output = '<div style="text-align:right;">'.l(t('Add a new configuration key'), 'admin/config/user-interface/replace_vars/add').'</div>';
    $output .= theme('table', array('header' => $header, 'rows' => $rows));
    return $output;
}

/**
 * Displays an edit form for a custom variable record.
 */
function replace_vars_form($form, &$form_state) {

    $form['back_to_list'] = array(
            '#type' => 'item',
            '#markup' => '<div style="text-align:right;">'.l(t('Back to the list of variables'), 'admin/config/user-interface/replace_vars').'</div>',
        );

    $key = arg(5);
    if (!empty($key)) {
        $form['key'] = array(
            '#type' => 'hidden',
            '#value' => $key,
        );
        $form['display_name'] = array(
            '#type' => 'item',
            //'#title' => '',
            '#markup' => '<span style="font-weight:bold;">Change the value of the variable: <span style="font-weight:bold;color:red;">'.$key.'</span>',
        );
    }
    else {
        $form['key'] = array(
            '#type' => 'textfield',
            '#title' => t('Key'),
            '#required' => TRUE,
            '#description' => t('The name of the variable'),
            //'#default_value' => $key,
            '#default_value' => '',
        );
    }

    $form['value'] = array(
        '#type' => 'textarea',
        '#title' => t('Value'),
        '#required' => FALSE,
        //'#description' => t('The value of the variable'),
        '#default_value' => variable_get(REPLACE_VARS_PREFIX.$key, ''),
    );

    $form['buttons']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    // show delete button only on edit
    if ($key) {
        $form['buttons']['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Delete'),
        '#submit' => array('replace_vars_form_delete'),
        );
    }

    return $form;
}

function replace_vars_form_submit($form, &$form_state) {
    $key = $form_state['values']['key'];
    $value = $form_state['values']['value'];
    // when adding a variable -> add prefix to key
    $key = REPLACE_VARS_PREFIX.$key;
    variable_set($key, $value);
    $form_state['redirect'] = 'admin/config/user-interface/replace_vars';
    drupal_set_message(t('The configuration options have been saved.'));
}

function replace_vars_form_delete($form, &$form_state) {
    $key = REPLACE_VARS_PREFIX.$form_state['values']['key'];
    db_delete('variable')->condition('name', $key)->execute();
    $form_state['redirect'] = 'admin/config/user-interface/replace_vars';
    drupal_set_message(t('The configuration option has been deleted.'));
}