Skip to content

Commit

Permalink
Issue backdrop#764: Improve comment settings on both node form and no…
Browse files Browse the repository at this point in the history
…de type form.
  • Loading branch information
Jen Lampton committed Mar 3, 2015
1 parent 9a082ed commit 2b07e68
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
17 changes: 15 additions & 2 deletions core/modules/comment/comment.install
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* @file
* Install, update and uninstall functions for the Comment module.
Expand Down Expand Up @@ -351,7 +350,21 @@ function comment_update_1002() {
update_variable_del('comment_maintain_node_statistics');
}

/**
* Node type comment settings should be closed instead of hidden by default.
*/
function comment_update_1003() {
foreach (config_get_names_with_prefix('node.type.') as $config_name)
$config = config($config_name);
$comment_default = $config->get('settings.comment_default');
if ($comment_default == COMMENT_NODE_HIDDEN) {
$config->set('settings.comment_default', COMMENT_NODE_CLOSED);
$config->save();
}
}
}

/**
* @} End of "addtogroup updates-7.x-to-1.x"
* The next series of updates should start at 2000.
*/
*/
47 changes: 29 additions & 18 deletions core/modules/comment/comment.module
Original file line number Diff line number Diff line change
Expand Up @@ -1139,20 +1139,19 @@ function comment_form_node_type_form_alter(&$form, $form_state) {
'class' => array('comment-node-type-settings-form'),
),
'#attached' => array(
'js' => array(backdrop_get_path('module', 'comment') . '/js/comment-node-form.js'),
'js' => array(backdrop_get_path('module', 'comment') . '/js/comment.admin.js'),
),
);
// 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_default'] = array(
'#type' => 'select',
'#type' => 'radios',
'#title' => t('Default comment setting for new content'),
'#default_value' => $node_type->settings['comment_default'],
'#options' => array(
COMMENT_NODE_OPEN => t('Open'),
COMMENT_NODE_CLOSED => t('Closed'),
COMMENT_NODE_HIDDEN => t('Hidden'),
COMMENT_NODE_OPEN => t('Open comments'),
COMMENT_NODE_CLOSED => t('Close comments'),
),
);
$form['comment']['comment_per_page'] = array(
Expand Down Expand Up @@ -1224,12 +1223,12 @@ function comment_form_node_form_alter(&$form, $form_state) {
'class' => array('comment-node-settings-form'),
),
'#attached' => array(
'js' => array(backdrop_get_path('module', 'comment') . '/js/comment-node-form.js'),
'js' => array(backdrop_get_path('module', 'comment') . '/js/comment.admin.js'),
),
'#weight' => 80,
);
$comment_count = isset($node->nid) ? db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField() : 0;
$comment_settings = ($node->comment == COMMENT_NODE_HIDDEN && empty($comment_count)) ? COMMENT_NODE_CLOSED : $node->comment;
$comment_settings = ($node->comment == COMMENT_NODE_HIDDEN) ? COMMENT_NODE_CLOSED : $node->comment;
$form['comment_settings']['comment'] = array(
'#type' => 'radios',
'#title' => t('Comments'),
Expand All @@ -1239,24 +1238,36 @@ function comment_form_node_form_alter(&$form, $form_state) {
'#options' => array(
COMMENT_NODE_OPEN => t('Open'),
COMMENT_NODE_CLOSED => t('Closed'),
COMMENT_NODE_HIDDEN => t('Hidden'),
),
COMMENT_NODE_OPEN => array(
'#description' => t('Users with the "Post comments" permission can post comments.'),
),
COMMENT_NODE_CLOSED => array(
'#description' => t('Users cannot post comments, but existing comments will be displayed.'),
),
COMMENT_NODE_HIDDEN => array(
'#description' => t('Comments are hidden from view.'),
'#description' => t('Users cannot post comments.'),
),
);
// If the node doesn't have any comments, the "hidden" option makes no
// sense, so don't even bother presenting it to the user.
if (empty($comment_count)) {
$form['comment_settings']['comment'][COMMENT_NODE_HIDDEN]['#access'] = FALSE;
// Also adjust the description of the "closed" option.
$form['comment_settings']['comment'][COMMENT_NODE_CLOSED]['#description'] = t('Users cannot post comments.');
// If the node has comments, add the "hidden" option too.
if (!empty($comment_count)) {
$form['comment_settings']['comment_hidden'] = array(
'#type' => 'checkbox',
'#title' => t('Hide existing comments.'),
'#default_value' => ($node->comment == COMMENT_NODE_HIDDEN) ? TRUE : FALSE,
'#states' => array(
'visible' => array(
':input[name="comment"]' => array('value' => COMMENT_NODE_CLOSED),
),
),
);
}
}

/**
* Implements hook_node_presave().
*/
function comment_node_presave(Node $node) {
if ($node->comment == COMMENT_NODE_CLOSED && property_exists($node, 'comment_hidden') && $node->comment_hidden) {
$node->comment = COMMENT_NODE_HIDDEN;
unset($node->comment_hidden);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ Backdrop.behaviors.commentFieldsetSummaries = {
attach: function (context) {
var $context = $(context);
$context.find('fieldset.comment-node-settings-form').backdropSetSummary(function () {
return Backdrop.checkPlain($context.find('.form-item-comment input:checked').next('label').text());
var vals = [];
vals.push(Backdrop.checkPlain($context.find('.form-item-comment input:checked').next('label').text()));
if ($context.find(".form-item-comment-hidden input:checked").length) {
vals.push(Backdrop.t('Hidden'));
}
return Backdrop.checkPlain(vals.join(', '));
});

// Provide the summary for the node type form.
$context.find('fieldset.comment-node-type-settings-form').backdropSetSummary(function() {
var vals = [];

// Default comment setting.
vals.push($context.find(".form-item-comment-default select option:selected").text());
vals.push($context.find(".form-item-comment-default input:checked").parent().find('label').text());

// Comments per page.
var number = parseInt($context.find(".form-item-comment-per-page select option:selected").val());
Expand Down

0 comments on commit 2b07e68

Please sign in to comment.