Skip to content
Snippets Groups Projects
Commit 6340839b authored by Felip Manyer i Ballester's avatar Felip Manyer i Ballester
Browse files

Fixed integration with VBO

parent 89b05513
Branches
Tags 2.0.1-alpha2
No related merge requests found
......@@ -73,6 +73,8 @@ class GbifOccurrence extends QueryPluginBase {
/**
* {@inheritdoc}
*
* We do not really use this, unless $field value is 'key'.
*/
public function addWhere($group, $field, $value = NULL, $operator = NULL) {
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all
......@@ -95,12 +97,23 @@ class GbifOccurrence extends QueryPluginBase {
* Builds the necessary info to execute the query.
*/
public function build(ViewExecutable $view) {
// Filters.
$view->build_info['query'] = [];
// Filters.
foreach ($view->filter as $filter) {
$view->build_info['query'] += $filter->generate();
}
// Where conditions (only for key).
if (isset($this->where[0]['conditions'])) {
foreach ($this->where[0]['conditions'] as $condition) {
$operators = ['=', 'IN'];
if ($condition['field'] == 'key' && in_array($condition['operator'], $operators)) {
$view->build_info['query']['key'] = $condition['value'];
}
}
}
// Pager.
$view->initPager();
if ($view->pager->usePager()) {
......@@ -118,7 +131,7 @@ class GbifOccurrence extends QueryPluginBase {
public function execute(ViewExecutable $view) {
$start = microtime(TRUE);
$index = 0;
$results = $this->occ->search($view->build_info['query']);
$results = $this->getResults($view);
foreach ($results['results'] as $data) {
$row = array_intersect_key($data, $view->field);
......@@ -133,4 +146,36 @@ class GbifOccurrence extends QueryPluginBase {
$view->execute_time = microtime(TRUE) - $start;
}
/**
* Get view results.
*
* @param \Drupal\views\ViewExecutable $view
* The view resuls will be retrieved for.
*
* @return array
* An array with two keys:
* - results contains an array of GBIF occurrences,
* - count the number of results.
*/
protected function getResults(ViewExecutable $view) {
$results = [];
if (isset($view->build_info['query']['key'])) {
// If key is specified, GBIF search API is not called as it does not allow
// to specify this as a parameter. Instead, we directly retrieve
// occurrences. This behaviour takes precedence.
$keys = $view->build_info['query']['key'];
$keys = is_array($keys) ? $keys : [$keys];
foreach ($keys as $key) {
$results['results'][] = $this->occ->get($key);
}
$results['count'] = count($keys);
}
else {
$results = $this->occ->search($view->build_info['query']);
}
return $results;
}
}
......@@ -36,11 +36,7 @@ class GbifVboViewsData {
* The current view object.
*/
public function getEntityFromRow(ResultRow $row, $relationship_id, ViewExecutable $view) {
// We do not really need a full-blown entity in this context. This would be
// rather inefficient, though we could count on cache.
return $this->entityTypeManager->getStorage('gbif_occurrence')->create([
'key' => $row->key,
]);
return $this->entityTypeManager->getStorage('gbif_occurrence')->load($row->key);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment