diff --git a/core/modules/forum/config/forum.yml b/core/modules/forum/config/forum.yml new file mode 100644 index 0000000..cc10254 --- /dev/null +++ b/core/modules/forum/config/forum.yml @@ -0,0 +1,7 @@ +hot_topic: 15 +per_page: 25 +order: 1 +nav_vocabulary: 0 +containers: [] +block_num_active: 5 +block_num_new: 5 \ No newline at end of file diff --git a/core/modules/forum/forum.admin.inc b/core/modules/forum/forum.admin.inc index dea573c..454a5bf 100644 --- a/core/modules/forum/forum.admin.inc +++ b/core/modules/forum/forum.admin.inc @@ -45,6 +45,7 @@ function forum_form_main($type, $edit = array()) { * @ingroup forms */ function forum_form_forum($form, &$form_state, $edit = array()) { + $config = config('forum'); $edit += array( 'name' => '', 'description' => '', @@ -71,7 +72,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->get('nav_vocabulary')); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save')); if ($edit['tid']) { @@ -88,6 +89,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'); if ($form['form_id']['#value'] == 'forum_form_container') { $container = TRUE; $type = t('forum container'); @@ -104,9 +106,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); } $form_state['values']['tid'] = $term->tid; drupal_set_message(t('Created new @type %term.', array('%term' => $form_state['values']['name'], '@type' => $type))); @@ -118,6 +120,7 @@ function forum_form_submit($form, &$form_state) { break; } $form_state['redirect'] = 'admin/structure/forum'; + $config->save(); return; } @@ -148,6 +151,7 @@ function theme_forum_form($variables) { * @ingroup forms */ function forum_form_container($form, &$form_state, $edit = array()) { + $config = config('forum'); $edit += array( 'name' => '', 'description' => '', @@ -181,7 +185,7 @@ function forum_form_container($form, &$form_state, $edit = array()) { $form['vid'] = array( '#type' => 'hidden', - '#value' => variable_get('forum_nav_vocabulary', ''), + '#value' => $config->get('nav_vocabulary'), ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( @@ -235,29 +239,47 @@ 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'); $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', + $form['hot_topic'] = array('#type' => 'select', '#title' => t('Hot topic threshold'), - '#default_value' => variable_get('forum_hot_topic', 15), + '#default_value' => $config->get('hot_topic'), '#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', + $form['per_page'] = array('#type' => 'select', '#title' => t('Topics per page'), - '#default_value' => variable_get('forum_per_page', 25), + '#default_value' => $config->get('per_page'), '#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', + $form['order'] = array('#type' => 'radios', '#title' => t('Default order'), - '#default_value' => variable_get('forum_order', 1), + '#default_value' => $config->get('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(). + * + * @see forum_admin_settings() + */ +function forum_admin_settings_submit($form, &$form_state) { + $config = config('forum'); + $config_items = array('hot_topic', 'per_page', 'order'); + foreach ($config_items as $config_item) { + if ($config->get($config_item) != $form_state['values'][$config_item]) { + $config-set($config_item, $form_state['values'][$config_item]); + } + } + $config->save(); } /** @@ -271,8 +293,9 @@ function forum_admin_settings($form) { */ function forum_overview($form, &$form_state) { module_load_include('inc', 'taxonomy', 'taxonomy.admin'); + $config = config('forum'); - $vid = variable_get('forum_nav_vocabulary', ''); + $vid = $config->get('nav_vocabulary'); $vocabulary = taxonomy_vocabulary_load($vid); $form = taxonomy_overview_terms($form, $form_state, $vocabulary); @@ -281,7 +304,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']; } @@ -316,7 +339,6 @@ function forum_overview($form, &$form_state) { * A select form element. */ function _forum_parent_select($tid, $title, $child_type) { - $parents = taxonomy_term_load_parents($tid); if ($parents) { $parent = array_shift($parents); @@ -326,7 +348,7 @@ function _forum_parent_select($tid, $title, $child_type) { $parent = 0; } - $vid = variable_get('forum_nav_vocabulary', ''); + $vid = config('forum')->get('nav_vocabulary'); $children = taxonomy_get_tree($vid, $tid); // A term can't be the child of itself, nor of its children. diff --git a/core/modules/forum/forum.install b/core/modules/forum/forum.install index fb1ba4c..6d159d6 100644 --- a/core/modules/forum/forum.install +++ b/core/modules/forum/forum.install @@ -16,6 +16,7 @@ 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 this to use cmi once node module does. variable_set('node_options_forum', array('status')); } @@ -28,7 +29,8 @@ function forum_enable() { // 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)); + $config = config('forum'); + $vocabulary = taxonomy_vocabulary_load($config->get('nav_vocabulary')); if (!$vocabulary) { $vocabulary = entity_create('taxonomy_vocabulary', array( 'name' => t('Forums'), @@ -40,7 +42,8 @@ function forum_enable() { 'weight' => -10, )); taxonomy_vocabulary_save($vocabulary); - variable_set('forum_nav_vocabulary', $vocabulary->vid); + $config->set('nav_vocabulary', $vocabulary->vid); + $config->save(); } // Create the 'taxonomy_forums' field if it doesn't already exist. @@ -106,14 +109,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'); // Purge field data now to allow taxonomy module to be uninstalled // if this is the only field remaining. @@ -246,3 +241,19 @@ function forum_schema() { function forum_update_last_removed() { return 7003; } + +/** + * Update forum module to use the configuration system. + */ +function forum_update_8000() { + config_install_default_config('forum'); + update_variables_to_config('forum', array( + 'forum_hot_topic' => 'hot_topic', + 'forum_per_page' => 'per_page', + 'forum_order' => 'order', + 'forum_nav_vocabulary' => 'nav_vocabulary', + 'forum_containers' => 'containers', + 'forum_block_num_active' => 'block_num_active', + 'forum_block_num_new' => 'block_num_new', + )); +} diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index ca9bf7e..e09db09 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -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')->get('nav_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')->get('nav_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')->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. @@ -474,12 +474,14 @@ 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'); + $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); + $config->save(); } /** @@ -593,7 +595,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')->get('nav_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 +614,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')->get('nav_vocabulary'); if (isset($form['vid']['#value']) && $form['vid']['#value'] == $vid) { // Hide multiple parents select from forum terms. $form['relations']['parent']['#access'] = FALSE; @@ -659,7 +661,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')->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 +669,9 @@ 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 = config('forum'); + $config->set('block_num_' . $delta, $edit['block_num_' . $delta]); + $config->save(); } /** @@ -677,6 +681,7 @@ function forum_block_save($delta = '', $edit = array()) { * recently added forum topics. */ function forum_block_view($delta = '') { + $config = config('forum'); $query = db_select('forum_index', 'f') ->fields('f') ->addTag('node_access'); @@ -685,14 +690,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_num_active')); 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_num_new')); break; } @@ -767,7 +772,8 @@ function forum_forum_load($tid = NULL) { return $cache[$tid]; } - $vid = variable_get('forum_nav_vocabulary', 0); + $config = config('forum'); + $vid = $config->get('nav_vocabulary'); // Load and validate the parent term. if ($tid) { @@ -784,7 +790,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; } @@ -812,7 +818,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; } @@ -1056,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'); + $vid = $config->get('nav_vocabulary'); $vocabulary = taxonomy_vocabulary_load($vid); $title = !empty($vocabulary->name) ? $vocabulary->name : ''; @@ -1087,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); } @@ -1251,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')->get('hot_topic'); 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'); diff --git a/core/modules/forum/forum.pages.inc b/core/modules/forum/forum.pages.inc index 8538310..12da66a 100644 --- a/core/modules/forum/forum.pages.inc +++ b/core/modules/forum/forum.pages.inc @@ -18,13 +18,14 @@ * @see forum_menu() */ function forum_page($forum_term = NULL) { + $config = config('forum'); 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('per_page'); + $sortby = $config->get('order'); if (empty($forum_term->container)) { $topics = forum_get_topics($forum_term->tid, $sortby, $forum_per_page); diff --git a/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php b/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php index 14a4c7b..3143540 100644 --- a/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php +++ b/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php @@ -215,7 +215,7 @@ class ForumTest extends WebTestBase { */ function testAddOrphanTopic() { // Must remove forum topics to test creating orphan topics. - $vid = variable_get('forum_nav_vocabulary'); + $vid = config('forum')->get('nav_vocabulary'); $tree = taxonomy_get_tree($vid); foreach ($tree as $term) { taxonomy_term_delete($term->tid); @@ -329,7 +329,7 @@ class ForumTest extends WebTestBase { */ function editForumTaxonomy() { // Backup forum taxonomy. - $vid = variable_get('forum_nav_vocabulary', ''); + $vid = config('forum')->get('nav_vocabulary'); $original_settings = taxonomy_vocabulary_load($vid); // Generate a random name/description. @@ -392,7 +392,7 @@ class ForumTest extends WebTestBase { $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')->get('nav_vocabulary'), ':name' => $name, ':desc' => $description))->fetchAssoc(); $this->assertTrue(!empty($term), 'The ' . $type . ' exists in the database'); // Verify forum hierarchy. @@ -420,7 +420,7 @@ class ForumTest extends WebTestBase { // Assert that the associated term has been removed from the // forum_containers variable. - $containers = variable_get('forum_containers', array()); + $containers = config('forum')->get('containers'); $this->assertFalse(in_array($tid, $containers), 'The forum_containers variable has been updated.'); }