I have attached a patch that adds the option to force users to create a bio before continuing through the site. It only applies to uid > 1, and drops a 303 (see also) redirect to node/add/bio (or whatever content type is chosen).

P.S. Sorry if the patch is a little janky. I've never really patched anything for contrib.

Comments

marcp’s picture

Status: Needs review » Needs work

In bio_init() you're duplicating a good chunk of what the function bio_for_user() does. You can just call that and see if it returns FALSE to check if the user hasn't yet created their bio.

This is a great idea, by the way.

jscheel’s picture

Status: Needs work » Needs review
StatusFileSize
new1.97 KB

Good catch! I need to look through the code a little better, eh? I've made the change. I've also wrapped the message string in t() for proper text handling.

marcp’s picture

Status: Needs review » Needs work

It looks like you're using $may_cache without having intialized it. It would also be great to run your code through the Coder module, which will help you get the code better "Drupalized."

I haven't thought too much about it, but is hook_init() the best place to do this?

dldege’s picture

This might be easier - its what I do in my own code at the moment and doesn't require any menu hooking. It simply keeps redirecting the user to user/N/bio until they have submitted it once (ie. bio node exists for the user). The only valid url until this point is q=logout.

function site_init() {
  global $user;
  if ($user->uid > 1 && strcmp('logout', $_GET['q'])) {
      // Make sure the user creates a bio
      if (strcmp('user/' . $user->uid . '/bio', $_GET['q'])) {
        site_verify_user_bio($user);
    }
  }
}

function site_verify_user_bio($user) {
  $bio = bio_for_user($user->uid);
  if (!$bio) {
    drupal_goto('user/' . $user->uid . '/bio');  
  }
}

robloach’s picture

Status: Needs work » Needs review
StatusFileSize
new1.41 KB

Here's a re-work using dldege's solution. It gives you an option in admin/user/bio to turn on Required, which will make the user have to create a Bio node before using the rest of the site.

robloach’s picture

Status: Needs review » Needs work

Now that I look at it, this is pretty much the same as jscheel's solution. I think it would be good to move it to hook_menu, using !$may_cache in there.

dldege’s picture

Yeah, they are very similiar and it looks like either version will work. hook_init happens a lot earlier in the page request lifecycle which might be the only advantage of doing it there - no need to deserialize and build the menus, etc. I just offered it as another approach - I do think its a feature worth adding though.

Thanks for adding the settings form handling.

robloach’s picture

Version: 5.x-1.0 » 5.x-1.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.98 KB

Re-factored with all the above provided solutions, and in hook_menu instead of hook_init. It also displays the Biography node type name instead of just "bio" when asking the user to create a bio node.

Rob_Feature’s picture

Status: Needs review » Reviewed & tested by the community

Installed and works perfectly as advertised. I'd suggest this is ready to commit...and it's a beautiful feature as well.

SamStealth’s picture

i tried out your latest patch and it works like a charm! great work!

SamStealth’s picture

it took me ages to get moofie's locationfields to work with 'enforce bio creation'.
somehow dependent dropdowns want to fetch data from 'locationfields/ajax/bio' ... and drupal_goto bio node broke it.
finally i found a one-line solution:

            if (strcmp($_GET['q'],'locationfields')<=0)
                drupal_goto('user/' . $user->uid . '/bio'); 

maybe it's good to place a 'do not redirect on following pages...' option on config screen.

SamStealth’s picture

FYI
i noticed, that - if clicking around on my ajax empowered form - more and more error messages are accumulated,
so here is a slight improvement to my previous post:

            if (!preg_match('/^locationfields/i', $_GET['q']))
            {
                drupal_set_message(t('Please create a %bio before continuing.', array('%bio' => $biotypename)), 'error');
                drupal_goto('user/' . $user->uid . '/bio'); 
            }
jcruz’s picture

subscribing

robloach’s picture

SamStealth: Might be better to have it do something similar to the wildcard solution in admin/content/block where you type in what pages are exceptions. In this case, these would be pages where users would be allowed to visit without requiring the Bio node.

jjeff’s picture

Re: Wildcard solution: Good idea!

I'm working up a new version of the module right now and I'll try to roll this in. However, if anyone else has any interest/time for this, please post here..

jscheel’s picture

Sorry, I've been kindof awol on this issue. I can write up something to integrate wildcards. Just let me know.

Caleb G2’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.71 KB

After applying this patch the following message was always returned when I went to actually submit the bio-node form:

"This user already has a bio. Edit it here or assign this entry to another user."

After a little tinkering around I figure out that just pulling bio_nodeapi() seemed to have no obvious adverse affects. The problem is gone, and I don't get the message anymore, so I guess it was left over cruft??

In any case here is a new patch with bio_nodeapi() taken out.

designwork’s picture

StatusFileSize
new7.44 KB

Hi all,

