The sites module's "Sites" Views filter (Drupal\sites\Plugin\views\filter\Sites) exposes a "Canonical only" option for sites whose plugin implements \Drupal\sites\QueryableCanonicalSiteInterface. This restricts a content listing to entities for which the given site is the canonical/source site, rather than any site the entity happens to be available on (which is what the existing queryAlter() / QueryableSiteInterface does).

GroupSitesBase currently implements QueryableSiteInterface::queryAlter() but not QueryableCanonicalSiteInterface, so the "Canonical only" option is unavailable for sites_group-based sites.

We have a local, uncommitted patch for this (patches/sites_group/canonical-site-filter.patch) that adds the interface and a canonicalQueryAlter() implementation, mirroring queryAlter() but with an additional is_source_site = 1 condition on the group_relationship_field_data join. This issue is to land that properly as a patch/MR against sites_group instead of carrying it as a local-only patch.

Proposed change

class GroupSitesBase extends SiteWithContentBase implements GroupSiteInterface, QueryableCanonicalSiteInterface {

  // ...

  /**
   * {@inheritdoc}
   */
  public function canonicalQueryAlter(SelectInterface $query, EntityTypeInterface $entity_type) {
    if (!$plugin_ids = $this->sitesGroupService->getPluginIdsByEntityTypeId($entity_type->id())) {
      return;
    }

    $group_relationship_data_table = $this->entityTypeManager->getDefinition('group_relationship')->getDataTable();
    $data_table = $entity_type->getDataTable();
    $id_key = $entity_type->getKey('id');
    $group_id = $this->getGroup()->id();

    $query->join(
      $group_relationship_data_table,
      'gr_site',
      "$data_table.$id_key = %alias.entity_id AND %alias.plugin_id IN (:plugin_ids[]) AND %alias.gid = :gr_site_gid_" . $group_id . " AND %alias.is_source_site = 1",
      [
        ':plugin_ids[]' => $plugin_ids,
        ':gr_site_gid_' . $group_id => $group_id,
      ],
    );
  }
}

Prerequisite

https://git.erdfisch.de/contrib/sites/-/merge_requests/195

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hydra created an issue. See original summary.

hydra’s picture

Issue summary: View changes

hydra’s picture

Issue summary: View changes

  • hydra committed 30c78ae6 on 1.x
    feat: #3595527 QueryableCanonicalSiteInterface (canonical-only Views...
hydra’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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