Im working in the book "Pro Drupal Development".

I have now write down Chapter 1 and everyting is working, EXCEPT the .install file. (Or, to say it differently, the database isn't autofill with the annotation table)

To go for sure, I have copy the whole code from the code examples of http://www.drupalbook.com/files/7559ch02%20-%20Your%20First%20Module.zip. And put that over my code. That did not work..

Look, I can run a MySQL query, but I dont want that, I want that it works. I spent a lot of hours and cant find it, and I am sure that you can do this better :-)

Do you have some suggestions, or fixes?

Comments

Anonymous’s picture

You can find out if your mysql query is properly formed by running it in the query window of phpmyadmin, or something like that. Then you'll know if it's the query or your code in the install file.

Cornell Jake’s picture

When I run the query manually in the SQL-window, then it works perfect, and it also stores the data correctly.

But my problem is, that 'Drupal' dont see the .install file..
Thus also you are correctly that you think its the .install file.

Okay here he is:

Annotate.info

; $Id$ 
name = Annotate 
description = Allows users to annotate nodes. 
package = Example 
version = "$Name$"

Annotate.install

<?php 
// $Id$ 

function annotate_install() { 
  drupal_set_message(t('Beginning installation of annotate module.')); 
  switch ($GLOBALS['db_type']) { 
    case 'mysql': 
    case 'mysqli': 
      db_query("CREATE TABLE annotations ( 
        uid int NOT NULL default 0, 
        nid int NOT NULL default 0, 
        note longtext NOT NULL, 
        timestamp int(11) NOT NULL default 0, 
        PRIMARY KEY  (uid, nid) 
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;" 
      ); 
      $success = TRUE; 
      break; 
    case 'pgsql': 
      db_query("CREATE TABLE annotations ( 
        uid int NOT NULL DEFAULT 0, 
        nid int NOT NULL DEFAULT 0, 
        note text NOT NULL, 
        timestamp int NOT NULL DEFAULT 0, 
        PRIMARY KEY  (uid, nid) 
        );" 
      ); 
      $success = TRUE; 
      break; 
    default: 
      drupal_set_message(t('Unsupported database.')); 
  }
   
  if ($success) { 
    drupal_set_message(t('The module installed tables successfully.')); 
  } 
  else { 
    drupal_set_message(t('The installation of the annotate module was unsuccessful.'),'error'); 
  } 
}

Annotate.module

<?php 
// $Id$ 
/** 
 * @file 
 * Lets users add private annotations to nodes. 
 * 
 * Adds a text field when a node is displayed 
 * so that authenticated users may make notes. 
 */ 

/** 
 * Implementation of hook_menu(). 
 */ 
function annotate_menu($may_cache) { 
  $items = array(); 
  if ($may_cache) { 
    $items[] = array( 
      'path' => 'admin/settings/annotate', 
      'title' => t('Annotation settings'), 
      'description' => t('Change how annotations behave.'), 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array('annotate_admin_settings'), 
      'access' => user_access('administer site configuration') 
    ); 
  } 
  return $items; 
} 

/** 
 * Define the settings form. 
 */ 
function annotate_admin_settings() { 
  $form['annotate_nodetypes'] = array( 
    '#type' => 'checkboxes', 
    '#title' => t('Users may annotate these node types'), 
    '#options' => node_get_types('names'), 
    '#default_value' => variable_get('annotate_nodetypes', array('story')), 
    '#description' => t('A text field will be available on these node types to make user-specific notes.'), 
  ); 
  $form['array_filter'] = array('#type' => 'hidden'); 
  return system_settings_form($form); 
} 

/** 
 * Implementation of hook_nodeapi(). 
 */ 
function annotate_nodeapi(&$node, $op, $teaser, $page) { 
  switch ($op) { 
    case 'view': 
      global $user; 
      // If only the node summary is being displayed, or if the 
      // user is an anonymous user (not logged in), abort.
      if ($teaser || $user->uid == 0) { 
        break; 
      } 
      $types_to_annotate = variable_get('annotate_nodetypes', array('story')); 
      if (!in_array($node->type, $types_to_annotate)) { 
        break; 
      } 
      
      // Get previously saved note, if any. 
      $result = db_query("SELECT note FROM {annotations} WHERE uid = %d AND nid = %d", $user->uid, $node->nid); 
      $node->annotation = db_result($result); 
      
      // Add our form as a content item.
      $node->content['annotation_form'] = array( 
        '#value' => drupal_get_form('annotate_entry_form', $node), 
        '#weight' => 10 
      ); 
  } 
} 

