Commit 60782b52 authored by Dries's avatar Dries

- Patch #1696902 by sun, larowlan, cosmicdreams, heyrocker, tobiasb: Convert...

- Patch #1696902 by sun, larowlan, cosmicdreams, heyrocker, tobiasb: Convert forum_admin_settings() to new configuration system.
parent 492b12d9
......@@ -71,7 +71,7 @@ function forum_form_forum($form, &$form_state, $edit = array()) {
'#description' => t('Forums are displayed in ascending order by weight (forums with equal weights are displayed alphabetically).'),
);
$form['vid'] = array('#type' => 'hidden', '#value' => variable_get('forum_nav_vocabulary', ''));
$form['vid'] = array('#type' => 'hidden', '#value' => config('forum.settings')->get('vocabulary'));
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
if ($edit['tid']) {
......@@ -88,6 +88,7 @@ function forum_form_forum($form, &$form_state, $edit = array()) {
* Form submission handler for forum_form_forum() and forum_form_container().
*/
function forum_form_submit($form, &$form_state) {
$config = config('forum.settings');
if ($form['form_id']['#value'] == 'forum_form_container') {
$container = TRUE;
$type = t('forum container');
......@@ -104,9 +105,9 @@ function forum_form_submit($form, &$form_state) {
switch ($status) {
case SAVED_NEW:
if ($container) {
$containers = variable_get('forum_containers', array());
$containers = $config->get('containers');
$containers[] = $term->tid;
variable_set('forum_containers', $containers);
$config->set('containers', $containers)->save();
}
$form_state['values']['tid'] = $term->tid;
drupal_set_message(t('Created new @type %term.', array('%term' => $form_state['values']['name'], '@type' => $type)));
......@@ -118,7 +119,6 @@ function forum_form_submit($form, &$form_state) {
break;
}
$form_state['redirect'] = 'admin/structure/forum';
return;
}
/**
......@@ -148,6 +148,7 @@ function theme_forum_form($variables) {
* @ingroup forms
*/
function forum_form_container($form, &$form_state, $edit = array()) {
$config = config('forum.settings');
$edit += array(
'name' => '',
'description' => '',
......@@ -181,7 +182,7 @@ function forum_form_container($form, &$form_state, $edit = array()) {
$form['vid'] = array(
'#type' => 'hidden',
'#value' => variable_get('forum_nav_vocabulary', ''),
'#value' => $config->get('vocabulary'),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
......@@ -235,29 +236,42 @@ function forum_confirm_delete_submit($form, &$form_state) {
* @see system_settings_form()
* @ingroup forms
*/
function forum_admin_settings($form) {
function forum_admin_settings($form, &$form_state) {
$config = config('forum.settings');
$number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100, 150, 200, 250, 300, 350, 400, 500));
$form['forum_hot_topic'] = array('#type' => 'select',
'#title' => t('Hot topic threshold'),
'#default_value' => variable_get('forum_hot_topic', 15),
'#default_value' => $config->get('topics.hot_threshold'),
'#options' => $number,
'#description' => t('The number of replies a topic must have to be considered "hot".'),
);
$number = drupal_map_assoc(array(10, 25, 50, 75, 100));
$form['forum_per_page'] = array('#type' => 'select',
'#title' => t('Topics per page'),
'#default_value' => variable_get('forum_per_page', 25),
'#default_value' => $config->get('topics.page_limit'),
'#options' => $number,
'#description' => t('Default number of forum topics displayed per page.'),
);
$forder = array(1 => t('Date - newest first'), 2 => t('Date - oldest first'), 3 => t('Posts - most active first'), 4 => t('Posts - least active first'));
$form['forum_order'] = array('#type' => 'radios',
'#title' => t('Default order'),
'#default_value' => variable_get('forum_order', 1),
'#default_value' => $config->get('topics.order'),
'#options' => $forder,
'#description' => t('Default display order for topics.'),
);
return system_settings_form($form);
return system_config_form($form, $form_state);
}
/**
* Form submission handler for forum_admin_settings().
*/
function forum_admin_settings_submit($form, &$form_state) {
config('forum.settings')
->set('topics.hot_threshold', $form_state['values']['forum_hot_topic'])
->set('topics.page_limit', $form_state['values']['forum_per_page'])
->set('topics.order', $form_state['values']['forum_order'])
->save();
}
/**
......@@ -271,8 +285,9 @@ function forum_admin_settings($form) {
*/
function forum_overview($form, &$form_state) {
module_load_include('inc', 'taxonomy', 'taxonomy.admin');
$config = config('forum.settings');
$vid = variable_get('forum_nav_vocabulary', '');
$vid = $config->get('vocabulary');
$vocabulary = taxonomy_vocabulary_load($vid);
$form = taxonomy_overview_terms($form, $form_state, $vocabulary);
......@@ -281,7 +296,7 @@ function forum_overview($form, &$form_state) {
$term = $form[$key]['#term'];
$form[$key]['view']['#href'] = 'forum/' . $term['tid'];
unset($form[$key]['operations']['#links']['delete']);
if (in_array($form[$key]['#term']['tid'], variable_get('forum_containers', array()))) {
if (in_array($form[$key]['#term']['tid'], $config->get('containers'))) {
$form[$key]['operations']['#links']['edit']['title'] = t('edit container');
$form[$key]['operations']['#links']['edit']['href'] = 'admin/structure/forum/edit/container/' . $term['tid'];
}
......@@ -326,7 +341,7 @@ function _forum_parent_select($tid, $title, $child_type) {
$parent = 0;
}
$vid = variable_get('forum_nav_vocabulary', '');
$vid = config('forum.settings')->get('vocabulary');
$children = taxonomy_get_tree($vid, $tid);
// A term can't be the child of itself, nor of its children.
......
......@@ -16,6 +16,8 @@ function forum_install() {
->execute();
// Forum topics are published by default, but do not have any other default
// options set (for example, they are not promoted to the front page).
// @todo Convert to default module configuration, once Node module's content
// types are converted.
variable_set('node_options_forum', array('status'));
}
......@@ -27,20 +29,32 @@ function forum_enable() {
// field_associate_fields() as otherwise the field won't be enabled until
// hook modules_enabled is called which takes place after hook_enable events.
field_associate_fields('taxonomy');
// Create the forum vocabulary if it does not exist.
$vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0));
// @todo Change Forum module so forum.settings can contain the vocabulary's
// machine name.
$config = config('forum.settings');
// If the module was disabled only, the current config may contain a valid
// vocabulary ID already.
$vocabulary = taxonomy_vocabulary_load($config->get('vocabulary'));
if (!$vocabulary) {
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => t('Forums'),
'machine_name' => 'forums',
'langcode' => language_default()->langcode,
'description' => t('Forum navigation vocabulary'),
'hierarchy' => 1,
'module' => 'forum',
'weight' => -10,
));
taxonomy_vocabulary_save($vocabulary);
variable_set('forum_nav_vocabulary', $vocabulary->vid);
// If the module was installed and uninstalled previously, the vocabulary
// with machine name 'forums' might still exist.
$vocabulary = taxonomy_vocabulary_machine_name_load('forums');
if (!$vocabulary) {
// Otherwise, installing from scratch; create the vocabulary.
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => t('Forums'),
'machine_name' => 'forums',
'langcode' => language_default()->langcode,
'description' => t('Forum navigation vocabulary'),
'hierarchy' => 1,
'module' => 'forum',
'weight' => -10,
));
taxonomy_vocabulary_save($vocabulary);
}
$config->set('vocabulary', $vocabulary->vid)->save();
}
// Create the 'taxonomy_forums' field if it doesn't already exist.
......@@ -82,11 +96,11 @@ function forum_enable() {
'display' => array(
'default' => array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
'weight' => 10,
),
'teaser' => array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
'weight' => 10,
),
),
);
......@@ -106,12 +120,6 @@ function forum_uninstall() {
// Load the dependent Taxonomy module, in case it has been disabled.
drupal_load('module', 'taxonomy');
variable_del('forum_containers');
variable_del('forum_hot_topic');
variable_del('forum_per_page');
variable_del('forum_order');
variable_del('forum_block_num_active');
variable_del('forum_block_num_new');
variable_del('node_options_forum');
field_delete_field('taxonomy_forums');
......@@ -246,3 +254,20 @@ function forum_schema() {
function forum_update_last_removed() {
return 7003;
}
/**
* Moves forum settings from variable to config.
*
* @ingroup config_upgrade
*/
function forum_update_8000() {
update_variables_to_config('forum.settings', array(
'forum_hot_topic' => 'topics.hot_threshold',
'forum_per_page' => 'topics.page_limit',
'forum_order' => 'topics.order',
'forum_nav_vocabulary' => 'vocabulary',
'forum_containers' => 'containers',
'forum_block_num_active' => 'block.active.limit',
'forum_block_num_new' => 'block.new.limit',
));
}
......@@ -219,7 +219,7 @@ function forum_menu_local_tasks_alter(&$data, $router_item, $root_path) {
*/
function forum_entity_info_alter(&$info) {
// Take over URI construction for taxonomy terms that are forums.
if ($vid = variable_get('forum_nav_vocabulary', 0)) {
if ($vid = config('forum.settings')->get('vocabulary')) {
// Within hook_entity_info(), we can't invoke entity_load() as that would
// cause infinite recursion, so we call taxonomy_vocabulary_get_names()
// instead of taxonomy_vocabulary_load(). All we need is the machine name
......@@ -264,7 +264,7 @@ function _forum_node_check_node_type(Node $node) {
* Implements hook_node_view().
*/
function forum_node_view(Node $node, $view_mode) {
$vid = variable_get('forum_nav_vocabulary', 0);
$vid = config('forum.settings')->get('vocabulary');
$vocabulary = taxonomy_vocabulary_load($vid);
if (_forum_node_check_node_type($node)) {
if ($view_mode == 'full' && node_is_page($node)) {
......@@ -295,7 +295,7 @@ function forum_node_validate(Node $node, $form) {
// vocabulary is selected, not a "container" term.
if (!empty($node->taxonomy_forums[$langcode])) {
// Extract the node's proper topic ID.
$containers = variable_get('forum_containers', array());
$containers = config('forum.settings')->get('containers');
foreach ($node->taxonomy_forums[$langcode] as $delta => $item) {
// If no term was selected (e.g. when no terms exist yet), remove the
// item.
......@@ -308,7 +308,7 @@ function forum_node_validate(Node $node, $form) {
form_set_error('taxonomy_forums', t('Select a forum.'));
continue;
}
$used = db_query_range('SELECT 1 FROM {taxonomy_term_data} WHERE tid = :tid AND vid = :vid',0 , 1, array(
$used = db_query_range('SELECT 1 FROM {taxonomy_term_data} WHERE tid = :tid AND vid = :vid', 0, 1, array(
':tid' => $term->tid,
':vid' => $term->vid,
))->fetchField();
......@@ -474,12 +474,13 @@ function forum_permission() {
*/
function forum_taxonomy_term_delete(Term $term) {
// For containers, remove the tid from the forum_containers variable.
$containers = variable_get('forum_containers', array());
$config = config('forum.settings');
$containers = $config->get('containers');
$key = array_search($term->tid, $containers);
if ($key !== FALSE) {
unset($containers[$key]);
}
variable_set('forum_containers', $containers);
$config->set('containers', $containers)->save();
}
/**
......@@ -593,7 +594,7 @@ function forum_field_storage_pre_update($entity_type, $entity, &$skip_fields) {
* Implements hook_form_FORM_ID_alter() for taxonomy_form_vocabulary().
*/
function forum_form_taxonomy_form_vocabulary_alter(&$form, &$form_state, $form_id) {
$vid = variable_get('forum_nav_vocabulary', 0);
$vid = config('forum.settings')->get('vocabulary');
if (isset($form['vid']['#value']) && $form['vid']['#value'] == $vid) {
$form['help_forum_vocab'] = array(
'#markup' => t('This is the designated forum vocabulary. Some of the normal vocabulary options have been removed.'),
......@@ -612,7 +613,7 @@ function forum_form_taxonomy_form_vocabulary_alter(&$form, &$form_state, $form_i
* Implements hook_form_FORM_ID_alter() for taxonomy_form_term().
*/
function forum_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
$vid = variable_get('forum_nav_vocabulary', 0);
$vid = config('forum.settings')->get('vocabulary');
if (isset($form['vid']['#value']) && $form['vid']['#value'] == $vid) {
// Hide multiple parents select from forum terms.
$form['relations']['parent']['#access'] = FALSE;
......@@ -659,7 +660,7 @@ function forum_block_info() {
* Implements hook_block_configure().
*/
function forum_block_configure($delta = '') {
$form['forum_block_num_' . $delta] = array('#type' => 'select', '#title' => t('Number of topics'), '#default_value' => variable_get('forum_block_num_' . $delta, '5'), '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)));
$form['block_num_' . $delta] = array('#type' => 'select', '#title' => t('Number of topics'), '#default_value' => config('forum.settings')->get('block.num_' . $delta), '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)));
return $form;
}
......@@ -667,7 +668,7 @@ function forum_block_configure($delta = '') {
* Implements hook_block_save().
*/
function forum_block_save($delta = '', $edit = array()) {
variable_set('forum_block_num_' . $delta, $edit['forum_block_num_' . $delta]);
config('forum.settings')->set('block.num_' . $delta, $edit['block_num_' . $delta])->save();
}
/**
......@@ -677,6 +678,7 @@ function forum_block_save($delta = '', $edit = array()) {
* recently added forum topics.
*/
function forum_block_view($delta = '') {
$config = config('forum.settings');
$query = db_select('forum_index', 'f')
->fields('f')
->addTag('node_access')
......@@ -686,14 +688,14 @@ function forum_block_view($delta = '') {
$title = t('Active forum topics');
$query
->orderBy('f.last_comment_timestamp', 'DESC')
->range(0, variable_get('forum_block_num_active', '5'));
->range(0, $config->get('block.active.limit'));
break;
case 'new':
$title = t('New forum topics');
$query
->orderBy('f.created', 'DESC')
->range(0, variable_get('forum_block_num_new', '5'));
->range(0, $config->get('block.new.limit'));
break;
}
......@@ -768,7 +770,8 @@ function forum_forum_load($tid = NULL) {
return $cache[$tid];
}
$vid = variable_get('forum_nav_vocabulary', 0);
$config = config('forum.settings');
$vid = $config->get('vocabulary');
// Load and validate the parent term.
if ($tid) {
......@@ -785,7 +788,7 @@ function forum_forum_load($tid = NULL) {
}
// Determine if the requested term is a container.
if (!$forum_term->tid || in_array($forum_term->tid, variable_get('forum_containers', array()))) {
if (!$forum_term->tid || in_array($forum_term->tid, $config->get('containers'))) {
$forum_term->container = 1;
}
......@@ -813,7 +816,7 @@ function forum_forum_load($tid = NULL) {
foreach ($_forums as $forum) {
// Determine if the child term is a container.
if (in_array($forum->tid, variable_get('forum_containers', array()))) {
if (in_array($forum->tid, $config->get('containers'))) {
$forum->container = 1;
}
......@@ -1059,7 +1062,8 @@ function forum_preprocess_block(&$variables) {
function template_preprocess_forums(&$variables) {
global $user;
$vid = variable_get('forum_nav_vocabulary', 0);
$config = config('forum.settings');
$vid = $config->get('vocabulary');
$vocabulary = taxonomy_vocabulary_load($vid);
$title = !empty($vocabulary->name) ? $vocabulary->name : '';
......@@ -1090,7 +1094,7 @@ function template_preprocess_forums(&$variables) {
$variables['forums'] = '';
}
if ($variables['tid'] && !in_array($variables['tid'], variable_get('forum_containers', array()))) {
if ($variables['tid'] && !in_array($variables['tid'], $config->get('containers'))) {
$variables['topics'] = theme('forum_topic_list', $variables);
drupal_add_feed('taxonomy/term/' . $variables['tid'] . '/feed', 'RSS - ' . $title);
}
......@@ -1254,7 +1258,7 @@ function template_preprocess_forum_topic_list(&$variables) {
* @see theme_forum_icon()
*/
function template_preprocess_forum_icon(&$variables) {
$variables['hot_threshold'] = variable_get('forum_hot_topic', 15);
$variables['hot_threshold'] = config('forum.settings')->get('topics.hot_threshold');
if ($variables['num_posts'] > $variables['hot_threshold']) {
$variables['icon_class'] = $variables['new_posts'] ? 'hot-new' : 'hot';
$variables['icon_title'] = $variables['new_posts'] ? t('Hot topic, new comments') : t('Hot topic');
......
......@@ -18,13 +18,14 @@
* @see forum_menu()
*/
function forum_page($forum_term = NULL) {
$config = config('forum.settings');
if (!isset($forum_term)) {
// On the main page, display all the top-level forums.
$forum_term = forum_forum_load(0);
}
$forum_per_page = variable_get('forum_per_page', 25);
$sortby = variable_get('forum_order', 1);
$forum_per_page = $config->get('topics.page_limit');
$sortby = $config->get('topics.order');
if (empty($forum_term->container)) {
$topics = forum_get_topics($forum_term->tid, $sortby, $forum_per_page);
......
......@@ -215,7 +215,7 @@ function testForum() {
*/
function testAddOrphanTopic() {
// Must remove forum topics to test creating orphan topics.
$vid = variable_get('forum_nav_vocabulary');
$vid = config('forum.settings')->get('vocabulary');
$tree = taxonomy_get_tree($vid);
foreach ($tree as $term) {
taxonomy_term_delete($term->tid);
......@@ -329,7 +329,7 @@ private function doAdminTests($user) {
*/
function editForumTaxonomy() {
// Backup forum taxonomy.
$vid = variable_get('forum_nav_vocabulary', '');
$vid = config('forum.settings')->get('vocabulary');
$original_settings = taxonomy_vocabulary_load($vid);
// Generate a random name/description.
......@@ -392,7 +392,7 @@ function createForum($type, $parent = 0) {
$this->assertRaw(t('Created new @type %term.', array('%term' => $name, '@type' => t($type))), t(ucfirst($type) . ' was created'));
// Verify forum.
$term = db_query("SELECT * FROM {taxonomy_term_data} t WHERE t.vid = :vid AND t.name = :name AND t.description = :desc", array(':vid' => variable_get('forum_nav_vocabulary', ''), ':name' => $name, ':desc' => $description))->fetchAssoc();
$term = db_query("SELECT * FROM {taxonomy_term_data} t WHERE t.vid = :vid AND t.name = :name AND t.description = :desc", array(':vid' => config('forum.settings')->get('vocabulary'), ':name' => $name, ':desc' => $description))->fetchAssoc();
$this->assertTrue(!empty($term), 'The ' . $type . ' exists in the database');
// Verify forum hierarchy.
......@@ -420,7 +420,7 @@ function deleteForum($tid) {
// Assert that the associated term has been removed from the
// forum_containers variable.
$containers = variable_get('forum_containers', array());
$containers = config('forum.settings')->get('containers');
$this->assertFalse(in_array($tid, $containers), 'The forum_containers variable has been updated.');
}
......
......@@ -65,7 +65,7 @@ public function testCommentPager() {
*/
public function testForumPager() {
// Lookup the forums vocabulary vid.
$vid = variable_get('forum_nav_vocabulary', 0);
$vid = config('forum.settings')->get('vocabulary');
$this->assertTrue($vid, t('Forum navigation vocabulary found.'));
// Lookup the general discussion term.
......
......@@ -63,7 +63,7 @@ function testUrlAlter() {
// level and for a specific existing forum.
$this->assertUrlInboundAlter('community', 'forum');
$this->assertUrlOutboundAlter('forum', 'community');
$forum_vid = variable_get('forum_nav_vocabulary');
$forum_vid = config('forum.settings')->get('vocabulary');
$tid = db_insert('taxonomy_term_data')
->fields(array(
'name' => $this->randomName(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment