Check content type of node in preprocess hook I have a series of variables I want to make available on twig templates for specific content templates.

In MYTHEME.theme I have:

function MYTHEME_preprocess_node(&amp;$variables) {

  if (isset($variables['node'])) {

    $mycustomblock = \Drupal::service('plugin.manager.block')
      -&gt;createInstance('myblock', []);

    $variables['mycustomblock'] = $mycustomblock-&gt;build();

    $headertext = \Drupal::service('plugin.manager.block')
      -&gt;createInstance('headertext',
        [
          'text-align-submit'  =&gt; 'right',
          'uppercase-submit'   =&gt; TRUE,
          'header_size'        =&gt; 'h4',
          'header-size-submit' =&gt; 'h4',
          'grid-size-submit'   =&gt; 6,
          'header-title'       =&gt; 'This is a test',
        ]
      );

    $variables['headertext'] = $headertext-&gt;build();
  }
}


This allows me to use {{ headertext }} and {{ mycustomblock }} in content type templates like node--contenttype1.html.twig and node--contenttype2.html.twig. But I'd rather not build the 'headertext' and 'mycustomblock' variables for every single node type when I only need each one for one content type. It'd be nice to have a switch or if statement that checks which content type it is before generating a variable with a block.

Is there a way that I can check the content type of the node?

I've tried 

$type = $variables['node']-&gt;type;


But that did not work.
To get the content type of a node, you just use $variables['node']-&gt;getType() or $variables['node']-&gt;bundle(). The first method just calls the second, so calling one or the other one doesn't make much difference. 

public function getType() {
  return $this-&gt;bundle();
}


The only difference is that bundle() is defined for every entity, since the concept of bundle is common for all the entities, while content type is the property Drupal used for nodes before changing them in one of the entities Drupal core modules implement; using bundle() makes easier to adapt the code for other entity types, but I don't think it is the case for the code you are using.