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..5aa4eb4 --- /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 cdc399f..116a01d 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php +++ b/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php @@ -8,7 +8,6 @@ namespace Drupal\node\Plugin\Search; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Config\Config; use Drupal\Core\Database\Connection; use Drupal\Core\Database\Query\SelectExtender; use Drupal\Core\Entity\EntityInterface; @@ -59,13 +58,6 @@ class NodeSearch extends SearchPluginBase implements AccessibleInterface, Search protected $moduleHandler; /** - * A config object for 'search.settings'. - * - * @var \Drupal\Core\Config\Config - */ - protected $searchSettings; - - /** * The Drupal state object used to set 'node.cron_last'. * * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface @@ -110,7 +102,6 @@ static public function create(ContainerInterface $container, array $configuratio $container->get('database'), $container->get('plugin.manager.entity'), $container->get('module_handler'), - $container->get('config.factory')->get('search.settings'), $container->get('keyvalue')->get('state'), $container->get('current_user') ); @@ -131,18 +122,15 @@ static public function create(ContainerInterface $container, array $configuratio * An entity manager object. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * A module manager object. - * @param \Drupal\Core\Config\Config $search_settings - * A config object for 'search.settings'. * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state * The Drupal state object used to set 'node.cron_last'. * @param \Drupal\Core\Session\AccountInterface $account * The $account object to use for checking for access to advanced search. */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database, EntityManager $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, KeyValueStoreInterface $state, AccountInterface $account = NULL) { + public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database, EntityManager $entity_manager, ModuleHandlerInterface $module_handler, KeyValueStoreInterface $state, AccountInterface $account = NULL) { $this->database = $database; $this->entityManager = $entity_manager; $this->moduleHandler = $module_handler; - $this->searchSettings = $search_settings; $this->state = $state; $this->account = $account; parent::__construct($configuration, $plugin_id, $plugin_definition); @@ -272,14 +260,14 @@ 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)) { + $rank = $this->configuration['search.plugin.' . $this->pluginId]->get('rank.' . $rank); + if (!empty($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']); } $arguments = isset($values['arguments']) ? $values['arguments'] : array(); - $query->addScore($values['score'], $arguments, $node_rank); + $query->addScore($values['score'], $arguments, $rank); } } } @@ -289,7 +277,7 @@ protected function addNodeRankings(SelectExtender $query) { * {@inheritdoc} */ public function updateIndex() { - $limit = (int) $this->searchSettings->get('index.cron_limit'); + $limit = (int) $this->configuration['search.settings']->get('index.cron_limit'); $result = $this->database->queryRange("SELECT DISTINCT n.nid FROM {node} n LEFT JOIN {search_dataset} d ON d.type = :type AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, n.nid ASC", 0, $limit, array(':type' => $this->getPluginId()), array('target' => 'slave')); $nids = $result->fetchCol(); @@ -531,11 +519,12 @@ public function buildConfigurationForm(array $form, array &$form_state) { // Note: reversed to reflect that higher number = higher ranking. $options = drupal_map_assoc(range(0, 10)); foreach ($this->moduleHandler->invokeAll('ranking') as $var => $values) { + $rank = $this->configuration['search.plugin.' . $this->pluginId]->get('rank.' . $var); $form['content_ranking']['factors']['node_rank_' . $var] = array( '#title' => $values['title'], '#type' => 'select', '#options' => $options, - '#default_value' => variable_get('node_rank_' . $var, 0), + '#default_value' => isset($rank) ? $rank : 0, ); } return $form; @@ -551,12 +540,13 @@ public function validateConfigurationForm(array &$form, array &$form_state) { * {@inheritdoc} */ public function submitConfigurationForm(array &$form, array &$form_state) { + $config = $this->configuration['search.plugin.' . $this->pluginId]; 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 1264c78..067bd7e 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; /** @@ -423,12 +424,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'); @@ -1137,6 +1132,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/Plugin/SearchPluginBase.php b/core/modules/search/lib/Drupal/search/Plugin/SearchPluginBase.php index 7f534d5..0cba8f9 100644 --- a/core/modules/search/lib/Drupal/search/Plugin/SearchPluginBase.php +++ b/core/modules/search/lib/Drupal/search/Plugin/SearchPluginBase.php @@ -9,6 +9,7 @@ use Drupal\Core\Plugin\PluginBase; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Defines a base class for plugins wishing to support search. @@ -39,6 +40,17 @@ /** * {@inheritdoc} */ + static public function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition + ); + } + + /** + * {@inheritdoc} + */ public function setSearch($keywords, array $parameters, array $attributes) { $this->keywords = (string) $keywords; $this->searchParameters = $parameters; diff --git a/core/modules/search/lib/Drupal/search/SearchPluginManager.php b/core/modules/search/lib/Drupal/search/SearchPluginManager.php index 612db99..6216913 100644 --- a/core/modules/search/lib/Drupal/search/SearchPluginManager.php +++ b/core/modules/search/lib/Drupal/search/SearchPluginManager.php @@ -119,4 +119,29 @@ public function pluginAccess($plugin_id, AccountInterface $account) { } return TRUE; } + + /** + * {@inheritdoc} + */ + public function createInstance($plugin_id, array $configuration = array()) { + $configuration = array_merge($configuration, $this->getConfiguration($plugin_id)); + return $this->factory->createInstance($plugin_id, $configuration); + } + + /** + * Gets plugin configuration stored in the configuration management system. + * + * @param string $plugin_id + * The ID of the plugin to get configuration for. + * + * @return array + * An array of plugin configuration. + */ + protected function getConfiguration($plugin_id) { + $configuration = array(); + $configuration['search.plugin.' . $plugin_id] = $this->configFactory->get('search.plugin.' . $plugin_id); + $configuration['search.settings'] = $this->configFactory->get('search.settings'); + return $configuration; + } + } 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/search/tests/modules/search_extra_type/config/search.plugin.search_extra_type_search.yml b/core/modules/search/tests/modules/search_extra_type/config/search.plugin.search_extra_type_search.yml new file mode 100644 index 0000000..db64246 --- /dev/null +++ b/core/modules/search/tests/modules/search_extra_type/config/search.plugin.search_extra_type_search.yml @@ -0,0 +1 @@ +boost: bi diff --git a/core/modules/search/tests/modules/search_extra_type/lib/Drupal/search_extra_type/Plugin/Search/SearchExtraTypeSearch.php b/core/modules/search/tests/modules/search_extra_type/lib/Drupal/search_extra_type/Plugin/Search/SearchExtraTypeSearch.php index 2ea35eb..21c8c58 100644 --- a/core/modules/search/tests/modules/search_extra_type/lib/Drupal/search_extra_type/Plugin/Search/SearchExtraTypeSearch.php +++ b/core/modules/search/tests/modules/search_extra_type/lib/Drupal/search_extra_type/Plugin/Search/SearchExtraTypeSearch.php @@ -26,40 +26,6 @@ class SearchExtraTypeSearch extends SearchPluginBase implements PluginFormInterface { /** - * @var \Drupal\Core\Config\Config - */ - protected $configSettings; - - /** - * {@inheritdoc} - */ - static public function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { - return new static( - $container->get('config.factory')->get('search_extra_type.settings'), - $configuration, - $plugin_id, - $plugin_definition - ); - } - - /** - * Creates a SearchExtraTypeSearch object. - * - * @param Config $config_settings - * The extra config settings. - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param array $plugin_definition - * The plugin implementation definition. - */ - public function __construct(Config $config_settings, array $configuration, $plugin_id, array $plugin_definition) { - $this->configSettings = $config_settings; - parent::__construct($configuration, $plugin_id, $plugin_definition); - } - - /** * {@inheritdoc} */ public function setSearch($keywords, array $parameters, array $attributes) { @@ -140,7 +106,7 @@ public function buildConfigurationForm(array $form, array &$form_state) { 'bi' => t('Bistromathic'), 'ii' => t('Infinite Improbability'), ), - '#default_value' => $this->configSettings->get('boost'), + '#default_value' => $this->configuration['search.plugin.' . $this->pluginId]->get('boost'), ); return $form; } @@ -155,7 +121,7 @@ public function validateConfigurationForm(array &$form, array &$form_state) { * {@inheritdoc} */ public function submitConfigurationForm(array &$form, array &$form_state) { - $this->configSettings + $this->configuration['search.plugin.' . $this->pluginId] ->set('boost', $form_state['values']['extra_type_settings']['boost']) ->save(); } 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..e0f9c14 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')