Quick module tips

More familiar structure of node object

When viewing a node object in Kint, do the following to get a structure that's more like Drupal 7:

kint($node->toArray());

Remove title block for a certain page

Add the following to the .module file

/**
 * Implements hook_block_view_alter().
 */
function MODULE_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
  if ($block->getBaseId() === 'page_title_block') {
    // Remove the title block from the front page.
    $is_front = \Drupal::service('path.matcher')->isFrontPage();
    if ($is_front) {
      $build['#access'] = FALSE;
    }
  }
}

Create URLs

// Internal path (defined by a route in Drupal 8).
use Drupal\Core\Link;
use Drupal\Core\Url;
$internal_link = Link::fromTextAndUrl(t('Drupal nodes'), Url::fromUri('internal:/node', $options))->toString();

// External Url.
use Drupal\Core\Link;
use Drupal\Core\Url;
$external_link = Link::fromTextAndUrl(t('The Carney Effect'), Url::fromUri('http://www.thecarneyeffect.co.uk/'))->toString();

// Url with `options.
use Drupal\Core\Link;
use Drupal\Core\Url;
$options = array(
  'query'      => ['type' => 'article', 'status' => 1],
  'fragment'   => 'article-list',
  'attributes' => ['class' => ['btn', 'btn-mini']],
  'absolute'   => TRUE,
);
$link = Link::fromTextAndUrl(t('Drupal node articles'), Url::fromUri('internal:/node', $options))->toString();

// Render type Link
use Drupal\Core\Url;
$render_array['link'] = array(
  '#title' => $this->t('Link Text Goes Here!'),
  '#type' => 'link',
  '#url' => Url::fromRoute('entity.node.canonical', ['node' => 1]),
);

Check user has Role

use Drupal\Core\Session\AccountProxy;

$user_roles = $this->currentUser->getRoles();
if(in_array('role_to_check', $user_roles)) {
  ksm('true');
}

Disable access to known user pages

Disable access to pages such as 'user' and 'user/nid/view' etc. Add the following to the .module file

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_ENTITY_TYPE_access() for entity type "user".
 *
 * @param \Drupal\user\UserInterface $entity
 *   The user object to check access for.
 *
 * @return
 *   Permission.
 */
function MODULE_user_access(UserInterface $entity, $op, AccountInterface $account) {
  // Disable access to user pages.
  if ($entity->id() == $account->id()) {
    switch ($op) {
      case 'update':
      case 'view':
      case 'user_name':
      case 'user_mail':
      case 'user_pass':
      case 'user_edit':
      case 'user_delete':
        // Example of limiting access to known user pages by role.
        $user_roles = $account->getRoles();
        if (!in_array('super_admin', $user_roles)) {
          return AccessResult::forbidden();
        }
        break;
    }
  }
  // No opinion.
  return AccessResult::neutral();
}

 Helpful node methods

// Get node type.
$node_type = $node->getType();

// Get node title.
$title = $node->getTitle();

// Get node status.
$visible = $node->isPublished();

// Get node promoted status.
$node->setPromoted(TRUE);

// Get node uid.
$uid = $node->getOwnerId();

// Get node user account.
$author = $node->getOwner();