I've been looking at many sites, answer the following situation:

Case
I need to connect Drupal Views with some external databases and these databases have the same tables.

Situation
I created one module for each external database via hook_views_data and all is ok while enabling one at a time.

Problem
I think the structure of $data overwrite each table for the different databases. See below

//MODULE FOR DATABASE1 (database1_conection)
function database1_connection_views_data {
  $data['example_table']['table']['group'] = t('Example table');
  $data['example_table']['table']['base'] = array(
    'field' => 'id',
    //...
    'database' => 'database1';
  );
  $data['example_table']['some_field'] = array(
    'title' => t('Example content'),
    //...
  );
  //...
}

//MODULE FOR DATABASE2 (database2_conection)
function database2_connection_views_data {
  $data['example_table']['table']['group'] = t('Example table');
  $data['example_table']['table']['base'] = array(
    'field' => 'id',
    //...
    'database' => 'database2';
  );
  $data['example_table']['some_field'] = array(
    'title' => t('Example content'),
    //...
  );
  //...
}

Possible Solutions (Obviously I'm considering these options but I do not know how can I do this yet. Or even if they are not feasible)

1. Create alias for tables (I did a test with field aliases but is not enough)
2. Create only one $data for the default external database and alter the hook_views_data dynamically and change 'database' parameter for each table
3. Create a view and then alter 'database' before make a query
4. Use an argument (contextual parameter) in a view and insert a snippet in a php code.
5. Or something easier that I do not know yet

Someone can help me?

CommentFileSizeAuthor
#1 issue.png19.68 KBdelbozkester

Comments

delbozkester’s picture

StatusFileSize
new19.68 KB

When I try to create a view with more than two modules enabled I get the following error:

Creating a view
creating view

Error

Fatal error: Function name must be a string in ...\sites\all\modules\views\includes\admin.inc on line 697

// admin.inc FILE
// ...
function views_ui_wizard_form_validate($form, &$form_state) {
  $wizard = views_ui_get_wizard($form_state['values']['show']['wizard_key']);
  $form_state['wizard'] = $wizard;
  $get_instance = $wizard['get_instance'];
  $form_state['wizard_instance'] = $get_instance($wizard); // ------------ LINE 697 -------------//
  $errors = $form_state['wizard_instance']->validate($form, $form_state);
  foreach ($errors as $name => $message) {
    form_set_error($name, $message);
  }
}
//...
jbrauer’s picture

This seems to be related to the optgroup that is created in the wizard.

For most items in the list $form_state['values']['show']['wizard_key'] is the string name of the entity.

But if as in this case there's an optgroup in the menu the label for the optgroup is the string and the values for the items are 0, 1, 2 etc.

Without having looked I wonder if this select list should be using optgroups.

merlinofchaos’s picture

Status: Active » Fixed

Comment #1 is unfortunately unrelated to the original post.

For comment #1, only one implementation of hook_views_data is allowed to add data to a table. Any other modules implementing it must use hook_views_data_alter() to add their data to the table. See, for example, user_views_data_alter().

merlinofchaos’s picture

Actually, I maybe wrong.

#1 *might* be caused by two hook_views_data() attempting to declare the same base table, and array_merge_recursive chokes on it. It looks like there is some kind of mixup between Frutas and Color there, causing a matrix which shouldn't exist.

Mołot’s picture

Automatically closed -- issue fixed for 2 weeks with no activity.