I created a custom field and this is its formElement()
method.
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$value = isset($items[$delta]->value) ? $items[$delta]->value : '';
$client = \Drupal::httpClient();
$request = $client->get('http://localhost:8083/regionilist');
$response = json_decode($request->getBody());
$element += [
'#type' => 'select',
'#title' => $this->t('Select element'),
'#options' => $response,
//'#options' => [
// 'Abruzzo' => $this->t('Abruzzo label'),
// 'Lazio' => $this->t('Lazio label'),
// 'Sicilia' => $this->t('Sicilia label'),
//],
'#suffix' => '<div class="custom-field-widget"></div>',
'#attributes' => ['class' => ['edit-custom-field-widget']],
'#attached' => [
'library' => [
'custom_field/my_js_custom_field',
],
],
];
return ['value' => $element];
}
The Rest service output is this.
{
"Abruzzo": "Abruzzo label",
"Lazio": "Lazio label"
}
The output of json_decode()
is the following one.
array (
'Abruzzo' => 'Abruzzo label',
'Lazio' => 'Lazio label',
)
When I add the field to a content type I get the following error.
TypeError: Argument 1 passed to Drupal\Core\Form\OptGroup::flattenOptions() must be of the type array, object given, called in C:\xampp\htdocs\drupal8DPC\core\lib\Drupal\Core\Form\FormValidator.php on line 340 in Drupal\Core\Form\OptGroup::flattenOptions() (line 23 of core\lib\Drupal\Core\Form\OptGroup.php).
Drupal\Core\Form\OptGroup::flattenOptions(Object) (Line: 340)
Drupal\Core\Form\FormValidator->performRequiredValidation(Array, Object) (Line: 247)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'field_config_edit_form') (Line: 119)
Drupal\Core\Form\FormValidator->validateForm('field_config_edit_form', Array, Object) (Line: 571)
Drupal\Core\Form\FormBuilder->processForm('field_config_edit_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('field_config_edit_form', Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object) call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 38)
Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 664)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Error does not occur when I use the fixed code (commented in the code above) instead of the REST service.