n show only first level in taxonomy level drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

In this tuto, I'll show you how to display only first level in taxonomy level drupal 8.

Default select with 2 levels:

render node title field with link to content in views unformatted twig template drupal 8

 

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:

render node title field with link to content in views unformatted twig template drupal 8

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook