开发者

Best approach to limit users to a single node of a given content type in Drupal

I need to limit users to a single node of a given content type. So a user can only create one node of TypeX. I've come up with two approaches. Which would be better to use...

1) Edit the node/add/typex menu item to check the database to see if the user has already created a node of TypeX, as well as if they have permissions to create it.

2) When a user creates a node of TypeX, assign them to a different role that doesn't have permissions to create that type of node.

In approach 1, I have to make an additional database call on every page load to see if they should be able to see the "Create Type开发者_StackOverflowX" (node/add/typex). But in approach 2, I have to maintain two separate roles.

Which approach would you use?


http://drupal.org/project/node_limit

UPDATE: this is even better, updated week ago, first one is not updated in a year

http://drupal.org/project/node_limitnumber


If you want you can study the code of the OnlyOne module (sandbox) to see a simple way to accomplish this.

The Only One module allows the creation of Only One node per language in the selected content types for this configuration.

/**
 * Implements hook_form_alter().
 * @param $form
 * @param $form_state
 * @param $form_id
 */
function onlyone_form_alter(&$form, &$form_state, $form_id) {
  $onlyone_content_types = variable_get('onlyone_node_types');
  //getting the name of the node type
  $node_type = substr($form_id, 0, -10);

  //Verifying if the new node should by onlyone
  if (isset($onlyone_content_types) && in_array($node_type, $onlyone_content_types, TRUE)) {
    $node = $form_state['node'];
    //if we are trying to create a new node
    if (!isset($node->nid) || isset($node->is_new)) {
      $query = new EntityFieldQuery();
      $query->entityCondition('entity_type', 'node')
        ->entityCondition('bundle', $node_type);

      if (drupal_multilingual()) {
        global $language;
        $query->propertyCondition('language', $language->language);
      }

      $result = $query->execute();
      //if we have one node, then redirect to the edit page
      if (isset($result['node'])) {
        $nid = array_keys($result['node'])[0];
        drupal_goto('node/' . $nid . '/edit');
      }
    }
  }
}

Disclosure: I'm the maintainer of the module OnlyOne.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