I'm new to Drupal 8 and I'm trying to create a form that allow to fetch my content types hierarchy and show its fields using ajax callback, but unfortunately none of the fields shows up eventhough the ajax works fine and callback function correctly fired? The Fields Types in my ContentType is {body:text_with_summary, price:decimal, title:string} As I Tried with debugging it seems that my code cannot define those types as form field types
here is my code in MyForm.php File:
public function level3SelectedCallback(array &$form, FormStateInterface $form_state) {
$ajax_response = new AjaxResponse();
$fields = MyController::contentTypeFields('MY_VOC');
foreach ($fields as $field){
$name = $field->id();
$label = $field->label();
$type = $field->getType();
$desc = $field->getDescription();
$form['fields_wrapper']['field_'.$label] = array(
'#type' => $type,
'#description' => $desc,
//'#options' => '',
'#attributes' => array(
'class' => array(
'myField'
),
),
);
$ajax_response->addCommand(new HtmlCommand('#fields-wrapper', $form['fields_wrapper']['field_'.$label]));
}
$ajax_response->addCommand(new InvokeCommand('#fields-wrapper', 'change'));
$form_state->setRebuild(True);
return $ajax_response;
}
My Controller Function is:
static function contentTypeFieldsAsFields($contentType) {
// Get the definitions
$fields = array_filter(
\Drupal::service('entity.manager')->getFieldDefinitions('node', $contentType), function ($field_definition) {
return $field_definition instanceof \Drupal\field\Entity\FieldConfig;
}
);
return $fields;
}