在 Drupal 中将用户限制为给定内容类型的单个节点的最佳方法

发布于 2024-08-30 22:58:04 字数 306 浏览 7 评论 0原文

我需要将用户限制为给定内容类型的单个节点。所以一个用户只能创建一个TypeX节点。我想出了两种方法。使用哪个会更好...

1)编辑node/add/typex菜单项,检查数据库,查看用户是否已经创建了TypeX的节点,以及是否有权限创建。

2)当用户创建TypeX节点时,将其分配给没有创建该类型节点权限的不同角色。

在方法 1 中,我必须在每个页面加载时进行额外的数据库调用,以查看它们是否应该能够看到“Create TypeX”(node/add/typex)。但在方法 2 中,我必须保持两个独立的角色。

您会使用哪种方法?

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 TypeX" (node/add/typex). But in approach 2, I have to maintain two separate roles.

Which approach would you use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2024-09-06 22:58:04

http://drupal.org/project/node_limit

更新:这更好,一周前更新,第一个一年内未更新

http://drupal.org/project/node_limitnumber

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

独夜无伴 2024-09-06 22:58:04

如果您愿意,可以研究 OnlyOne 模块(沙箱)的代码,看看一个简单的方法来实现这一点。

“唯一一个”模块允许为每种语言创建“唯一一个”节点
在此配置的选定内容类型中。

/**
 * 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');
      }
    }
  }
}

披露:我是模块的维护者OnlyOne

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文