diff --git a/core/modules/node/config/search.plugin.node_search.yml b/core/modules/node/config/search.plugin.node_search.yml new file mode 100644 index 0000000..102d659 --- /dev/null +++ b/core/modules/node/config/search.plugin.node_search.yml @@ -0,0 +1,5 @@ +rank: + promote: 0 + recent: 0 + relevance: 0 + sticky: 0 diff --git a/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php b/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php index 4b2d488..d632808 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php +++ b/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php @@ -272,8 +272,8 @@ protected function addNodeRankings(SelectExtender $query) { if ($ranking = $this->moduleHandler->invokeAll('ranking')) { $tables = &$query->getTables(); foreach ($ranking as $rank => $values) { - // @todo - move rank out of drupal variables. - if ($node_rank = variable_get('node_rank_' . $rank, 0)) { + $node_rank = \Drupal::config('search.plugin.node_search')->get('rank.' . $rank); + if (!empty($node_rank)) { // If the table defined in the ranking isn't already joined, then add it. if (isset($values['join']) && !isset($tables[$values['join']['alias']])) { $query->addJoin($values['join']['type'], $values['join']['table'], $values['join']['alias'], $values['join']['on']); @@ -535,7 +535,7 @@ public function buildConfigurationForm(array $form, array &$form_state) { '#title' => $values['title'], '#type' => 'select', '#options' => $options, - '#default_value' => variable_get('node_rank_' . $var, 0), + '#default_value' => \Drupal::config('search.plugin.node_search')->get('rank.' . $var), ); } return $form; @@ -551,12 +551,13 @@ public function validateConfigurationForm(array &$form, array &$form_state) { * {@inheritdoc} */ public function submitConfigurationForm(array &$form, array &$form_state) { + $config = \Drupal::config('search.plugin.node_search'); foreach ($this->moduleHandler->invokeAll('ranking') as $var => $values) { if (isset($form_state['values']['node_rank_' . $var])) { - // @todo Fix when https://drupal.org/node/1831632 is in. - variable_set('node_rank_' . $var, $form_state['values']['node_rank_' . $var]); + $config->set('rank.' . $var, $form_state['values']['node_rank_' . $var]); } } + $config->save(); } } diff --git a/core/modules/node/node.install b/core/modules/node/node.install index b240ecc..817fea2 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -6,6 +6,7 @@ */ use Drupal\Component\Uuid\Uuid; +use Drupal\Core\Config\FileStorage; use Drupal\Core\Language\Language; /** @@ -450,12 +451,6 @@ function node_uninstall() { \Drupal::config('language.settings')->clear('node. ' . $type . '.language.default_configuration')->save(); } - // Delete node search ranking variables. - variable_del('node_rank_relevance'); - variable_del('node_rank_sticky'); - variable_del('node_rank_promote'); - variable_del('node_rank_recent'); - // Delete remaining general module variables. \Drupal::state()->delete('node.node_access_needs_rebuild'); variable_del('node_recent_block_count'); @@ -1164,6 +1159,38 @@ function node_update_8021() { } /** + * Converts node search ranking variables to config. + * + * @ingroup config_upgrade + */ +function node_update_8022() { + if (\Drupal::moduleHandler()->moduleExists('search')) { + $config_name = 'search.plugin.node_search'; + $config = Drupal::config($config_name); + + // Load and set default configuration values. + $file = new FileStorage(drupal_get_path('module', 'node') . '/config'); + $default_data = $file->read($config_name); + + // Apply the default values. + $config->setData($default_data); + + $variables = db_query("SELECT name, value FROM {variable} WHERE name like 'node_rank_%'")->fetchAllKeyed(); + if (!empty($variables)) { + foreach ($variables as $name => $value) { + // Trim node_rank_ + $rank = substr($name, 10); + $config->set('rank.' . $rank, unserialize($value)); + } + } + $config->save(); + } + + // Clean up any node_rank_ variables. + db_delete('variable')->condition('name', db_like('node_rank_') . '%', 'LIKE')->execute(); +} + +/** * @} End of "addtogroup updates-7.x-to-8.x" * The next series of updates should start at 9000. */ diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php index bb4e6ba..8682cd5 100644 --- a/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php +++ b/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php @@ -114,10 +114,13 @@ public function testRankings() { array_pop($node_ranks); array_pop($node_ranks); foreach ($node_ranks as $node_rank) { + $config = \Drupal::config('search.plugin.node_search'); // Disable all relevancy rankings except the one we are testing. foreach ($node_ranks as $var) { - variable_set('node_rank_' . $var, $var == $node_rank ? 10 : 0); + $config->set('rank.' . $var, $var == $node_rank ? 10 : 0); } + $config->save(); + $this->nodeSearchPlugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); // Do the search and assert the results. $this->nodeSearchPlugin->setSearch('rocks', array(), array()); $set = $this->nodeSearchPlugin->execute(); @@ -172,9 +175,12 @@ public function testHTMLRankings() { // Disable all other rankings. $node_ranks = array('sticky', 'promote', 'recent', 'comments', 'views'); + $config = \Drupal::config('search.plugin.node_search'); foreach ($node_ranks as $node_rank) { - variable_set('node_rank_' . $node_rank, 0); + $config->set('rank.' . $node_rank, 0); } + $config->save(); + $this->nodeSearchPlugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); $this->nodeSearchPlugin->setSearch('rocks', array(), array()); // Do the search and assert the results. $set = $this->nodeSearchPlugin->execute(); @@ -245,11 +251,13 @@ function testDoubleRankings() { // Set up for ranking sticky and lots of comments; make sure others are // disabled. $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views'); + $config = \Drupal::config('search.plugin.node_search'); foreach ($node_ranks as $var) { $value = ($var == 'sticky' || $var == 'comments') ? 10 : 0; - variable_set('node_rank_' . $var, $value); + $config->set('rank.' . $var, $value); } - + $config->save(); + $this->nodeSearchPlugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); // Do the search and assert the results. $this->nodeSearchPlugin->setSearch('rocks', array(), array()); // Do the search and assert the results. diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php index 6b0ad23..9f77b0e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php @@ -168,6 +168,13 @@ public function testVariableUpgrade() { 'source.list_max' => 5, ); + $expected_config['search.plugin.node_search'] = array( + 'rank.promote' => 10, + 'rank.views' => 5, + 'rank.comments' => -2, + 'rank.custom' => 6, + ); + foreach ($expected_config as $file => $values) { $config = \Drupal::config($file); $this->verbose(print_r($config->get(), TRUE)); diff --git a/core/modules/system/tests/upgrade/drupal-7.system.database.php b/core/modules/system/tests/upgrade/drupal-7.system.database.php index f15d406..62e2102 100644 --- a/core/modules/system/tests/upgrade/drupal-7.system.database.php +++ b/core/modules/system/tests/upgrade/drupal-7.system.database.php @@ -163,6 +163,22 @@ 'name' => 'aggregator_summary_items', 'value' => 'i:5;', )) + ->values(array( + 'name' => 'node_rank_promote', + 'value' => 'i:10;', + )) + ->values(array( + 'name' => 'node_rank_views', + 'value' => 'i:5;', + )) + ->values(array( + 'name' => 'node_rank_comments', + 'value' => 'i:-2;', + )) + ->values(array( + 'name' => 'node_rank_custom', + 'value' => 'i:6;', + )) ->execute(); db_update('variable')