Posted by robertrackl on September 3, 2015 at 4:10am
Hi!
I am a Drupal learner but have quite a bit of other software and database experience, I would say semi-professional. Is there a way to use Views on an alternate database (i.e., not the Drupal one for the web site) without writing php code - or at least very little php code (since I have no php experience)?
Thanks,
r4
Comments
The Drupal Views module uses
The Drupal Views module uses the field APIs to figure out where to make the joins between tables. So no, you can't use it on an arbitrary database without writing a Views handler for that database's structure.
(removed duplicate of edited
(removed duplicate of edited comment)
I'm not sure about using a
I'm not sure about using a secondary database (I assume it's possible, but I don't know the details of how it works with Views). However, it's definitely possible to tell Views about your table structures, by writing a custom module that implements the
hook_views_data
hook and returns a structured array representing your database schema.This can be quite simple or rather complicated, depending on the complexity of fields and joins you need.
Look at the demo implementation of
hook_views_data
inviews.api.php
for an example (or any of the other implementations within themodules
subdirectory, or any other module with a function named something like*_views_data
).You might also check out the Views Schema module, which automates this process to some extent. There's also the Data module, which provides a point-and-click admin UI for the same purpose. However, I found that the Data module has some limitations, so if you are happy working with code you may be better off implementing the necessary hooks directly.
In some cases, it's necessary to also provide new Views handlers by implementing a new subclass. This is slightly more involved.
In addition to Views Schema,
In addition to Views Schema, there's also the similarly-named Schema Views. It looks like Schema Views is more static (you use it during the development process and paste the generated code into a file somewhere), where Views Schema works dynamically, inspecting the database at runtime.
More Complex than I Thought
Thank you all for your comments and explanations. Sounds like I need to patiently learn more and gain some experience. I had read a little about settings.php and had seen that one can specify an alternate database there, but then I did not know how to connect to it from Drupal in a simple way. You all are telling me that there is no simple way.
Views on Alternate Database
I talked about connecting Views to an alternate database at a SeaDUG meeting a few months back. It's not terribly hard, but it does require custom coding. If you search for how to do it, you'll probably find some good instructions that don't require a lot of PHP experience.