2010-05-03 23 views
2

我需要將用戶限制爲給定內容類型的單個節點。所以用戶只能創建TypeX的一個節點。我提出了兩種方法。哪個更好用...在Drupal中將用戶限制爲給定內容類型的單個節點的最佳方法

1)編輯node/add/typex菜單項來檢查數據庫,看看用戶是否已經創建了TypeX的節點,以及他們是否有權創建它。

2)當用戶創建TypeX節點時,將它們分配給不具有創建該類型節點權限的其他角色。

在方法1中,我必須對每個頁面加載進行額外的數據庫調用,以查看它們是否應該能夠看到「Create TypeX」(node/add/typex)。但在方法2中,我必須保持兩個不同的角色。

您會使用哪種方法?

回答

4

http://drupal.org/project/node_limit

UPDATE:這是更好的,更新的一週前,第一個未更新一年

http://drupal.org/project/node_limitnumber

+0

我梅南方法1)可能會更好,如果你有千兆網站需要核心優化 – henrijs 2010-05-03 10:39:12

+0

哇,我以爲我已經仔細研究了contrib模塊...不要猜測。感謝您的鏈接。我會接受答案,如果一個人爲我工作 – Chaulky 2010-05-03 13:45:23

+0

結束了一個開發人員已經得到了角色切換方法的工作,所以我們不會混亂它。但我很欣賞這些鏈接,未來可能會找到其中一個模塊 – Chaulky 2010-05-04 15:31:31

0

如果你願意,你可以研究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的維護者。

相關問題