diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 1b1dd79..10f98c8 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1177,6 +1177,20 @@ function comment_view_multiple($comments, Node $node, $view_mode = 'full', $weig * Implements hook_form_FORM_ID_alter(). */ function comment_form_node_type_form_alter(&$form, $form_state) { + + if (($form['#form_id'] == 'node_type_form') && (config('comment.settings')->get('comment_saved_' . $form['#node_type']->type) == 'no')) { + // Save the default values to config if config is empty. + config('comment.settings')->set('comment_' . $form['#node_type']->type, COMMENT_NODE_OPEN); + config('comment.settings')->set('comment_default_mode_' . $form['#node_type']->type, COMMENT_MODE_THREADED); + config('comment.settings')->set('comment_default_per_page_' . $form['#node_type']->type, '50'); + config('comment.settings')->set('comment_anonymous_' . $form['#node_type']->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT); + config('comment.settings')->set('comment_subject_field_' . $form['#node_type']->type, '1'); + config('comment.settings')->set('comment_form_location_' . $form['#node_type']->type, COMMENT_FORM_BELOW); + config('comment.settings')->set('comment_preview_' . $form['#node_type']->type, DRUPAL_OPTIONAL); + config('comment.settings')->set('comment_saved_' . $form['#node_type']->type, 'yes'); + config('comment.settings')->save(); + } + if (isset($form['type'])) { $form['comment'] = array( '#type' => 'fieldset', @@ -1191,13 +1205,14 @@ function comment_form_node_type_form_alter(&$form, $form_state) { 'library' => array('comment', 'drupal.comment'), ), ); + // Unlike coment_form_node_form_alter(), all of these settings are applied // as defaults to all new nodes. Therefore, it would be wrong to use #states // to hide the other settings based on the primary comment setting. $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' => (config('comment.settings')->get('comment_saved_' . $form['#node_type']->type) == 'no') ? config('comment.settings')->get('comment_' . $form['#node_type']->type) : unserialize(config('comment.settings')->get('comment_' . $form['#node_type']->type)), '#options' => array( COMMENT_NODE_OPEN => t('Open'), COMMENT_NODE_CLOSED => t('Closed'), @@ -1207,19 +1222,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' => (config('comment.settings')->get('comment_saved_' . $form['#node_type']->type) == 'no') ? config('comment.settings')->get('comment_default_mode_' . $form['#node_type']->type) : unserialize(config('comment.settings')->get('comment_default_mode_' . $form['#node_type']->type)), '#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' => (config('comment.settings')->get('comment_saved_' . $form['#node_type']->type) == 'no') ? config('comment.settings')->get('comment_default_per_page_' . $form['#node_type']->type) : unserialize(config('comment.settings')->get('comment_default_per_page_' . $form['#node_type']->type)), '#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' => (config('comment.settings')->get('comment_saved_' . $form['#node_type']->type) == 'no') ? config('comment.settings')->get('comment_anonymous_' . $form['#node_type']->type) : unserialize(config('comment.settings')->get('comment_anonymous_' . $form['#node_type']->type)), '#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'), @@ -1230,24 +1245,59 @@ 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' => (config('comment.settings')->get('comment_saved_' . $form['#node_type']->type) == 'no') ? config('comment.settings')->get('comment_subject_field_' . $form['#node_type']->type) : unserialize(config('comment.settings')->get('comment_subject_field_' . $form['#node_type']->type)), ); $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' => (config('comment.settings')->get('comment_saved_' . $form['#node_type']->type) == 'no') ? config('comment.settings')->get('comment_form_location_' . $form['#node_type']->type) : unserialize(config('comment.settings')->get('comment_form_location_' . $form['#node_type']->type)), ); $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' => (config('comment.settings')->get('comment_saved_' . $form['#node_type']->type) == 'no') ? config('comment.settings')->get('comment_preview_' . $form['#node_type']->type) : unserialize(config('comment.settings')->get('comment_preview_' . $form['#node_type']->type)), '#options' => array( DRUPAL_DISABLED => t('Disabled'), DRUPAL_OPTIONAL => t('Optional'), DRUPAL_REQUIRED => t('Required'), ), ); - } + $form['#validate'][] = 'comment_validate_config'; + $form['#submit'][] = 'comment_submit_config'; + } +} + +function comment_validate_config($form, &$form_state) { + //TODO + // validate here +} + +function comment_submit_config($form, &$form_state) { + $config = config('comment.settings'); + $comment = db_query("SELECT value FROM {variable} WHERE name = 'comment_" . $form['#node_type']->type . "'")->fetchField(); + $config->set('comment_' . $form['#node_type']->type, $comment); + db_delete('variable')->condition('name', 'comment_' . $form['#node_type']->type)->execute(); + $comment_default_mode = db_query("SELECT value FROM {variable} WHERE name = 'comment_default_mode_" . $form['#node_type']->type . "'")->fetchField(); + $config->set('comment_default_mode_' . $form['#node_type']->type, $comment_default_mode); + db_delete('variable')->condition('name', 'comment_default_mode_' . $form['#node_type']->type)->execute(); + $comment_default_per_page = db_query("SELECT value FROM {variable} WHERE name = 'comment_default_per_page_" . $form['#node_type']->type . "'")->fetchField(); + $config->set('comment_default_per_page_' . $form['#node_type']->type, $comment_default_per_page); + db_delete('variable')->condition('name', 'comment_default_per_page_' . $form['#node_type']->type)->execute(); + $comment_anonymous = db_query("SELECT value FROM {variable} WHERE name = 'comment_anonymous_" . $form['#node_type']->type . "'")->fetchField(); + $config->set('comment_anonymous_' . $form['#node_type']->type, $comment_anonymous); + db_delete('variable')->condition('name', 'comment_anonymous_' . $form['#node_type']->type)->execute(); + $comment_subject_field = db_query("SELECT value FROM {variable} WHERE name = 'comment_subject_field_" . $form['#node_type']->type . "'")->fetchField(); + $config->set('comment_subject_field_' . $form['#node_type']->type, $comment_subject_field); + db_delete('variable')->condition('name', 'comment_subject_field_' . $form['#node_type']->type)->execute(); + $comment_form_location = db_query("SELECT value FROM {variable} WHERE name = 'comment_form_location_" . $form['#node_type']->type . "'")->fetchField(); + $config->set('comment_form_location_' . $form['#node_type']->type, $comment_form_location); + db_delete('variable')->condition('name', 'comment_form_location_' . $form['#node_type']->type)->execute(); + $comment_preview = db_query("SELECT value FROM {variable} WHERE name = 'comment_preview_" . $form['#node_type']->type . "'")->fetchField(); + $config->set('comment_preview_' . $form['#node_type']->type, $comment_preview); + db_delete('variable')->condition('name', 'comment_preview_' . $form['#node_type']->type)->execute(); + $config->set('comment_saved_' . $form['#node_type']->type, 'yes'); + db_delete('variable')->condition('name', 'comment_saved_' . $form['#node_type']->type)->execute(); + $config->save(); } /** diff --git a/core/modules/comment/config/comment.settings.yml b/core/modules/comment/config/comment.settings.yml new file mode 100755 index 0000000..ad0e1d6 --- /dev/null +++ b/core/modules/comment/config/comment.settings.yml @@ -0,0 +1,8 @@ +comment_article: '2' +comment_default_mode_article: '1' +comment_default_per_page_article: '50' +comment_anonymous_article: '0' +comment_subject_field_article: '1' +comment_form_location_article: '1' +comment_preview_article: '0' +comment_saved_article: 'no'