Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Plugin/Field/FieldWidget/LocalistUrlWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ protected function getPlaces(?string $default_value = NULL): array {
/**
* Get the data from the localist API.
*/
protected function getApiData() {
public function getApiData($base_url = NULL) {
// Data was already fetched.
if ($this->apiData) {
return $this->apiData;
}

$base_url = $this->getSetting('base_url');
$base_url = $base_url ?? $this->getSetting('base_url');

// Check for some cached data before we fetch it all again.
if ($cache = $this->cache->get("localist_api:$base_url")) {
Expand All @@ -363,14 +363,15 @@ protected function getApiData() {
}

try {
$this->fetchApiData();
$this->fetchApiData($base_url);
}
catch (\Throwable $e) {
if (!$this->apiData) {
throw $e;
}
}

return $this->apiData;
}

/**
Expand All @@ -379,8 +380,7 @@ protected function getApiData() {
* @return array
* Keyed array of api data.
*/
protected function fetchApiData(): array {
$base_url = $this->getSetting('base_url');
protected function fetchApiData($base_url): array {
$options = [
'timeout' => 5,
'base_uri' => $base_url,
Expand All @@ -402,7 +402,7 @@ protected function fetchApiData(): array {
continue;
}

$this->apiData[$key] = $this->fetchPagedApiData($key, $response['page']['total']);
$this->apiData[$key] = $this->fetchPagedApiData($base_url, $key, $response['page']['total']);
}

$this->cache->set("localist_api:$base_url", [
Expand All @@ -424,8 +424,7 @@ protected function fetchApiData(): array {
* @return array
* Indexed array of api data.
*/
protected function fetchPagedApiData($endpoint, $total_count): array {
$base_url = $this->getSetting('base_url');
protected function fetchPagedApiData($base_url, $endpoint, $total_count): array {
$options = [
'timeout' => 5,
'base_uri' => $base_url,
Expand Down