I did a version of the biomodule automatically created for each user, were you can rename the Tab via admin settings etc.
someone want to test it? More explainantions you will find here http://drupal.org/node/184868.

I think this could be nice to have in bio as standart. But you have to see the code first. It works only for 5.x

Dirk

Caleb G2’s picture

DesignWork, it's great that you're trying to get involved with Drupal and making contributions back to the project, but please realize that you are spamming an active, and unrelated, thread. I've seen your comments about this on another thread somewhere on the site, and I can't remember if anyone there suggested it or not (I believe someone did) -- you should file a new issue to engage the bio maintainers about the possibility of integrating your changes, and/or get your own cvs account and make a separate project. Those are generally the only ways new code gets notice or accepted.

Michael Hofmockel’s picture

In order to enforce Bio node creation I did the following:

Created a block that is only visible to authenticated user.

Block body =
<?php
if (bio_for_user($user->uid) == false) {
  $message = 'Please create a biography.';
  drupal_set_message($message, $type = 'error');
  drupal_goto('node/add/bio');
}
?>

Show the block only at:
node/add
node/add/book*
node/add/page*
...

Caution! Dont show on "node/add/bio or node/add/*" as that will cause an endless loop.

This way you can use the entire site but you can not create content until you have created a Bio node.
I feel we do need a module based solution but this is an easy and effective solution in the mean time.

amcc’s picture

I applied this patch: bio.module.required.patch from comment 8 and when i enabled the module i got this error:

warning: array_keys() [function.array-keys]: The first argument should be an array in /Users/macintosh/Sites/monkey/sites/all/modules/bio/bio_views.inc on line 39.

is this something i need to worry about? Thanks for all the hard work guys - and also to jjeff for a great module and amazing and entertaining podcast.

kufeiko’s picture

Someone having problems with Imagefield when this patch is applied?

I use bio 1.0 and the latest patch from this thread. My bio content type holds an image. If an user is forced to create bio, he/she is unable to upload a picture. Seems the upload itself passes, but after that the image is not in the needed directory.

I have the feeling that when you press 'Upload' button to insert image, the enforcement is redirecting you again, preventing the processing of the image.

If an image is added to already existing bio node, no problems are found.

robloach’s picture

vanko: Be sure to be using ImageField 5.x-2.x-dev. I was experiencing this, updated to ImageField 2.x, and the problem seemed to magically go away. Not quite sure what was happening here, but 2.x fixed it. It also comes with some nice AJAX/AHAH magic which will make you very happy.

robloach’s picture

Status: Needs review » Closed (duplicate)

I believe we'll be moving to this solution as opposed to the drupal_goto solution suggested in this issue. Much cleaner, and most definitely looks nicer.

Zoologico’s picture

This works well.

In order to enforce Bio node creation I did the following:

Created a block that is only visible to authenticated user.

Block body =

if (bio_for_user($user->uid) == false) {
  $message = 'Please create a biography.';
  drupal_set_message($message, $type = 'error');
  drupal_goto('node/add/bio');
}

Show the block only at:
node/add
node/add/book*
node/add/page*
...

Caution! Dont show on "node/add/bio or node/add/*" as that will cause an endless loop.

This way you can use the entire site but you can not create content until you have created a Bio node.

I feel we do need a module based solution but this is an easy and effective solution in the mean time.

Thanks, mhofmockel.

kufeiko’s picture

Thanks for both of answers!

Unfortunately, imagefield 2.x didn't do any good to me - the upload broke. I got the HTML of altered web page in an error message. I didn't investigate further.

Bio fields on creation sounds nice, very nice! But i got again this problem with images upload :-(. Furthermore, the users are able to delete their own module and after that stay without bio node. Sad.

Will continiue to look for another solution, if you have any ideas - are welcome ;-)

regards,
Vanko

amcc’s picture

I've used the solution below - it works perfectly. What i'm worried about is the overheads of the code. Whenever a user logs in i want to enforce them to create a bio before they move on if they don't have one already.

Is this going to slow the site down in any significant way for authenticated users as there will be an extra database query on every page (i've included the bio code showing that query below). Or is this so insignificant that i really shouldn't worry about it.

Here is the code that i've put in my block, I'm making the block appear on every page apart from node/add/bio and node/add/*. I'm also only showing the block for authenticated users.

$bio =  bio_for_user();
if ($bio == FALSE){
	$message = 'Please create your Profile.';
	drupal_set_message($message, $type = 'error');
	drupal_goto('node/add/bio');
}

In an effort to make the code as simple as possible i've taken the $user->uid bit out of the block code as that's already in the bio module function anyway...

// function called in bio.module
function bio_for_user($uid = NULL){
  if (is_null($uid)){
    global $user;
    $uid = $user->uid;
  }
  return db_result(db_query("SELECT nid FROM {node} WHERE uid = %d AND type = '%s'", $uid, variable_get('bio_nodetype', 'bio')));
}
liliplanet’s picture

subscribe