2

I have made 2 db's one is of Drupal 8 and another is external, that is non-SQL(CouchDB) and to manage this DB I made an API with different endpoint(URLs) witch I can axes them by GET/POST methods. After correct request I get response in JSON from my API.

When I create for example a user, content(node) or something else I save some info in my Drupal database and some info in my API db (I made alteration of the forms that create content by hook_form_BASE_FORM_ID_alter and this way I create content in my external DB).

My question is how to Display a data from a views, combining information form two Data-Bases?

1

Maybe you can post some more details, but from what I understand you are storing additional information per node on an external database. You already have developed an API system for communicating with that database, so you don't really need to issue queries from Drupal directly to that database. In that case, one way to proceed this would be the following.

Configure Views to use a display mode to render your entity. On your node preprocess function, issue the API request and fetch the additional data from the 2nd database. Prepare the data and make them available to the template.

/**                                                                                                                                                                                                                                          
 * Implements hook_preprocess_node(&$variables).                                                                                                                                                                                         
 */
function MY_THEME_preprocess_node(&$variables) {
  // Issue API request.
  // Process JSON response.
  $variables['additional_field'] = $request_result;
}

You can then override the template for that entity (node) and display mode to render your additional fields.

If you need Views to use field display instead of rendering a display mode, you will need to develop a custom field handler that fetches the data from the external API. Let us know if you need help with that.

An alternative would be to use Views hooks to prepare the data in a similar way earlier in the process, but there's not really much of an advantage with that approach. That is, unless you have the capability to fetch all data for all results in a single API request. That would be the most efficient so that you don't issue multiple requests, one per node result. Let us know if this the case and if you need help with that.

| improve this answer | |
2
+50

If all you need to do is display data from the API, then you don't need to do anything special within views. Just select that you'd like to view rendered entities of your users with a specific view mode.

enter image description here

In the preprocess function for that view mode, you can hit the API and load the additional data that you need. Data loaded within a preprocess function should cache, so it shouldn't hit the API on subsequent page loads.

function mymodule_preprocess_user(&$variables) {
  /** @var $account \Drupal\user\UserInterface */
  $account = $variables['elements']['#user'];

  // custom code to hit the 3rd party API
| improve this answer | |
1

There are modules like Views Database Connector, External Entities in combination with Search API for views integration or Web Service Data - dunno about it's views integration. Remote Entity API is not yet ported to drupal 8. See: Drupal entity from different database

Ideas for custom coding are: Views Query Backend or Search API (or write your own search api backend - Maybe via solr)

In either way think of caching. As making request to your API in every request to your site is very expensive.

| improve this answer | |
1

First caveat. It is difficult to query across databases. Views by default does sql queries. Any kind of query that requires coalescing the data from both is 2 custom queries and a merge in PHP. This is not something that views is going to help you with.

There are a lot of approaches that will help you with this. I'd tackle it by building JSON data sources and using AngularJS to present the data.

2x sql sources. 2 sql data sources can be mapped as per drupal 7. Use the query interfaces and massage the data. Return as JSON.

The question leads to answers that map database tables to views, and there are tools to do this. I don't think you will get a views 'experience' form separate databases.

| improve this answer | |

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.