In this tuto, I'll show you how to display only first level in taxonomy level drupal 8.
Default select with 2 levels:
Example how to display only first level in taxonomy level drupal 8.
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id)
{
if ($form_id == 'node_article_edit_form') {
$options_category = $form['field_category']["widget"]['#options'];
foreach ($options_category as $key => $category) {
if (isset($category) && !is_object($category)) {
if (strpos($category, '-', 0) === 0 && $key != '_none') {
unset($options_category[$key]);
}
}
}
$form['field_category']["widget"]['#options'] = $options_category;
}
}
The result is like this: