0

I love the features the view provides in Drupal. Sortable tables, Filters, as well as ability to use external modules like views bulk export. My problem is that I want to display a separate database (Call this X), using views. What would be the best way to go about it?

To get around this problem in Drupal 6, I created a content type with all fields that were in Database X, then I created view for that content type by frontend. After that I used hook_pre_render_view() to push data from X to the view. Right now I want to achieve similar results in Drupal 8. I could use this same approach but it doesn't feel very Drupally. I was wondering if there is a better way to achieve this.

0

You can connect a external database inorderto let Drupal Know about it:

$databases['external']['default'] = array(
  'database' => 'custom_database',
  'username' => 'root',
  'password' => '',
  'prefix' => '',
  'host' => php_sapi_name() == 'cli' ? '127.0.0.1' : 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

Then you can connect to that database:

/*
* Switch to external db
*/
\Drupal\Core\Database\Database::setActiveConnection('custom_database');

/*
* Get connection
*/
$db = \Drupal\Core\Database\Database::getConnection();

Then do you stuff and don't footgrt to switch again to the standard database:

/*
* Switch back
*/
\Drupal\Core\Database\Database::setActiveConnection();

You can store your external database info into a content type programatically and the show in views.

3
  • Thank you, I am done with these steps. My biggest issue is what will be the best way to store external database into content type programatically, or if there is a way to bypass creation of content type and directly use views – Aves2432 Apr 27 '20 at 20:37
  • Here is a simple example: code$data = [ 'type' => 'my_content_type', 'title' => 'My new title', 'uid' => 1, 'field_my_field' => 'my field awesome value' ]; $node = Drupal::entityTypeManager() ->getStorage('node') ->create($data); $node->save();code – Alvaro dM Apr 27 '20 at 21:27
  • Thank you I ended up following the same technique I used in Drupal 6 – Aves2432 Apr 29 '20 at 15:22
0

You can by using the Views Database Connector module.

Views Database Connector is a powerful module that gives Views full access to external database tables found in the settings for your Drupal installation. With this module, you can setup a view around any table in any database configuration. This can be extremely useful to pull external data from a database to show to your users in a view.

See README.txt on how to use.

1
  • Thank you, module looks promising. I will keep it in mind when dealing with database and views in future. Right now, I just ended up doing the same thing I did in drupal 6 – Aves2432 Apr 29 '20 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.