I have an existing site where some users have "Personal contact form" selected under user Contact settings, and some have accepted the default (no personal contact form).

We have a requirement to now set all existing users to yes ("Personal contact form" selected).

From now on the default for new users will be "yes". That will be implemented using Robert Castelo's contact_default module. Ref: http://drupal.org/node/66648#comment-130975

I see that the setting for the personal contact form is recorded in the serialized 'data' field of the user table.

How can I step through the data field for each existing user, setting the personal contact form to 'yes'?

Comments

coreyp_1’s picture

Here's one way:

$resource = db_query("SELECT uid FROM {users}");

while ($uid = db_fetch_array($resource)) {
  if ($uid['uid']) {
    $user = user_load(array("uid" => $uid['uid']));
    $user->contact = 1;
    user_save($user);
  }
}

All the standard warnings apply: this is non-tested code. Use at your own risk. Back up your database first.

An easy way to do this is to click on "create content" -> "page", enter the text, enable the php filter, and click preview. The act of previewing the page will run the code.

Let us know if it worked!

- Corey

JDSaward’s picture

It failed the first time.

After looking at the API reference for user_save (4.7), I altered your code slightly:

$resource = db_query("SELECT uid FROM {users}");

while ($uid = db_fetch_array($resource)) {
  if ($uid['uid']) {
    $user = user_load(array("uid" => $uid['uid']));
    //$user->contact = 1;
    user_save($user,array('contact' => '1'));
  }
}

That worked!

Thanks heaps!

gurukripa’s picture

Cld u help with similar code for Drupal 5.1 for doing the following
1. enable contact form for all
2. enable guestbook for all

thanks a lot

lejon’s picture

anyone know how to do this? I need to force all users to have personal contact forms - would be good to use Userplus to be able to do this with a checkbox...!

ajayg’s picture

Same code for 4.7 above (by JDSaward ) works fine for drupal 5.7. I just tried to enable contact form for all existing users. As always backup your database first.

I tried the above code on my test site and it workd perfectly fine.

I had a small hiccup on my production site. Was getting white screen of death. So I made a slight modification to above script

<?php
$resource = db_query("SELECT uid FROM {users} LIMIT 0, 2000");

while ($uid = db_fetch_array($resource)) {
  if ($uid['uid']) {
    $user = user_load(array("uid" => $uid['uid']));
    //$user->contact = 1;
    user_save($user,array('contact' => '1'));
  }
}
?>

I have thousands of users so it was choking somewhere. SO I did 2000 users at a time (Limit 2001, 4000 etc) and then it worked very smoothly. You don't need to save the page, can just keep previewing by chaning the LIMIT to process more users.

hunt3r’s picture

You can drop this in your drupal root and run it via CLI script. This was tested and worked on Drupal 6.16.

<?php

// Bootstrap Drupal
require 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$resource = db_query("SELECT uid FROM {users} LIMIT 0, 2000");

while ($uid = db_fetch_array($resource)) {
  if ($uid['uid']) {
    $user = user_load(array("uid" => $uid['uid']));
    user_save($user,array('contact' => '1'));
    echo "finished $user->name\n";
  }
}
?>

save it as update-contact.php or whatever you want, then just run it like "php update-contact.php".

This will bootstrap the Drupal api and bi-pass the need for a module.

Chris

ajayg’s picture

You don't need a module for any of the above code. The code above just can be added by create content, add to the form and then choosing php input filter. So it is simpler than even the need for a script.

-Mania-’s picture

I had to modify this for Drupal 7, this worked for me:

// Bootstrap Drupal
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$resource = db_query("SELECT uid FROM {users} LIMIT 0, 2000")->fetchAll();

foreach ($resource as $uid) {
  if ($uid->uid) {
    $user = user_load(array("uid" => $uid->uid));
    user_save($user,array('contact' => '1'));
    echo "finished $user->name\n";
  }
}

I ran this inside a normal page with php filter.

jonmancheung’s picture

Works for me in Drupal 7

thanks Mania

thermalmusic’s picture

I also need to set all users' personal contact on as well and tried running this in a page. It seems to work, but I'm getting these warnings for each record processed:

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 173 of /var/www/coleserver.org/htdocs/jen7/includes/entity.inc).

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 350 of /var/www/coleserver.org/htdocs/jen7/includes/entity.inc).

Any ideas on how to get around this?

Thanks

wmfinnegan’s picture

Running into the same error, I discovered the user_load function is looking for a $uid variable rather than an array:
https://api.drupal.org/api/drupal/modules!user!user.module/function/user...

scotwith1t’s picture

i needed to do the opposite (i.e. set all contact forms to no...thousands of records were imported with the default setting on "y"!!) and this worked perfectly. just had to expand my LIMIT and change 1 (yes) to 0 (no). thanks!

COBadger’s picture

For a Drupal 7 site I created a custom module with the following code:

<?php 

function mymodule_menu() {
  $items['[put_some_random_alphanumeric_characters_here]'] = array(
    'page callback' => 'mymodule_enable_contact_form',
    'access arguments' => array('access content'),
  );
  return $items;
}

function mymodule_enable_contact_form() {
	drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

	$resource = db_query("SELECT uid FROM {users} LIMIT 0, 2000")->fetchAll();
	foreach ($resource as $uid) {
	  if ($uid->uid) {d
	    $account = user_load($uid->uid);
	    user_save($account,array('contact' => '1'));
	    echo "finished $account->name\n";
	  }
	}
}
?>

I enabled the module, navigated to the url specified in mymodule_menu() function, then disabled the module. (Actually removed this code altogether from the module.)

candelas’s picture

Thanks @COBadger
I was getting Warning: array_flip() and your code solved it :)

//trying to answer one question for each one that i make.
//this way, drupal will be more friendly and strong