In Drupal 8 you use the FormBuilder service to retrieve forms.

1
$form = \Drupal::formBuilder()->getForm('Drupal\search\Form\SearchBlockForm');

The argument passed to the getForm() method is the name of the class that defines your form and is an implementation of \Drupal\Core\Form\FormBuilderInterface. If you need to pass any additional parameters to the form, pass them on after the class name.

1
$form = \Drupal::formBuilder()->getForm('Drupal\search\Form\SearchBlockForm', $foo);

How to check if a module is enabled in Drupal 8:

1
\Drupal::moduleHandler()->moduleExists('search')

In Drupal 7, you could use the array('html' => TRUE) parameter to make an HTML link. Here’s how to do it in Twig.

1
{{ link(title, url.setOption('html', true)) }}

Note that url should be an instance of \Drupal\Core\Url.

1
2
3
// Format and add main menu to theme.
$menu = \Drupal::menuTree()->load('main', new MenuTreeParameters());
$variables['main_menu'] = \Drupal::menuTree()->build($menu);

Looks like dpm() is broken right now in devel. It gives you a WSOD.

Here’s some alternatives with the devel module enabled:

1. Krumo

1
krumo($variables);

2. Kint

Kint is a great alternative to Krumo.

1
kint($variables);

2. Dump for Twig

Inside Twig template you can:

1
{{ dump() }}

In Drupal 7, you would use the user_access function to check for user access. Here’s how to do it in Drupal 8.

How to check for permission in Drupal 8
1
2
3
4
5
// Get the current user
$user = \Drupal::currentUser();

// Check for permission
$user->hasPermission('access administration menu');

In your THEMENAME.theme file, add the following code:

Implements hook_preprocess_menu()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * Implements hook_preprocess_menu().
 */
function THEMENAME_preprocess_menu(&$variables, $hook) {
  if ($hook == 'menu__main') { // We're doing that for main menu.
    // Get the current path.
    $current_path = \Drupal::request()->getRequestUri();
    $items = $variables['items'];
    foreach ($items as $key => $item) {
      // If path is current_path, set active to li.
      if ($item['url']->toString() == $current_path) {
        // Add active link.
        $variables['items'][$key]['attributes']['class'] = 'active';
      }
    }
  }
}