Commit 5b041983 authored by catch's avatar catch

Issue #1906146 by damiankloip: Move views_fetch_base_tables() into ViewsDataCache class.

parent 8c82db74
......@@ -35,9 +35,9 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
* Implements Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
$base_tables = array_keys(views_fetch_base_tables());
$this->derivatives = array();
$views_data = drupal_container()->get('views.views_data');
$base_tables = array_keys($views_data->fetchBaseTables());
$this->derivatives = array();
foreach ($base_tables as $table) {
$views_info = $views_data->get($table);
if (empty($views_info['table']['wizard_id'])) {
......
......@@ -29,6 +29,12 @@ public static function getInfo() {
);
}
protected function setUp() {
parent::setUp();
$this->viewsDataCache = $this->container->get('views.views_data');
}
/**
* Tests the views.views_data service.
*
......@@ -38,8 +44,6 @@ public function testViewsFetchData() {
$table_name = 'views_test_data';
$expected_data = $this->viewsData();
$this->viewsDataCache = drupal_container()->get('views.views_data');
$data = $this->viewsDataCache->get($table_name);
$this->assertEqual($data, $expected_data[$table_name], 'Make sure fetching views data by table works as expected.');
......@@ -140,4 +144,32 @@ public function testViewsFetchFields() {
}
}
/**
* Tests the fetchBaseTables() method.
*/
public function testFetchBaseTables() {
// Enabled node module so there is more than 1 base table to test.
$this->enableModules(array('node'));
$data = $this->viewsDataCache->get();
$base_tables = $this->viewsDataCache->fetchBaseTables();
// Test the number of tables returned and their order.
$this->assertEqual(count($base_tables), 3, 'The correct amount of base tables were returned.');
$this->assertIdentical(array_keys($base_tables), array('node', 'node_revision', 'views_test_data'), 'The tables are sorted as expected.');
// Test the values returned for each base table.
$defaults = array(
'title' => '',
'help' => '',
'weight' => 0,
);
foreach ($base_tables as $base_table => $info) {
// Merge in default values as in fetchBaseTables().
$expected = $data[$base_table]['table']['base'] += $defaults;
foreach ($defaults as $key => $default) {
$this->assertEqual($info[$key], $expected[$key]);
}
}
}
}
......@@ -203,6 +203,43 @@ protected function processEntityTypes(array &$data) {
}
}
/**
* Fetches a list of all base tables available.
*
* @return array
* An array of base table data keyed by table name. Each item contains the
* following keys:
* - title: The title label for the base table.
* - help: The help text for the base table.
* - weight: The weight of the base table.
*/
public function fetchBaseTables() {
$tables = array();
foreach ($this->get() as $table => $info) {
if (!empty($info['table']['base'])) {
$tables[$table] = array(
'title' => $info['table']['base']['title'],
'help' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
);
}
}
// Sorts by the 'weight' and then by 'title' element.
uasort($tables, function ($a, $b) {
if ($a['weight'] != $b['weight']) {
return $a['weight'] < $b['weight'] ? -1 : 1;
}
if ($a['title'] != $b['title']) {
return $a['title'] < $b['title'] ? -1 : 1;
}
return 0;
});
return $tables;
}
/**
* Destructs the ViewDataCache object.
*/
......
......@@ -944,59 +944,6 @@ function views_get_handler($table, $field, $type, $override = NULL) {
return $manager->createInstance('broken');
}
/**
* Fetch a list of all base tables available
*
* @return array
* A keyed array of in the form of 'base_table' => 'Description'.
*/
function views_fetch_base_tables() {
static $base_tables = array();
if (empty($base_tables)) {
$tables = array();
$data = drupal_container()->get('views.views_data')->get();
foreach ($data as $table => $info) {
if (!empty($info['table']['base'])) {
$tables[$table] = array(
'title' => $info['table']['base']['title'],
'description' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
);
}
}
uasort($tables, '_views_weight_sort');
$base_tables = $tables;
}
return $base_tables;
}
/**
* Sorts a structured array by the 'weight' and then by 'title' element.
*
* Callback for uasort() in views_fetch_base_tables().
*
* @param array $a
* First item for comparison. The compared items should be associative arrays
* that include a 'weight' and title element.
* @param array $b
* Second item for comparison.
*
* @return int
* -1 if the first item comes first, 1 if second and 0 if they have the same
* order.
*/
function _views_weight_sort($a, $b) {
if ($a['weight'] != $b['weight']) {
return $a['weight'] < $b['weight'] ? -1 : 1;
}
if ($a['title'] != $b['title']) {
return $a['title'] < $b['title'] ? -1 : 1;
}
return 0;
}
/**
* Fetch a list of all base tables available
*
......
......@@ -305,7 +305,7 @@ function views_ui_library_info() {
*/
function views_ui_edit_page_title(ViewUI $view) {
module_load_include('inc', 'views_ui', 'admin');
$bases = views_fetch_base_tables();
$bases = drupal_container()->get('views.views_data')->fetchBaseTables();
$name = $view->getHumanName();
if (isset($bases[$view->get('base_table')])) {
$name .= ' (' . $bases[$view->get('base_table')]['title'] . ')';
......
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