I'm writing a little module that does some access manipulations on nodes depending on the targets of nodereference fields on them. I need to store some settings against each of the nodereference fields in a site. Being new to module development I can't see the obvious pitfalls of pursuing each of three possible approaches and I need some help. The three approaches are:

  1. Use variable_set() and variable_get() to set and retrieve values
  2. Use hook_form_alter() to add settings controls to the nodereference field settings form and then somehow save my module's settings with the existing CCK settings
  3. Create my own database table and put the settings there

Can anyone shed any light on which of these approaches is going to be the shortest path to the goal or the most "Drupal like"?

Comments

johnpitcairn’s picture

I think if your settings are per nodereference field settings (rather than per nodereference field in each node), variables would be an easy way out and acceptable enough. The pathauto module appears to store a lot in the variables table.

But if there will wind up being >20 or so variables, I'd probably go with a table anyway, so your hook_install() doesn't need to track and remove all your variables if your module is uninstalled. This also keeps your data out of the global $conf array.

You'll probably still want to hook_form_alter() the nodereference field settings form to add your UI, but to make CCK store your settings for you I think your module would need to be supplying the entire CCK field, and that sounds like way too much work.

jaypan’s picture

variable_set() and variable_get() are used for site-wide settings. Settings that apply to all users across the whole site. If you have user specific settings, or node specific settings, you will want to store them somewhere else in the database, not in the variables table.

Contact me to contract me for D7 -> D10/11 migrations.

yellek’s picture

Option 2.

There is this great CCK hook called hook_field_settings_alter() that allows modules to add settings widgets to cck fields and save the values using CCK. I have appended the relevant code below:

<?php
/**
 * Implementation of hook_field_settings_alter().
 */
function nodereference_create_access_field_settings_alter(&$form, $op, $field) {

  if ($field['type'] == 'nodereference') {
    switch ($op){
      case 'form':
        $form['nodereference_restrict_create'] = array(
          '#type' => 'checkbox',
          '#title' => t('Only allow references to nodes with edit access'),
          '#default_value' => isset($field['nodereference_restrict_create']) ? (bool) $field['nodereference_restrict_create'] : FALSE,
          '#description' => t('Restricts access depending upon the edit permissions of the node being linked to.'),
        );
        break;
      case 'save':
        // Add our new options to the list of settings to be saved.
        $form = array_merge($form, array('nodereference_restrict_create'));
        break;
    }
  }
}
?>
johnpitcairn’s picture

Thanks, I wasn't aware of that CCK alter hook. And I think I have a use for it...

adm925925’s picture

I'm new to PHP and Drupal and have some problems with variables. This is the issue I'm having:

Need:
I need to store the value of a variable that gets set once a user clicks a link. I need to maintain that value until the user clicks another link that will assigned a different value to that variable. Between these clicks, the user can navigate the site.

Problem:
I can assign that value, currently doing it through the browser request, and storing this value in $Globals, but as soon as I opened a form and submit it, the value is gone. Can anybody explain why this is happening?? How can this be done??
I cannot use variable_set/get because this value has to have session scope and variables from variable_set/get are site-wide.

I have been struggling with this for close to 2 weeks now.... Any help would be appreciated.

Please forgive me if I'm posting this question in the wrong place... this is my first post.

jaypan’s picture

Is the setting site wide, or user based?

Contact me to contract me for D7 -> D10/11 migrations.

adm925925’s picture

Thanks for your prompt response.
It should not be user, but session based. The same user might have another browser window where this variable holds a different value.

jaypan’s picture

In that case, use $_SESSION.

Contact me to contract me for D7 -> D10/11 migrations.

johnpitcairn’s picture

The $GLOBALS array is built from the variables table and other sources, but it's only held in memory - at the end of the page request, any changes you have made directly to that array will not be saved to the database. You'd need to use variable_set() to do that.

I guess you can't just prepend the user id to your variable name, because you need to track anonymous users (who are all user 0), right?

So either:

A - use the session array directly: $_SESSION['my_variable']. Drupal uses the $_SESSION array to pass messages around between page-views anyway. Make sure you use a name unique to your function/theme/module.

B - set and retrieve a cookie. But $_SESSION will be doing this anyway.

C - add the session id to your variable name: variable_set('my_variable_' . session_id(), $value). This would be considered bad because you're abusing the variables table, which is intended for site-wide config, not session data, and if you don't explicitly unset your variables they'll stay in the table forever. If you're doing this sort of thing you should really:

D - build a custom module, install your own database table, with rows keyed to session_id().

adm925925’s picture

I thought something along those lines was happening because, not matter how I tried to save the value in $GLOBALS, it would be deleted after submission.
I'm very excited about the "A" approach you suggested. I think this one should make it. I let you know after testing it.
Thank you very, very much for your help.

adm925925’s picture

IT WORKED !!!!!!! You got right. Thank you very much.