Commit 9cf9edde authored by alexpott's avatar alexpott

Issue #1912460 by damiankloip, dawehner: Move views_array_key_plus() to RearrangeFilter class.

parent a389298e
<?php
/**
* @file
* Contains \Drupal\Tests\UI\ViewsUITest.
*/
namespace Drupal\Tests\views\UI;
use Drupal\Tests\UnitTestCase;
use Drupal\views_ui\Form\Ajax\RearrangeFilter;
/**
* Tests views_ui functions and methods.
*
* @group Views UI
*/
class ViewsUITest extends UnitTestCase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('views', 'views_ui');
public static function getInfo() {
return array(
'name' => 'Module tests',
'description' => 'Unit tests for Views UI module functions.',
'group' => 'Views UI',
);
}
/**
* Tests static methods.
*/
public function testStaticMethods() {
// Test the RearrangeFilter::arrayKeyPlus method.
$original = array(0 => 'one', 1 => 'two', 2 => 'three');
$expected = array(1 => 'one', 2 => 'two', 3 => 'three');
$this->assertSame(RearrangeFilter::arrayKeyPlus($original), $expected);
}
}
......@@ -1770,22 +1770,6 @@ function _field_view_formatter_options($field_type = NULL) {
return $options;
}
/**
* Adds one to each key of the array.
*
* For example array(0 => 'foo') would be array(1 => 'foo').
*/
function views_array_key_plus($array) {
$keys = array_keys($array);
rsort($keys);
foreach ($keys as $key) {
$array[$key+1] = $array[$key];
unset($array[$key]);
}
asort($array);
return $array;
}
/**
* Set a cached item in the views cache.
*
......
......@@ -293,11 +293,11 @@ public function submitForm(array &$form, array &$form_state) {
// The actual update button was clicked. Remove the empty groups, and
// renumber them sequentially.
ksort($remember_groups);
$groups['groups'] = views_array_key_plus(array_values(array_intersect_key($groups['groups'], $remember_groups)));
$groups['groups'] = static::arrayKeyPlus(array_values(array_intersect_key($groups['groups'], $remember_groups)));
// Change the 'group' key on each field to match. Here, $mapping is an
// array whose keys are the old group numbers and whose values are the new
// (sequentially numbered) ones.
$mapping = array_flip(views_array_key_plus(array_keys($remember_groups)));
$mapping = array_flip(static::arrayKeyPlus(array_keys($remember_groups)));
foreach ($new_fields as &$new_field) {
$new_field['group'] = $mapping[$new_field['group']];
}
......@@ -314,5 +314,30 @@ public function submitForm(array &$form, array &$form_state) {
$form_state['view']->cacheSet();
}
/**
* Adds one to each key of an array.
*
* For example array(0 => 'foo') would be array(1 => 'foo').
*
* @param array
* The array to increment keys on.
*
* @return array
* The array with incremented keys.
*/
public static function arrayKeyPlus($array) {
$keys = array_keys($array);
// Sort the keys in reverse order so incrementing them doesn't overwrite any
// existing keys.
rsort($keys);
foreach ($keys as $key) {
$array[$key + 1] = $array[$key];
unset($array[$key]);
}
// Sort the keys back to ascending order.
ksort($array);
return $array;
}
}
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