diff --git a/core/includes/common.inc b/core/includes/common.inc index 3ff8096..a778e50 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1,7 +1,7 @@ , ', &, and " using the json_encode() options parameter. + return json_encode($var, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); } /** @@ -3913,11 +3912,9 @@ function drupal_json_encode($var) { * * @see drupal_json_encode() * @ingroup php_wrappers - * @deprecated as of Drupal 8.0. Use Drupal\Component\Utility\Json::decode() - * directly instead. */ function drupal_json_decode($var) { - return Json::decode($var); + return json_decode($var, TRUE); } /** @@ -5100,14 +5097,11 @@ function drupal_render_cid_create($elements) { * that optionally include a '#weight' key. * @param $b * Second item for comparison. + * + * @see \Drupal\Component\Utility\SortArray::sortByNumber() */ function element_sort($a, $b) { - $a_weight = (is_array($a) && isset($a['#weight'])) ? $a['#weight'] : 0; - $b_weight = (is_array($b) && isset($b['#weight'])) ? $b['#weight'] : 0; - if ($a_weight == $b_weight) { - return 0; - } - return ($a_weight < $b_weight) ? -1 : 1; + return SortArray::sortByNumber('#weight', $a, $b); } /** @@ -5122,11 +5116,11 @@ function element_sort($a, $b) { * that optionally include a '#title' key. * @param $b * Second item for comparison. + * + * @see \Drupal\Component\Utility\SortArray::sortByString() */ function element_sort_by_title($a, $b) { - $a_title = (is_array($a) && isset($a['#title'])) ? $a['#title'] : ''; - $b_title = (is_array($b) && isset($b['#title'])) ? $b['#title'] : ''; - return strnatcasecmp($a_title, $b_title); + return SortArray::sortByString('#title', $a, $b); } /** @@ -5184,14 +5178,12 @@ function element_info_property($type, $property_name, $default = NULL) { * element, a default value of 0 will be used. * @param $b * Second item for comparison. + * + * @see \Drupal\Component\Utility\SortArray::sortByNumber() + * */ -function drupal_sort_weight($a, $b) { - $a_weight = (is_array($a) && isset($a['weight'])) ? $a['weight'] : 0; - $b_weight = (is_array($b) && isset($b['weight'])) ? $b['weight'] : 0; - if ($a_weight == $b_weight) { - return 0; - } - return ($a_weight < $b_weight) ? -1 : 1; +function drupal_sort_weight(array $a, array $b) { + return SortArray::sortByNumber('weight', $a, $b); } /** @@ -5204,15 +5196,11 @@ function drupal_sort_weight($a, $b) { * that optionally include a 'title' key. * @param $b * Second item for comparison. + * + * @see \Drupal\Component\Utility\SortArray::sortByString() */ function drupal_sort_title($a, $b) { - if (!isset($b['title'])) { - return -1; - } - if (!isset($a['title'])) { - return 1; - } - return strcasecmp($a['title'], $b['title']); + return SortArray::sortByString('title', $a, $b); } /** @@ -5699,7 +5687,7 @@ function drupal_get_updaters() { if (!isset($updaters)) { $updaters = module_invoke_all('updater_info'); drupal_alter('updater_info', $updaters); - uasort($updaters, 'drupal_sort_weight'); + uasort($updaters, array('Drupal\Component\Utility\SortArray', 'sortWeight')); } return $updaters; } @@ -5719,7 +5707,7 @@ function drupal_get_filetransfer_info() { if (!isset($info)) { $info = module_invoke_all('filetransfer_info'); drupal_alter('filetransfer_info', $info); - uasort($info, 'drupal_sort_weight'); + uasort($info, array('Drupal\Component\Utility\SortArray', 'sortWeight')); } return $info; } diff --git a/core/includes/update.inc b/core/includes/update.inc index 3659caf..e4962a3 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -1033,7 +1033,7 @@ function update_resolve_dependencies($starting_updates) { // Perform the depth-first search and sort on the results. $graph_object = new Graph($graph); $graph = $graph_object->searchAndSort(); - uasort($graph, 'drupal_sort_weight'); + uasort($graph, array('Drupal\Component\Utility\SortArray', 'sortWeight')); foreach ($graph as $function => &$data) { $module = $data['module']; diff --git a/core/lib/Drupal/Component/Utility/SortArray.php b/core/lib/Drupal/Component/Utility/SortArray.php new file mode 100644 index 0000000..a3365bb --- /dev/null +++ b/core/lib/Drupal/Component/Utility/SortArray.php @@ -0,0 +1,70 @@ +getOperations($entity); $this->moduleHandler->alter('entity_operation', $operations, $entity); - uasort($operations, 'drupal_sort_weight'); + uasort($operations, array('Drupal\Component\Utility\SortArray', 'sortWeight')); $build = array( '#type' => 'operations', '#links' => $operations, diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigEntityListTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigEntityListTest.php index 7917731..789162f 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigEntityListTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigEntityListTest.php @@ -75,7 +75,7 @@ function testList() { $actual_operations = $controller->getOperations($entity); // Sort the operations to normalize link order. - uasort($actual_operations, 'drupal_sort_weight'); + uasort($actual_operations, array('Drupal\Component\Utility\SortArray', 'sortWeight')); $this->assertIdentical($expected_operations, $actual_operations); // Test buildHeader() method. @@ -148,7 +148,7 @@ function testList() { $actual_operations = $controller->getOperations($entity); // Sort the operations to normalize link order. - uasort($actual_operations, 'drupal_sort_weight'); + uasort($actual_operations, array('Drupal\Component\Utility\SortArray', 'sortWeight')); $this->assertIdentical($expected_operations, $actual_operations); } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php index d51544d..109882a 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php @@ -57,7 +57,7 @@ public function getInstance(array $options) { $selection_handler_groups = $this->getSelectionGroups($target_entity_type); // Sort the selection plugins by weight and select the best match. - uasort($selection_handler_groups[$selection_handler], 'drupal_sort_weight'); + uasort($selection_handler_groups[$selection_handler], array('Drupal\Component\Utility\SortArray', 'sortWeight')); end($selection_handler_groups[$selection_handler]); $plugin_id = key($selection_handler_groups[$selection_handler]); diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php index b8213ca..a547b12 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php @@ -143,7 +143,7 @@ public function getOptions($field_type = NULL) { $options = array(); $field_types = field_info_field_types(); $widget_types = $this->getDefinitions(); - uasort($widget_types, 'drupal_sort_weight'); + uasort($widget_types, array('Drupal\Component\Utility\SortArray', 'sortWeight')); foreach ($widget_types as $name => $widget_type) { foreach ($widget_type['field_types'] as $widget_field_type) { // Check that the field type exists. diff --git a/core/modules/field_ui/lib/Drupal/field_ui/OverviewBase.php b/core/modules/field_ui/lib/Drupal/field_ui/OverviewBase.php index 67b742b..a15d496 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/OverviewBase.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/OverviewBase.php @@ -222,7 +222,7 @@ public function reduceOrder($array, $a) { $array[] = $a['name']; } if (!empty($a['children'])) { - uasort($a['children'], 'drupal_sort_weight'); + uasort($a['children'], array('Drupal\Component\Utility\SortArray', 'sortWeight')); $array = array_merge($array, array_reduce($a['children'], array($this, 'reduceOrder'))); } return $array; diff --git a/core/modules/image/lib/Drupal/image/ImageStyleStorageController.php b/core/modules/image/lib/Drupal/image/ImageStyleStorageController.php index 3f35f5b..6555768 100644 --- a/core/modules/image/lib/Drupal/image/ImageStyleStorageController.php +++ b/core/modules/image/lib/Drupal/image/ImageStyleStorageController.php @@ -27,7 +27,7 @@ protected function attachLoad(&$queried_entities, $revision_id = FALSE) { $style->effects[$ieid] = $effect; } // Sort effects by weight. - uasort($style->effects, 'drupal_sort_weight'); + uasort($style->effects, array('Drupal\Component\Utility\SortArray', 'sortWeight')); } } parent::attachLoad($queried_entities, $revision_id); diff --git a/core/modules/layout/lib/Drupal/layout/Plugin/Core/Entity/Display.php b/core/modules/layout/lib/Drupal/layout/Plugin/Core/Entity/Display.php index adaf260..32f6b42 100644 --- a/core/modules/layout/lib/Drupal/layout/Plugin/Core/Entity/Display.php +++ b/core/modules/layout/lib/Drupal/layout/Plugin/Core/Entity/Display.php @@ -118,7 +118,7 @@ protected function sortBlocks() { } foreach ($regions as $region_name => &$blocks) { - uasort($blocks, 'drupal_sort_weight'); + uasort($blocks, array('Drupal\Component\Utility\SortArray', 'sortWeight')); $this->blocksInRegions[$region_name] = array_keys($blocks); } } diff --git a/core/modules/taxonomy/taxonomy.admin.inc b/core/modules/taxonomy/taxonomy.admin.inc index e8bd8e0..1c6fdaf 100644 --- a/core/modules/taxonomy/taxonomy.admin.inc +++ b/core/modules/taxonomy/taxonomy.admin.inc @@ -312,7 +312,7 @@ function taxonomy_overview_terms_submit($form, &$form_state) { } // Sort term order based on weight. - uasort($form_state['values']['terms'], 'drupal_sort_weight'); + uasort($form_state['values']['terms'], array('Drupal\Component\Utility\SortArray', 'sortWeight')); $vocabulary = $form_state['taxonomy']['vocabulary']; // Update the current hierarchy type as we go. diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/filter/FilterPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/filter/FilterPluginBase.php index ec65d2c..5257d33 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/filter/FilterPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/filter/FilterPluginBase.php @@ -667,7 +667,7 @@ protected function buildGroupValidate($form, &$form_state) { */ protected function buildGroupSubmit($form, &$form_state) { $groups = array(); - uasort($form_state['values']['options']['group_info']['group_items'], 'drupal_sort_weight'); + uasort($form_state['values']['options']['group_info']['group_items'], array('Drupal\Component\Utility\SortArray', 'sortWeight')); // Filter out removed items. // Start from 1 to avoid problems with #default_value in the widget. diff --git a/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php new file mode 100644 index 0000000..d3a5f9a --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php @@ -0,0 +1,198 @@ + 'SortArray test', + 'description' => 'Test that the SortArray functions work properly.', + 'group' => 'Common', + ); + } + + /** + * Tests SortArray::sortByNumber() input against expected output. + * + * @dataProvider providersortByNumber + * + * @param array $property + * The property of the arrays to compare. + * @param array $a + * The first input array for the SortArray::sortByNumber() method. + * @param array $b + * The second input array for the SortArray::sortByNumber(). + * @param integer $expected + * The expected output from calling the method. + * + * @see Drupal\Component\Utility\SortArray::sortByNumber() + * @see Drupal\Tests\Component\Utility\SortArrayTest::providersortByNumber() + */ + public function testsortByNumber($property, $a, $b, $expected) { + $result = SortArray::sortByNumber($property, $a, $b); + $this->assertEquals($expected, $result); + } + + /** + * Data provider for SortArray::sortByNumber(). + * + * @return array + * An array of tests, matching the parameter inputs for testsortByNumber. + * + * @see \Drupal\Component\Utility\SortArray::sortByNumber() + * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByNumber() + */ + public function providerSortByNumber() { + $tests = array(); + + // Weights set and equal + $tests[] = array( + 'weight', + array('weight' => 1), + array('weight' => 1), + 0 + ); + + // Weights set and $a is less (lighter) than $b + $tests[] = array( + 'weight', + array('weight' => 1), + array('weight' => 2), + -1 + ); + + // Weights set and $a is greater (heavier) than $b + $tests[] = array( + 'weight', + array('weight' => 2), + array('weight' => 1), + 1 + ); + + // Weights not set + $tests[] = array( + 'weight', + array(), + array(), + 0 + ); + + // Weights for $b not set + $tests[] = array( + '#weight', + array('#weight' => 1), + array(), + 1 + ); + + // Weights for $a not set + $tests[] = array( + '#weight', + array(), + array('#weight' => 1), + -1 + ); + + return $tests; + } + + /** + * Tests SortArray::sortByString() input against expected output. + * + * @dataProvider providerSortByString + * + * @param array $property + * The property of the arrays to compare. + * @param array $a + * The first input item for comparison. + * @param array $b + * The second item for comparison. + * @param integer $expected + * The expected output from calling the method. + * + * @see Drupal\Component\Utility\SortArray::sortByString() + * @see Drupal\Tests\Component\Utility\SortArrayTest::providerSortByString() + */ + public function testSortByString($property, $a, $b, $expected) { + $result = SortArray::sortByString($property, $a, $b); + $this->assertEquals($expected, $result); + } + + /** + * Data provider for SortArray::sortByString(). + * + * @return array + * An array of tests, matching the parameter inputs for testSortByString. + * + * @see \Drupal\Component\Utility\SortArray::sortByString() + * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByString() + */ + public function providerSortByString() { + $tests = array(); + + // Titles set and equal + $tests[] = array( + 'title', + array('title' => 'test'), + array('title' => 'test'), + 0 + ); + + // Title $a not set + $tests[] = array( + 'title', + array(), + array('title' => 'test'), + 1 + ); + + // Title $b not set + $tests[] = array( + 'title', + array('title' => 'test'), + array(), + -1 + ); + + // Titles set but not equal + $tests[] = array( + 'title', + array('title' => 'test'), + array('title' => 'testing'), + -3 + ); + + // Titles set but not equal + $tests[] = array( + 'title', + array('title' => 'testing'), + array('title' => 'test'), + 3 + ); + + // Different property name + $tests[] = array( + '#title', + array('#title' => 'testing'), + array('#title' => 'test'), + 3 + ); + + return $tests; + } + +}