0

I have a custom block module that has a configuration form of type text_field. If there is a value entered into the configuration form, I want to store that value in a variable.

Here is the configuration hook:

/**
 * Implements hook_block_configure().
 */
function roadmap_block_configure($delta = 'roadmap') {
  // This example comes from node.module.
  $form = array();

  switch($delta) {
    case 'roadmap' :
      $form['roadmap_configuration_form'] = array(
      '#type' => 'text_format',
      '#title' => t('Enter an alternative URL.'),
// the URL entered will be sanitized and attached to hook_block_view(), and ultimately passed to a custom JavaScript file.
    );
    break;
  }
  return $form;
}

If there is a value entered into the configuration form, how can I store it in a variable?

  • when you say configuration form, is this something for admin to set for the module as a global variable, or for general users to have as a user-specific variable? how will you use this variable? please elaborate – longboardnode Jan 10 '18 at 23:11
  • The configuration form is for a custom module I built that prints out a block. In Configure block, this form allows the editor to paste in an alternative link that should be passed to a variable (what I'm trying to do) which will then be attached to the block_view in a js setting and passed to a custom JS file. I've got that code, I just need to know how to store the configuration in a variable if the editor adds text to the form. – lane Jan 10 '18 at 23:12
  • so this if for your admin to configure how the module should behave? this is not for general users? – longboardnode Jan 10 '18 at 23:14
  • Yes, it's for the admin/site editor. – lane Jan 10 '18 at 23:18
1

So if this is for admin/editors only, one way to do it is to use the built-in Drupal hook_settings_form in your custom module. Here is a nice DO tutorial: https://www.drupal.org/docs/7/creating-custom-modules/creating-the-configuration-form

So in your case, this could look something like:

/**
 * Implements hook_settings_form() to configure module.
 */
function roadmap_settings_form($settings) {

  $form = array();
  $form['roadmap_url'] = array(
      '#type' => 'text_format',
      '#title' => t('Enter an alternative URL.'),
  );
  return system_settings_form($form);
}

this form will save the value as a system variable 'roadmap_url' or whatever value you enter in the form field's machine name. you'd probably need to call this from some admin page in your custom module like so:

/**
 * Implements hook_menu().
 */
function roadmap_menu() {
  return array(
    'admin/config/roadmap_OR_SOME_OTHER_PATH' => array(
      'title' => t('Roadmap settings'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('roadmap_settings_form'),
      'access arguments' => array('administer roadmap OR SOME OTHER PERMISSION'),
      'type' => MENU_LOCAL_TASK,
    ),
  );
}

Now you should be able to go to your config page, enter your URL setting and it will be saved automatically by Drupal. To access this variable, you just need to call:

$url = variable_get('roadmap_url');
|improve this answer|||||
  • Thanks, mate!! I'll give this a go today and report back. – lane Jan 11 '18 at 12:04

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.