diff --git a/src/Query/GroupOption.php b/src/Query/GroupOption.php index f5fb70a..5afb03e 100644 --- a/src/Query/GroupOption.php +++ b/src/Query/GroupOption.php @@ -106,15 +106,18 @@ class GroupOption implements QueryOptionInterface, QueryOptionTreeItemInterface /** * {@inheritdoc} */ - public function apply($query) { + public function apply($query, $original_query = NULL) { + if (is_null($original_query)) { + $original_query = $query; + } switch ($this->conjunction) { case 'OR': - $group = $query->orConditionGroup(); + $group = $original_query->orConditionGroup(); break; case 'AND': default: - $group = $query->andConditionGroup(); + $group = $original_query->andConditionGroup(); break; } @@ -125,8 +128,8 @@ class GroupOption implements QueryOptionInterface, QueryOptionTreeItemInterface } if (!empty($this->childGroups)) { - $group = array_reduce($this->childGroups, function ($group, $child) { - return $child->apply($group); + $group = array_reduce($this->childGroups, function ($group, $child) use ($original_query) { + return $child->apply($group, $original_query); }, $group); } @@ -147,11 +150,16 @@ class GroupOption implements QueryOptionInterface, QueryOptionTreeItemInterface return TRUE; } - // If any child Groups or their children have the id return TRUE. - return array_reduce($this->childGroups, function ($has_child, $group) use ($id) { - // If we already know that we have the child, skip evaluation and return. - return $has_child || $group->id() == $id || $group->hasChild($id); - }, FALSE); + if (is_array($this->childGroups)) { + // If any child Groups or their children have the id return TRUE. + return array_reduce($this->childGroups, function ($has_child, $group) use ($id) { + // If we already know that we have the child, skip evaluation and return. + return $has_child || $group->id() == $id || $group->hasChild($id); + }, FALSE); + } + else { + return FALSE; + } } } diff --git a/tests/src/Functional/JsonApiFunctionalTest.php b/tests/src/Functional/JsonApiFunctionalTest.php index 6da6396..9a6a0e5 100644 --- a/tests/src/Functional/JsonApiFunctionalTest.php +++ b/tests/src/Functional/JsonApiFunctionalTest.php @@ -336,45 +336,45 @@ class JsonApiFunctionalTest extends JsonApiFunctionalTestBase { $this->assertGreaterThanOrEqual(OffsetPage::$maxSize, count($collection_output['data'])); // 5. Grouping grouped filters: Get nodes that are promoted or sticky and // created by admin. -// $filter = [ -// 'and-group' => [ -// 'group' => [ -// 'conjunction' => 'AND', -// ], -// ], -// 'or-group' => [ -// 'group' => [ -// 'conjunction' => 'OR', -// 'memberOf' => 'and-group', -// ], -// ], -// 'admin-filter' => [ -// 'condition' => [ -// 'path' => 'uid.name', -// 'value' => $this->user->getAccountName(), -// 'memberOf' => 'and-group', -// ], -// ], -// 'sticky-filter' => [ -// 'condition' => [ -// 'path' => 'sticky', -// 'value' => 1, -// 'memberOf' => 'or-group', -// ], -// ], -// 'promote-filter' => [ -// 'condition' => [ -// 'path' => 'promote', -// 'value' => 1, -// 'memberOf' => 'or-group', -// ], -// ], -// ]; -// $collection_output = Json::decode($this->drupalGet('/jsonapi/node/article', [ -// 'query' => ['filter' => $filter], -// ])); -// $this->assertSession()->statusCodeEquals(200); -// $this->assertEquals(0, count($collection_output['data'])); + $filter = [ + 'and-group' => [ + 'group' => [ + 'conjunction' => 'AND', + ], + ], + 'or-group' => [ + 'group' => [ + 'conjunction' => 'OR', + 'memberOf' => 'and-group', + ], + ], + 'admin-filter' => [ + 'condition' => [ + 'path' => 'uid.name', + 'value' => $this->user->getAccountName(), + 'memberOf' => 'and-group', + ], + ], + 'sticky-filter' => [ + 'condition' => [ + 'path' => 'sticky', + 'value' => 1, + 'memberOf' => 'or-group', + ], + ], + 'promote-filter' => [ + 'condition' => [ + 'path' => 'promote', + 'value' => 0, + 'memberOf' => 'or-group', + ], + ], + ]; + $collection_output = Json::decode($this->drupalGet('/jsonapi/node/article', [ + 'query' => ['filter' => $filter], + ])); + $this->assertSession()->statusCodeEquals(200); + $this->assertEquals(0, count($collection_output['data'])); } /**