0

It's possible to pass custom variables into Views ? I'm trying to manipulate some elements from a Views by using the administration page.

This how I ask the custom variable ...

custom_module.module

/**
 * Implements hook_menu().
 */
function custom_module_menu() {
    $items = array();
    $items['admin/structure/custom_module'] = array(
        'title' => 'Custom Module Integration',
        'description' => t('Custom Module'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('custom_module_form'),
        'access arguments' => array('administer  custom module configuration'),
        'file' => 'custom_module_form.inc',
    );
    return $items;
}

custom_module_form.inc

/**
 * custom_module
 */
function custom_module_form($form, &$form_state) {

    $form['custom_variable'] = array(
        '#type' => 'textfield',
        '#title' => t('custom_module configuration page'),
        '#default_value' => variable_get('custom_variable'),
        '#description' => t('Enter my variable'),
    );


    return(system_settings_form($form));

}

The variable is store in my Drupal variable_get which I can confirm on variable editor. So my goal is to be able to used custom_variable on Rewrite Results or Global: Custom text on Views. Which I have try it but does not translate to the variable value.

What I'm doing wrong ? Or is not possible to used variable on Views ?

  • In your code i do not see where you set the custom_variable. using system_setting_form, you must make sure that the variable name and the array key for the form element is the same, ie: $form['custom_variable'] = array... – J. Reynolds Feb 11 '15 at 18:20
  • On my original code the custom_variable are the same. I forgot to replace that one. Even thought those are the same I still cant using on my Views. – itsdarrylnorris Feb 11 '15 at 18:24
  • Okay, I just pointed that out. @tunic has the right answer. You can also use php text format but it is no longer considered good practice. – J. Reynolds Feb 11 '15 at 18:25
1

You have to write a custom module that creates a suitable handler (for example, a Views area handler). In that handler you can read your custom variable and act accordingly.

Keep in mind Views cache. So, if you change that variable you should invalidate the cache object for that view or views that uses that handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.