0

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?

1
  • You can remove the ajax stuff on the submit button because FormBase calls submitForm automatically when you click a submit button. – user72672 May 24 '19 at 17:47
0

I think this error relates to using an ordinal array, rather than an associative array, for the $results variable.

I think you might be able to fix it by replacing this loop:

foreach ($data ['items'] as $item ) {
  $results[] = $item['name'];
}

With this one:

foreach ($data['items'] as $key => $item ) {
  $results[$key] = $item['name'];
}

Hope it helps.

0

The error message isn't helping you out much here. In an ideal world it would be more like this:

The value for element with key '0' is not an array, but also not NULL.

That would be more in-keeping with the logic that triggered the error in Element::children(), which is expecting each element whose key isn't prefixed with # to itself be a render array.

A valid render array can be formed with very little effort, changing your code to this should bring better luck:

foreach ($data['items'] as $key => $item ) {
  $results[] = ['#markup' => $item['name']];
}

See the Render arrays docs for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.