List

Updated on 20 October 2017

Unordered list

$items = [];
$items[] = 'one';
$items[] = 'two';
$build['my-list'] = array(
    '#theme' => 'item_list',
    '#items' => $items,
    '#type' => 'ul',
);

Custom attributes, here, classes on list items <li> and list <ul>

PHP Code Sniffer

Updated on 18 November 2017

Check Drupal coding standards

phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /path/to/check

Autofix

phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /path/to/check

 

Redirect user after login

Updated on 21 November 2017

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function [MODULENAME]_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Alter login form and add own custom submit handler.
  $form['#submit'][] = '_[MODULENAME]_user_login_form_submit';
}

/**
 * Custom submit handler for login form.
 */
function _[MODULENAME]_user_login_form_submit($form, FormStateInterface $form_state) {
  // Set redirect to login form.
  $form_state->setRedirect('custom.redirect_callback');
}

Render an Image

Updated on 20 October 2017

$file = File::load(1);
$variables = array(
  'style_name' => 'thumbnail',
  'uri' => $file->getFileUri(),
);

// The image.factory service will check if our image is valid.
$image = \Drupal::service('image.factory')->get($file->getFileUri());
if ($image->isValid()) {
  $variables['width'] = $image->getWidth();
  $variables['height'] = $image->getHeight();
}
else {
  $variables['width'] = $variables['height'] = NULL;
}

$logo_render_array = [
  '#theme' => 'image_style',
  '#width' => $variables['width'],
  '#height' => $variable