How to create custom exposed filters with external database

Created on 8 November 2023, about 1 year ago
Updated 23 February 2024, 11 months ago

I have configured External entity types, with a plan to is to build extra relationship between date database for custom exposed filter: 'user__field_school_roll_select.field_school_roll_select_value and external 'table' => 'schools', in "schools" database, 'field' => 'roll'

and implement for the query.

public function query() {
      if (!empty($this->value)) {
        $configuration = [
          'table' => 'user__field_school_roll_select',
          'field' => 'entity_id',
          'left_table' => 'users_field_data',
          'left_field' => 'uid',
          'operator' => '=',
        ];
        $join = Views::pluginManager('join')->createInstance('standard', $configuration);
        $this->query->addRelationship('user__field_school_roll_select', $join, 'users_field_data');

My idea for using this module was to have the ability to use External Entity Types to treat an external table as its own for the purpose of modifying this query. Of course, if it's possible at all. Your feedback is welcome.

From the second hand maybe there is no need to use the module at all for just for this purpose, but I didn't know of anything else.

πŸ’¬ Support request
Status

Closed: won't fix

Version

1.0

Component

Miscellaneous

Created by

πŸ‡΅πŸ‡±Poland adpo

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @adpo
  • πŸ‡«πŸ‡·France guignonv

    Hi! First, I don't really understand what you are trying to achieve. :-s I'm missing some context or details.
    ...but I'll try to provide an answer from what I got. :)

    First, you may just need xnttviews β†’ module. If you didn't try it, it may just do what you need. And, by the way, if you'd like to contribute to that module, your help would be appreciated.

    If you're trying to join external database tables using views feature, I doubt it's possible or simple. When you create an external entity, regardless of what's behind (ie. a REST service, an xnttdb, an xnttfiles, etc.), it's considered by Drupal as a content entity. As far as I got it, views module was initially designed to work with SQL content entity (a sub-class of content entities) but it has been modified to be able to handle other types of content entities through plugins. It may be what you are talking about and trying to do...
    On one hand, you would need to know that the external entity uses the xnttdb plugin when you edit your view (while the storage plugin can be changed after the view is created, leading to errors) and on the other hand, the problem is that I don't see any easy way to modify an xnttdb SQL query to add joins. The "joins" must be made another way, like if you are trying to "join" 2 types of entities that are not SQL-based, using PHP code instead of SQL statements (which would mean that your issue is not xnttdb-related).

    Again, I don't really understand what you want to do but maybe, you just need to use entity reference fields? If you don't want to create an xnttdb that has the JOINs in your SQL query, then you could create 2 (or more) external entity types that use xnttdb queries: one for your main table and one for the "joined" table separately. You would have an xntt "A" and an xntt "B". Then, you would add an entity reference field on your xntt "A" that references one or more xntt "B" and you would get the identifiers from the xntt "A" SQL query. You can give me an example of 2 SQL tables and I could develop my example on those. Are you using PostgreSQL by the way?

    Another possible approach is to use xnttmulti β†’ module. You would have 2 xnttdb sources and the second one would use a join on the given field from the first one. But it means the xntt would always have the joined table and it would be a 1-to-1 relationship which may not be the behavior you're looking for.

  • Status changed to Postponed: needs info about 1 year ago
  • πŸ‡΅πŸ‡±Poland adpo

    Hi! Thank you so much for mentioning xnttviewsβ€”it will certainly help maintain the external entity.

    Regarding the initial question, let me try to explain with an example. Each user entity has a school_id field, and in the external database (also connected), there is a column that contains school_id as well as a representative column (rep_name). To filter users by rep_name, I would need to create a third join. However, I assume, as you mentioned, there is no way to use an xnttdb SQL query to add joins.

    Thanks for the prompt response and the detailed answer.

  • Status changed to Closed: won't fix 11 months ago
Production build 0.71.5 2024