I'm having an issue when adding "field_domain_access: Domain Record" existing in my content type to my View as a relationship. I'd like to be able to get the view to show all content of the current domain, but cannot get past the first step of adding the domain as a relationship. For some reason it tries to add the table to the query twice resulting in the following query:

SELECT node_field_data.nid AS nid
FROM 
{node_field_data} node_field_data
LEFT JOIN {node__field_domain_access} node__field_domain_access ON node_field_data.nid = node__field_domain_access.entity_id AND (node__field_domain_access.deleted = '0' AND node__field_domain_access.langcode = node_field_data.langcode)
LEFT JOIN {} _node__field_domain_access ON node__field_domain_access.field_domain_access_target_id = _node__field_domain_access.id
LEFT JOIN {node__field_active_} node__field_active_ ON node_field_data.nid = node__field_active_.entity_id AND (node__field_active_.deleted = '0' AND node__field_active_.langcode = node_field_data.langcode)
WHERE (node_field_data.status = '1') AND (node_field_data.type IN ('equipment')) AND (node__field_active_.field_active__value = '1')
ORDER BY node_field_data.created ASC

As expected, it throws an SQL error detailing it cannot find table '_node__field_domain_access' which, doesn't exist. Quite why the code is trying to add this table twice (the line above is perfect enough) but the second attempt breaks the query. The field has only been added the once to left join on - see attached image. I get this with and without config_views enabled.

Any help on this would be greatly appreciated - I'm still trying to get to grips with Drupal 8.

Comments

JennyDumitrescu created an issue. See original summary.

agentrickard’s picture

You don't need a relationship to do what you are trying to do. You need a Contextual Filter (or a regular filter).

Take a look at the Views that ship with Domain Content, which do the same thing.

The relationship wouldn't add anything to the view. Relationships are used to load data fields from a related table. However, domains are config entities, and therefore not stored in a database table, so no additional fields would be available to the view.

agentrickard’s picture

Note also that you generally have to disable SQL rewriting in the View, otherwise node access will automatically filter by the current domain. Again, see the configuration of Advanced views settings in Domain Content.

agentrickard’s picture

Status: Active » Needs review
StatusFileSize
new1.62 KB

I confirmed this issue. The relationship will always fail because the table does not exist. Since domains do not have a table, it looks like views is trying to join back to the base table definition.

The attached patch removes the relationship handler entirely.

jennydumitrescu’s picture

Thanks @agentrickard. That was a great help - it hasnt been very clear to find the change from the given "Current domain" in D7 to how to do that within D8 and with the above advice i've been able to find that.

jennydumitrescu’s picture

Status: Needs review » Reviewed & tested by the community

I've applied the patch in #4 and tested on the site where the original error occured - I can confirm that I can no longer see the Domain access record as a option when adding a relationship. Thanks.

agentrickard’s picture

Status: Reviewed & tested by the community » Fixed

Committed! Thank you!

Status: Fixed » Closed (fixed)

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