/** 
 * Define the form for entering an annotation. 
 */ 
function annotate_entry_form($node) { 
  $form['annotate'] = array( 
    '#type' => 'fieldset', 
    '#title' => t('Annotations') 
  ); 
  $form['annotate']['nid'] = array( 
    '#type' => 'value', 
    '#value' => $node->nid 
  ); 
  $form['annotate']['note'] = array( 
    '#type' => 'textarea', 
    '#title' => t('Notes'), 
    '#default_value' => $node->annotation, 
    '#description' => t('Make your personal annotations about this content here. Only you (and the site administrator) will be able to see them.') 
  ); 
  $form['annotate']['submit'] = array( 
    '#type' => 'submit', 
    '#value' => t('Update') 
  ); 
  return $form; 
} 

/* 
 * Save the annotation to the database. 
 */ 
function annotate_entry_form_submit($form_id, $form_values) { 
  global $user; 
  $nid = $form_values['nid']; 
  $note = $form_values['note']; 
  db_query("DELETE FROM {annotations} WHERE uid = %d and nid = %d", $user->uid, $nid); 
  db_query("INSERT INTO {annotations} (uid, nid, note, timestamp) VALUES (%d, %d, '%s', %d)", $user->uid, $nid, $note, time()); 
  drupal_set_message(t('Your annotation was saved.')); 
}

I know its a long code, but please help me out. I cant find the prob you know, and I think its all good.

Thanks before guys!

Anonymous’s picture

Are you still having problems with the install file?

Does your drupal database user have mysql permissions to create new tables in the drupal database?

Here's the working code from my annotate install file:

<?php 
// $Id$ 

function annotate_install() { 
  drupal_set_message(t('Beginning installation of annotate module.')); 
  switch ($GLOBALS['db_type']) { 
    case 'mysql': 
    case 'mysqli': 
      db_query("CREATE TABLE annotations ( 
        uid int NOT NULL default 0, 
        nid int NOT NULL default 0, 
        note longtext NOT NULL, 
        timestamp int(11) NOT NULL default 0, 
        PRIMARY KEY  (uid, nid) 
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;" 
      ); 
      $success = TRUE; 
      break; 
    case 'pgsql': 
      db_query("CREATE TABLE annotations ( 
        uid int NOT NULL DEFAULT 0, 
        nid int NOT NULL DEFAULT 0, 
        note text NOT NULL, 
        timestamp int NOT NULL DEFAULT 0, 
        PRIMARY KEY  (uid, nid) 
        );" 
      ); 
      $success = TRUE; 
      break; 
    default: 
      drupal_set_message(t('Unsupported database.')); 
  }
   
  if ($success) { 
    drupal_set_message(t('The module installed tables successfully.')); 
  } 
  else { 
    drupal_set_message(t('The installation of the annotate module was unsuccessful.'),'error'); 
  } 
} 
Cornell Jake’s picture

Thanks for your help duggoff@drupal.org, but your code does still not work. Also I have checked my MySQL username and pass, and that is fine. (I am working on localhost with this testsite)

Do you have any suggestions?

cjokomay’s picture

All you need to do is disable the module and then delete its row from the system table of the database. After that is done enable the module again and all the tables will be created!

unclejustin’s picture

Bless your heart. This is what I had to do to fix the same problem for me. Thanks for helping out us noobs in our time of need!

PED-1’s picture

There's a number of posts on the Book's website with info about errors.
http://www.drupalbook.com/errata

Some reference this very piece of code - pointing out that the table name in the SQL query should be contained in curly brackets {}.

I've had precisely the same problem as you. I corrected the code and removed the row from the system table as mentioned by CmdrDork. (Thanks for that info!)
Upon the next enable of the Annotation module I got the proper confirmation messages telling me the database was set up. Those messages didn't appear before.

PE Design
http://www.pedesign.co.uk

lucasw’s picture

My module never had the curly bracket problem (I worked from a revised copy of the book) but the install still doesn't work - there are no success or failure messages from the drupal_set_message calls.

I don't have phpmyadmin- how do I manually delete a row corresponding to the module from the system table?

lucasw’s picture

I figured it out:

mysql -u root -p
use drupal
delete from system where filename = 'sites/all/modules/commentpoints/somemodule.module';