1

I'm not entirely sure how to title this question. I want to restrict the creation of a particular content type to one entry per user per the value of a particular field. So essentially what I'm saying is you should be unable to create a second piece of content of a particular type if the value of a particular field is identical to the value of the same field but in another piece of content created by the same user.

Is there anyway to accomplish this?

2 Answers 2

2

I'm not aware of an existing module with that functionality, but it wouldn't be too hard to code your own custom module to do the job.

This is an example using the 'article' node type:

function MYMODULE_form_article_node_form_alter(&$form, &$form_state, $form_id) {
  if (empty($form['nid']['#value'])) {
    $form['#validate'][] = 'MYMODULE_article_node_form_validate';
  }
}

function MYMODULE_article_node_form_validate($form, &$form_state) {
  global $user;

  // See if this user has already created a piece of content of this type.
  $args = array(':uid' => $user->uid, ':type' => 'article');
  $count = db_query('SELECT COUNT(nid) FROM {node} WHERE uid = :uid AND type = :type', $args)->fetchField();

  // If they have, check for another node created by the user with the same field value
  if ($count > 0) {
    $query = new EntityFieldQuery;
    $query->entityCondition('entity_type')
      ->entityCondition('bundle', 'article')
      ->propertyCondition('uid', $user->uid)
      ->fieldCondition('field_some_field', 'value', $form_state['values']['field_some_field'][LANGUAGE_NONE][0]['value']);

    $results = $query->execute();

    // If we have a result, invalidate the form.
    if (!empty($results['node'])) {
      form_set_error('field_some_field', t('Error message here'));
    }
  }
}

That'll need a bit of massaging to match your exact situation, but it should be a good place to start.

5
  • 1
    I was figuring I'd be going the custom validation route. I'll have to add one to my already existing Custom Validations module :( Probably better I do it that way anyway. Still, I walk the line between homebrewing and using already created and support modules... I tend towards the latter because I hate having responsibility :) Oct 9, 2012 at 18:03
  • 1
    Amen to that :) I think this might be one of those times where it's good to have fine-grained control over what you're doing; you might decide in the future you want to change the logic slightly, and that might not have been possible if you were using a contrib module. But...it's Drupal, if you look hard enough someone will probably have written one that does everything you need ;)
    – Clive
    Oct 9, 2012 at 18:06
  • Oorah. Also quick question, does the global $user; line access the logged in user only? Or does it refer to the author of the loaded content item? Oct 9, 2012 at 18:09
  • Yeah it's the logged in user, I made mistake in the code it should be checking to make sure it's a node add form for that example. Sorry about that. You probably want to add a separate validate function for when it's not a node form if you need to do different validation for updates
    – Clive
    Oct 9, 2012 at 18:59
  • Finally got this implemented. Thanks again Clive. Oct 18, 2012 at 22:34
0

This is probably not ideal and the lazy and ugly way, but you could look at the unique field module

http://drupal.org/project/unique_field

I say it's ugly because the user will still be able to fill out all the fields and believe they can submit the content, but when it comes to validation after submitting it, then they'll get the error, but probably won't know why.

1
  • Unique Filed module displays quite handy error message if user tries to add content with duplicated values Oct 10, 2012 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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