diff --git a/modules/comment/comment.install b/modules/comment/comment.install index 0213808..6f279e5 100644 --- a/modules/comment/comment.install +++ b/modules/comment/comment.install @@ -17,15 +17,15 @@ function comment_uninstall() { $node_types = array_keys(node_type_get_types()); foreach ($node_types as $node_type) { field_attach_delete_bundle('comment', 'comment_node_' . $node_type); - variable_del('comment_' . $node_type); - variable_del('comment_anonymous_' . $node_type); - variable_del('comment_controls_' . $node_type); - variable_del('comment_default_mode_' . $node_type); - variable_del('comment_default_order_' . $node_type); - variable_del('comment_default_per_page_' . $node_type); - variable_del('comment_form_location_' . $node_type); - variable_del('comment_preview_' . $node_type); - variable_del('comment_subject_field_' . $node_type); + variable_del('comment_node_' . $node_type); + variable_del('comment_anonymous_node_' . $node_type); + variable_del('comment_controls_node_' . $node_type); + variable_del('comment_default_mode_node_' . $node_type); + variable_del('comment_default_order_node_' . $node_type); + variable_del('comment_default_per_page_node_' . $node_type); + variable_del('comment_form_location_node_' . $node_type); + variable_del('comment_preview_node_' . $node_type); + variable_del('comment_subject_field_node_' . $node_type); } } @@ -195,10 +195,17 @@ function comment_schema() { ), ); - $schema['node_comment_statistics'] = array( + $schema['comment_entity_statistics'] = array( 'description' => 'Maintains statistics of node and comments posts to show "new" and "updated" flags.', 'fields' => array( - 'nid' => array( + 'entity_type' => array( + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + 'description' => 'The entity type', + ), + 'entity_id' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, @@ -259,3 +266,47 @@ function comment_schema() { return $schema; } + +/** + * Rename comment/node variable to comment/entity. + */ +function comment_update_8000() { + foreach (node_type_get_types() as $node_type) { + $old_keys[] = 'comment_'; + $old_keys[] = 'comment_default_mode_'; + $old_keys[] = 'comment_default_per_page_'; + $old_keys[] = 'comment_anonymous_'; + $old_keys[] = 'comment_subject_field_'; + $old_keys[] = 'comment_form_location_'; + $old_keys[] = 'comment_preview_'; + foreach ($old_keys as $old_key) { + $new_key = "{$old_key}_node_{$node_type->type}"; + $old_key = "{$old_key}_{$node_type->type}"; + $variable = variable_get($old_key); + variable_set($new_key, $variable); + variable_del($old_key); + } + } +} + +/** + * Update table {node_comment_statistics}. + */ +function comment_update_8001() { + # Rename {node_comment_statistics} to {comment_entity_statistics} + db_rename_table('node_comment_statistics', 'comment_entity_statistics'); + + # Add field entity_type + $comment_schema = comment_schema(); + $entity_column = $comment_schema['comment_entity_statistics']['fields']['entity_type']; + db_add_field('comment_entity_statistics', 'entity_type', $entity_column); + + # Set entity_type to 'node' + db_update('comment_entity_statistics') + ->fields(array('entity_type' => 'node')) + ->execute(); + + # Rename nid to entity_id + $entity_id_column = $comment_schema['comment_entity_statistics']['fields']['entity_id']; + db_change_field('comment_entity_statistics', 'nid', 'entity_id', $entity_id_column); +} diff --git a/modules/comment/comment.module b/modules/comment/comment.module index ae9278c..c8a1f66 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -609,6 +609,12 @@ function theme_comment_block() { } /** + * @TODO: Implements hook_entity_view(). + */ +function comment_entity_view($entity, $entity_type, $view_mode, $langcode) { +} + +/** * Implements hook_node_view(). */ function comment_node_view($node, $view_mode) { @@ -1133,7 +1139,7 @@ function comment_form_node_type_form_alter(&$form, $form_state) { $form['comment']['comment'] = array( '#type' => 'select', '#title' => t('Default comment setting for new content'), - '#default_value' => variable_get('comment_' . $form['#node_type']->type, COMMENT_NODE_OPEN), + '#default_value' => variable_get('comment_node_' . $form['#node_type']->type, COMMENT_NODE_OPEN), '#options' => array( COMMENT_NODE_OPEN => t('Open'), COMMENT_NODE_CLOSED => t('Closed'), @@ -1143,19 +1149,19 @@ function comment_form_node_type_form_alter(&$form, $form_state) { $form['comment']['comment_default_mode'] = array( '#type' => 'checkbox', '#title' => t('Threading'), - '#default_value' => variable_get('comment_default_mode_' . $form['#node_type']->type, COMMENT_MODE_THREADED), + '#default_value' => variable_get('comment_default_mode_node_' . $form['#node_type']->type, COMMENT_MODE_THREADED), '#description' => t('Show comment replies in a threaded list.'), ); $form['comment']['comment_default_per_page'] = array( '#type' => 'select', '#title' => t('Comments per page'), - '#default_value' => variable_get('comment_default_per_page_' . $form['#node_type']->type, 50), + '#default_value' => variable_get('comment_default_per_page_node_' . $form['#node_type']->type, 50), '#options' => _comment_per_page(), ); $form['comment']['comment_anonymous'] = array( '#type' => 'select', '#title' => t('Anonymous commenting'), - '#default_value' => variable_get('comment_anonymous_' . $form['#node_type']->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT), + '#default_value' => variable_get('comment_anonymous_node_' . $form['#node_type']->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT), '#options' => array( COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'), COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'), @@ -1166,17 +1172,17 @@ function comment_form_node_type_form_alter(&$form, $form_state) { $form['comment']['comment_subject_field'] = array( '#type' => 'checkbox', '#title' => t('Allow comment title'), - '#default_value' => variable_get('comment_subject_field_' . $form['#node_type']->type, 1), + '#default_value' => variable_get('comment_subject_field_node_' . $form['#node_type']->type, 1), ); $form['comment']['comment_form_location'] = array( '#type' => 'checkbox', '#title' => t('Show reply form on the same page as comments'), - '#default_value' => variable_get('comment_form_location_' . $form['#node_type']->type, COMMENT_FORM_BELOW), + '#default_value' => variable_get('comment_form_location_node_' . $form['#node_type']->type, COMMENT_FORM_BELOW), ); $form['comment']['comment_preview'] = array( '#type' => 'radios', '#title' => t('Preview comment'), - '#default_value' => variable_get('comment_preview_' . $form['#node_type']->type, DRUPAL_OPTIONAL), + '#default_value' => variable_get('comment_preview_node_' . $form['#node_type']->type, DRUPAL_OPTIONAL), '#options' => array( DRUPAL_DISABLED => t('Disabled'), DRUPAL_OPTIONAL => t('Optional'), @@ -1239,6 +1245,49 @@ function comment_form_node_form_alter(&$form, $form_state) { } /** + * Implements hook_entity_load(). + */ +function comment_entity_load($entities, $entity_type) { + $entity_info = entity_get_info($entity_type); + + // Entity does not support comment, we do nothing. + if (empty($entity_info['comment'])) { + return; + } + + # dsm($entity_info); return; + $id_field = $entity_info['entity keys']['id']; + + $comments_enabled = array(); + foreach ($entities as $entity) { + // Store whether comments are enabled for this node. + if ($entity->comment != COMMENT_NODE_HIDDEN) { + $comments_enabled[] = $entity->{$id_field}; + } + else { + $entity->cid = 0; + $entity->last_comment_timestamp = isset($entity->created) ? $entity->created : NULL; + $entity->last_comment_name = ''; + $entity->last_comment_uid = $entity->uid; + $entity->last_comment_uid = isset($entity->uid) ? $entity->uid : 0; + $entity->comment_count = 0; + } + } + + // For nodes with comments enabled, fetch information from the database. + if (!empty($comments_enabled)) { + $result = db_query('SELECT cid, entity_id, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count FROM {comment_entity_statistics} WHERE entity_type = :type AND entity_id IN (:comments_enabled)', array(':entity_type' => $entity_type, ':comments_enabled' => $comments_enabled)); + foreach ($result as $record) { + $entities[$record->entity_id]->cid = $record->cid; + $entities[$record->entity_id]->last_comment_timestamp = $record->last_comment_timestamp; + $entities[$record->entity_id]->last_comment_name = $record->last_comment_name; + $entities[$record->entity_id]->last_comment_uid = $record->last_comment_uid; + $entities[$record->entity_id]->comment_count = $record->comment_count; + } + } +} + +/** * Implements hook_node_load(). */ function comment_node_load($nodes, $types) { @@ -1262,7 +1311,10 @@ function comment_node_load($nodes, $types) { // For nodes with comments enabled, fetch information from the database. if (!empty($comments_enabled)) { - $result = db_query('SELECT nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count FROM {node_comment_statistics} WHERE nid IN (:comments_enabled)', array(':comments_enabled' => $comments_enabled)); + $result = db_query(' + SELECT cid, entity_id, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count + FROM {comment_entity_statistics} + WHERE entity_type = :type AND entity_id IN (:comments_enabled)', array(':entity_type' => '', ':comments_enabled' => $comments_enabled)); foreach ($result as $record) { $nodes[$record->nid]->cid = $record->cid; $nodes[$record->nid]->last_comment_timestamp = $record->last_comment_timestamp; @@ -2717,3 +2769,32 @@ function comment_file_download_access($field, $entity_type, $entity) { return FALSE; } } + +/** + * Implements hook_schema_alter(). + * Create comment field to entity base and revision tables. + */ +function comment_schema_alter(&$schema) { + foreach (entity_get_info() as $entity_type => $entity_info) { + if (!empty($entity_info['comment'])) { + foreach (array('base table', 'revision table') as $i) { + if (isset($entity_info[$i])) { + $table_name = $entity_info[$i]; + if (isset($schema[$table_name]) && !isset($schema[$table_name]['fields']['comment'])) { + $schema[$table_name]['fields']['comment'] = array( + 'description' => 'Whether comments are allowed on this node: 0 = no, 1 = closed (read only), 2 = open (read/write).', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ); + + try { + db_add_field($table_name, 'comment', $schema[$table_name]['fields']['comment']); + } + catch (Exception $e) {} + } + } + } + } + } +} diff --git a/modules/node/node.install b/modules/node/node.install index 3f732a4..48cf50a 100644 --- a/modules/node/node.install +++ b/modules/node/node.install @@ -70,12 +70,6 @@ function node_schema() { 'not null' => TRUE, 'default' => 0, ), - 'comment' => array( - 'description' => 'Whether comments are allowed on this node: 0 = no, 1 = closed (read only), 2 = open (read/write).', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - ), 'promote' => array( 'description' => 'Boolean indicating whether the node should be displayed on the front page.', 'type' => 'int', @@ -234,12 +228,6 @@ function node_schema() { 'not null' => TRUE, 'default' => 1, ), - 'comment' => array( - 'description' => 'Whether comments are allowed on this node (at the time of this revision): 0 = no, 1 = closed (read only), 2 = open (read/write).', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - ), 'promote' => array( 'description' => 'Boolean indicating whether the node (at the time of this revision) should be displayed on the front page.', 'type' => 'int', diff --git a/modules/node/node.module b/modules/node/node.module index 0c3cfb7..45b5ce0 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -172,6 +172,7 @@ function node_entity_info() { 'revision table' => 'node_revision', 'uri callback' => 'node_uri', 'fieldable' => TRUE, + 'comment' => TRUE, 'entity keys' => array( 'id' => 'nid', 'revision' => 'vid',