Skip to content
Snippets Groups Projects
Commit 5ba8e8ef authored by Marco Primitivo's avatar Marco Primitivo Committed by Joseph Olstad
Browse files

Issue #3115789 by siddhant.bhosale, naresh_bavaskar, dca123, Bladedu,...

Issue #3115789 by siddhant.bhosale, naresh_bavaskar, dca123, Bladedu, joseph.olstad: Drupal 9 Compatibility
parent 5c61e897
No related branches found
Tags 8.x-1.6
No related merge requests found
......@@ -154,10 +154,8 @@ class AddViewsCustomTable extends FormBase {
}
}
$int_types = ['tinyint', 'smallint', 'mediumint', 'int', 'bigint'];
$text_query = new FormattableMarkup('DESCRIBE @table_name', [
'@table_name' => $table_name,
]);
$connection = Database::getConnection('default', $databaseName);
$text_query = 'DESCRIBE ' . $connection->escapeTable($table_name);
$query = $connection->query($text_query);
foreach ($query as $row) {
$row_type = explode('(', $row->Type);
......@@ -259,12 +257,12 @@ class AddViewsCustomTable extends FormBase {
->set($table_name . '.created_by', $user->id());
$result = $this->configEditable->save();
if ($result) {
drupal_set_message($this->t('@table is added to views. Please clear cache to see changes.', [
$this->messenger()->addStatus($this->t('@table is added to views. Please clear cache to see changes.', [
'@table' => $table_name,
]));
}
else {
drupal_set_message($this->t('Could not add table to views, please check log messages for error.'), 'error');
$this->messenger()->addError($this->t('Could not add table to views, please check log messages for error.'));
}
}
$form_state->setRedirect('view_custom_table.customtable');
......
......@@ -100,10 +100,8 @@ class EditTableRelations extends FormBase {
}
}
$int_types = ['tinyint', 'smallint', 'mediumint', 'int', 'bigint'];
$text_query = new FormattableMarkup('DESCRIBE @table_name', [
'@table_name' => $table_name,
]);
$connection = Database::getConnection('default', $config[$table_name]['table_database']);
$text_query = 'DESCRIBE ' . $connection->escapeTable($table_name);
$query = $connection->query($text_query);
foreach ($query as $row) {
$row_type = explode('(', $row->Type);
......@@ -172,14 +170,14 @@ class EditTableRelations extends FormBase {
$this->configEditable->set($table_name . '.column_relations', $serialize_relations);
$result = $this->configEditable->save();
if ($result) {
drupal_set_message($this->t('@table relations are updated.', [
$this->messenger()->addStatus($this->t('@table relations are updated.', [
'@table' => $table_name,
]));
}
else {
drupal_set_message($this->t('Could not update @table data, please check log messages for error.', [
$this->messenger()->addError($this->t('Could not update @table data, please check log messages for error.', [
'@table' => $table_name,
]), 'error');
]));
}
$form_state->setRedirect('view_custom_table.customtable');
}
......
......@@ -127,14 +127,14 @@ class EditViewsCustomTable extends FormBase {
->set($table_name . '.description', $description);
$result = $this->configEditable->save();
if ($result) {
drupal_set_message($this->t('@table is updated.', [
$this->messenger()->addStatus($this->t('@table is updated.', [
'@table' => $table_name,
]));
}
else {
drupal_set_message($this->t('Could not update @table data, please check log messages for error.', [
$this->messenger()->addError($this->t('Could not update @table data, please check log messages for error.', [
'@table' => $table_name,
]), 'error');
]));
}
$form_state->setRedirect('view_custom_table.customtable');
}
......
......@@ -3,6 +3,7 @@ name: Views Custom Table
description: 'Extends views to use custom table data.'
package: Views
core: 8.x
core_version_requirement: ^8 || ^9
dependencies:
- drupal:views
configure: view_custom_table.customtable
......@@ -73,7 +73,9 @@ function view_custom_table_views_data() {
$data[$table_name]['table']['group'] = t('Custom Table Views');
$table = view_custom_table_load_table_structure($table_name, $custom_table['table_database']);
foreach ($table as $column) {
$table_listed = [];
foreach ((array) $table as $column) {
$column_display_name = ucfirst($column->Field);
$column_display_name = str_replace('_', ' ', $column_display_name);
......@@ -85,7 +87,9 @@ function view_custom_table_views_data() {
$column_type = $column->Type;
}
if ($column->Key == 'PRI') {
if (!(\array_key_exists($table_name, $table_listed))) {
$table_listed[$table_name] = TRUE;
$data[$table_name]['table']['base'] = [
'field' => $column->Field,
'title' => t('@table data', [
......@@ -170,7 +174,7 @@ function view_custom_table_views_data() {
$entity_info = \Drupal::entityTypeManager()->getDefinition($table_relations[$column->Field]);
$entity_keys = $entity_info->getKeys();
$data[$table_name][$column->Field]['relationship'] = [
'base' => $entity_info->getDataTable(),
'base' => $entity_info->getBaseTable(),
'id' => 'standard',
'base field' => $entity_keys['id'],
'label' => t('@column_name', [
......@@ -184,11 +188,24 @@ function view_custom_table_views_data() {
'@entity' => $entity_info->getBaseTable(),
]),
];
$data[$entity_info->getBaseTable()][$table_name] = [
'title' => t('Reverse reference to @custom_table', ['@custom_table' => $custom_table['description'] ?? $custom_table['table_name']]),
'help' => t('Reference each custom table entry back from table @custom_table', ['@custom_table' => $custom_table['table_name']]),
'relationship' => [
'id' => 'entity_reverse',
'field_name' => $table_name,
'field table' => $table_name,
'field field' => $column->Field,
'base' => $table_name,
'base field' => $column->Field,
'label' => $custom_table['description'] ?? $custom_table['table_name'],
],
];
}
else {
$related_table_info = $all_custom_tables[$table_relations[$column->Field]];
$related_table = view_custom_table_load_table_structure($related_table_info['table_name'], $related_table_info['table_database']);
foreach ($related_table as $related_table_column) {
foreach ((array) $related_table as $related_table_column) {
if ($related_table_column->Key == 'PRI') {
$related_table_pri_key = $related_table_column->Field;
}
......@@ -215,33 +232,30 @@ function view_custom_table_views_data() {
}
}
}
return isset($data) ? $data : [];
return $data ?? [];
}
/**
* Function to get table structure information.
*
* @param null $table_name
* @param string $table_name
* Name of the table.
* @param string $database
* Name of database to connect to.
*
* @return bool
* Return table info or FALSE.
* @return array|null
* Return table info or NULL.
*/
function view_custom_table_load_table_structure($table_name = NULL, $database = 'default') {
$connection = Database::getConnection('default', $database);
if ($table_name != NULL) {
$query = new FormattableMarkup('DESCRIBE @table_name', [
'@table_name' => $table_name,
]);
$result = $connection->query($query);
if ($result) {
$table_info = $result->fetchAll();
}
if ($table_name === NULL) {
return NULL;
}
else {
return FALSE;
$connection = Database::getConnection('default', $database);
$query = 'DESCRIBE ' . $connection->escapeTable($table_name);
if ($result = $connection->query($query)) {
$table_info = $result->fetchAll();
}
return isset($table_info) ? $table_info : FALSE;
return $table_info ?? NULL;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment