I receive this error message when I try my custom form in Drupal 8.
User error: “0” is an invalid render array key in Drupal\Core\Render\Element::children()
This is the code I am using
namespace Drupal\drupalup_simple_form\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\HtmlCommand; /** * Our simple form class. */ class SimpleForm extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'drupalup_simple_form'; } /** * {@inheritdoc} */ public function buildForm(array $ form, FormStateInterface $ form_state) { $ form['search'] = array( '#type' => 'textfield', '#title' => t('Search:'), ); $ form['submit'] = array( '#type' => 'submit', '#value' => $ this->t('Submit'), '#button_type' => 'primary', '#ajax' => array( 'callback' => [$ this, 'submitForm'], 'event' => 'click', ), ); return $ form; } /** * {@inheritdoc} */ public function submitForm(array &$ form, FormStateInterface $ form_state) { $ url = 'http://example.se/api/json/1.0/searchProduct.json?query='; $ term = $ form_state->getValue('search'); $ query_url = $ url. $ term; $ client = \Drupal::httpClient(); $ request = $ client->get($ query_url); $ data = json_decode($ request->getBody(), TRUE); $ results = []; if (!empty($ data) && !empty($ data['items'])) { foreach ($ data['items'] as $ item ) { $ results[] = $ item['name']; } } return $ results; } } ?>
The end result should be that all results from the URL with the [searchword] are shown when you have entered the search word in the textfield and pressed submit. Does anyone know how to solve this?