Hello,

I have created on a source site a vocabulary with ~50000 terms. I need to use theses terms on a second website. 

How can I "share" or "use" the vocabulary of the first website on the second Drupal ? This vocabulary has a lot of terms then I don't want use a module to sync or import the terms (entity share, feeds...) in the second site (remote site).

I see that we can add a second database in the settings.php and we can create a connector with 

\Drupal\Core\Database\Database::setActiveConnection('external');

I think this feature is efficiant to load / get datas from another site (without submodule).

How to display / retrieve the vocabulary inside the taxonomy of the remote site? Is there a possibility to achieve this? If yes, can you explain me how to proceed? Can you describe the steps to use/get the vocabulary in my second site?

Thanks for your advices and replies to understand this mecanism.

Comments

muhammad.tanweer’s picture

From what I have found,  you can add connection string to multiple databases in your settings file, but you can use one database at a time. Meaning, you can use one database for some parts of the website and another database for other parts (modules). But you can't use both simultaneously so I dont think its possible to do what you are trying to do, which is, connect to both databases, and try copy the terms from one to the other.

If you don't want to use feeds or any other export modules, then you can directly export only the taxonomy related database tables from the source website, and import them over to the target website database. Obviously, you will have to drop the existing similar tables from target website database.

kumkum29’s picture

Hello,

Thanks for your reply. I don't see how to solve this problem. In the first databse I have +50.000 terms. I don't want to synchronise terms from a databse to another database.

In this post (https://www.drupal8.ovh/en/tutoriels/285/use-multiple-databases-on-drupal-8) we see that we can connect multiple database/tables to a D8 site. Is it a possible solution ? A secur solution ?

What is your opinion of this?

Thanks. 

muhammad.tanweer’s picture

The type of work done on that link is to progressively copy over contents from tables of one db to another db. I have not used this type of a thing so can't comment on whether is workable and secure or not.

In your case, you need a one time copy of all the terms form one db to another. I still suggest you use a custom piece of code which will take the data from one table and copy to another. Meaning you first copy all the taxonomy related tables from source db and paste them into target db but with different naming (otherwise you will need to delete the already existing tables from target). Then, the script/module will take data from the source tables, and put it in destination tables, using the proper hooks of drupal for terms creation etc. I don't expect that piece of code/module to be any more than ~50 lines. I can help write that code if needed.

kumkum29’s picture

@muhammad.tanweer

the vocabulary is updated several times by month. I use Feeds module on the first site to update terms (and add new terms). So, launch manually a custom code isn't a good solution for me. For the moment, I have configurated a shared database between the sites 'see below). I don't know if this method is a good process for D8...

mmjvb’s picture

Use the prefix to include the database name for the taxonomy tables. This way the source tables can be shared by multiple other sites.

Note that per table prefixes are deprecated. Considering the poor track record of Drupal, suggest to explore alternatives.

kumkum29’s picture

@mmjvb

I have tested your solution : share tables between the sites. At this time, this is the simpliest solution:

1- Create a third database to insert the shared taxonomy tables

2- Delete these tables in the database of first site & second site

3- Configure the settings.php of each site to call this third database:

<code>

$databases['default']['default'] = array (
  'database' => 'mydb',
  'username' => 'myusername',
  'password' => 'pass',
  'prefix' => array(
    'default' => '',
    'taxonomy_index' => 'shared_tables.',
    'taxonomy_term_data' => 'shared_tables.',
    'taxonomy_term_field_data' => 'shared_tables.',
    'taxonomy_term_hierarchy' => 'shared_tables.',
    'taxonomy_term__feeds_item' => 'shared_tables.',
  ),
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);
 

</code>

I have configurated the settings.php after have reading comments from default.settings.php file:

<code>

* You can also use a reference to a schema/database as a prefix. This may be
 * useful if your Drupal installation exists in a schema that is not the default
 * or you want to access several databases from the same code base at the same
 * time.
 * Example:
 * @code
 *   'prefix' => array(
 *     'default'   => 'main.',
 *     'users'     => 'shared.',
 *     'sessions'  => 'shared.',
 *     'role'      => 'shared.',
 *     'authmap'   => 'shared.',
 *   );
 * @endcode

</code>

4- Use Features module to duplicate the vocabulary configs to a site from another.

But, in many posts I see that shared tables isn't always a good solution. But at this moment, that is the unique solution I see for my case... and this method is explained in the default.settings.php...
On D8, it's strange that we can't more easily share vocabularies between Drupal...

Is it a very bad thing to use this method? A security problem?

What is yours opinions on this?

Thanks !