Restricting book creation to certain content types (code proposal)

sahlborn - July 30, 2009 - 17:05
Project:BookMadeSimple
Version:6.x-2.1
Component:User interface
Category:feature request
Priority:normal
Assigned:MarcElbichon
Status:closed
Description

Hello,

I'm wondering if it may be possible to introduce a feature to restrict the creation of books to certain content types - i.e. you will only be able to create books with these content types as base pages.
I was playing around with some code. In my opinion the below code will suffice for that task (no real patch, yet a 'quasi patch' :-) ):

function book_made_simple_form_alter(&$form, $form_state, $form_id) {
+  global $user;
   if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
     $node = $form['#node'];
+    $nid = isset($node->nid) ? $node->nid : 'new';
+    if (array_key_exists($form['type']['#value'], variable_get('book_made_simple_forbid_main_page_creation', array())) && $user->uid != 1) {
+      unset($form['book']['bid']['#options'][$nid]);
+      // The following two lines are to prevent that book_manager module sets the unset item again
+      $form['#node']->nid = '0';
+      $form['#node']->book['original_bid'] = '0';
+    }
...
   switch ($form["#id"]) {
     case "node-type-form" : // $type . '_node_settings':
...
         '#description' => t('Checked will create a new book main page when adding.'),);
+      $varTypes = variable_get('book_made_simple_forbid_main_page_creation', array());
+      $default = (array_key_exists($type, $varTypes) && $varTypes[$type] != "0");
+      $form['book_made_simple']['3']['book_made_simple_forbid_main_page_creation'] = array(
+        '#attributes' => array('style' =>'float:none;clear:both'),
+        '#type' => 'checkbox',
+        '#title' => t('Forbid creation of book main page'),
+        '#default_value' => $default,
+        '#description' => t('Checked will prevent books from being manually created with this content type as main page type.'),);
...

function book_made_simple_form_submit($form, &$form_state) {
...
   variable_set('book_made_simple_auto_main_page', $types);
+
+  $text = $values["book_made_simple_forbid_main_page_creation"];
+  $type = $values["type"];
+  variable_del("book_made_simple_forbid_main_page_creation_" . $type);
+  $types = variable_get('book_made_simple_forbid_main_page_creation', array());
+  if ($text != "0") {$types[$type]= $type;}
+  else {unset($types[$type]);}
+  variable_set('book_made_simple_forbid_main_page_creation', $types);

Btw: This code will work in conjunction with book_manager module too.

#1

MarcElbichon - July 31, 2009 - 08:48
Assigned to:Anonymous» MarcElbichon
Status:needs review» needs work

If I understand your request, you want to hide core book outline section for content-type not allowed as books ?
Can't you disable Administer book outlines user permission ?

if not, BMS 2.2d (released soon - available Here) has a user permission to hide core book outline tab and fieldset. This should be a workaround.

Do you do difference between content-type that will be auto created as books by BMS, and content-type allowed as books ?
If not, I could restrict outline links only to selected content-type that can be auto created as book.

#2

sahlborn - August 2, 2009 - 20:11

The problem comes from my usage of book_manager module together with BMS. There I have to set user permissions to 'create personal books' to auto create a book, otherwise the book will not being created automatically. The user permission 'create new books' is not set here. But then a user can create (personal) books from all content types and I just don't want books to be created other than the automatically created ones...

#3

sahlborn - August 5, 2009 - 16:19

Correction of the above code (otherwise nodes could not be deleted properly):
Change lines 9 and 10 to

$form['#node']->nid = isset($form['#node']->nid) ? $form['#node']->nid : '0';
$form['#node']->book['original_bid'] = $form['#node']->nid;

So, as a whole this will be

function book_made_simple_form_alter(&$form, $form_state, $form_id) {
+  global $user;
   if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
     $node = $form['#node'];
+    $nid = isset($node->nid) ? $node->nid : 'new';
+    if (array_key_exists($form['type']['#value'], variable_get('book_made_simple_forbid_main_page_creation', array())) && $user->uid != 1) {
+      unset($form['book']['bid']['#options'][$nid]);
+      // The following two lines are to prevent that book_manager module sets the unset item again
+      $form['#node']->nid = isset($form['#node']->nid) ? $form['#node']->nid : '0';
+      $form['#node']->book['original_bid'] = $form['#node']->nid;
+    }
...
   switch ($form["#id"]) {
     case "node-type-form" : // $type . '_node_settings':
...
         '#description' => t('Checked will create a new book main page when adding.'),);
+      $varTypes = variable_get('book_made_simple_forbid_main_page_creation', array());
+      $default = (array_key_exists($type, $varTypes) && $varTypes[$type] != "0");
+      $form['book_made_simple']['3']['book_made_simple_forbid_main_page_creation'] = array(
+        '#attributes' => array('style' =>'float:none;clear:both'),
+        '#type' => 'checkbox',
+        '#title' => t('Forbid creation of book main page'),
+        '#default_value' => $default,
+        '#description' => t('Checked will prevent books from being manually created with this content type as main page type.'),);
...

function book_made_simple_form_submit($form, &$form_state) {
...
   variable_set('book_made_simple_auto_main_page', $types);
+
+  $text = $values["book_made_simple_forbid_main_page_creation"];
+  $type = $values["type"];
+  variable_del("book_made_simple_forbid_main_page_creation_" . $type);
+  $types = variable_get('book_made_simple_forbid_main_page_creation', array());
+  if ($text != "0") {$types[$type]= $type;}
+  else {unset($types[$type]);}
+  variable_set('book_made_simple_forbid_main_page_creation', $types);

#4

MarcElbichon - August 27, 2009 - 06:46

Will be added to next release.
Included in the next dev version

#5

MarcElbichon - August 27, 2009 - 07:09
Status:needs work» closed
 
 

Drupal is a registered trademark of Dries Buytaert.