Hello,
I am struggling with a component, if anyone has an idea I would be glad to hear it.
Basically, I have a front end form that allows users to query an external API. I want to store the query result in a class variable in order to be accessed later on in another form.
I can query and display the query result in my view but when launching the onSelect function $this->searchResults is empty and actually even page[‘searchResults’] is empty even if it was previously set.
Thank you for your help
namespace Your\Plugin\Components;
use Cms\Classes\ComponentBase;
use Illuminate\Support\Facades\Http;
class CompanySearch extends ComponentBase
{
public $searchResults = [];
public function componentDetails()
{
return [
'name' => 'Company Search',
'description' => 'Search for companies and prefill form with details'
];
}
public function onRun()
{
// Initialize the search results
$this->page['searchResults'] = $this->searchResults;
}
public function onSearch()
{
$query = post('query');
$response = Http::get('https://recherche-entreprises.api.gouv.fr/search', [
'q' => $query
]);
if ($response->successful()) {
$this->searchResults = $response->json();
} else {
$this->searchResults = [];
}
// Store search results
$this->page['searchResults'] = $this->searchResults;
}
public function onSelect()
{
// Access the search results
$searchResultsbis = $this->searchResults;
$this->page['searchResultsbis'] = $this->searchResults;
];
}
}