If Content Type Equals, then I'm trying to get run some code if the content type of the current page is equal to events. I am using Drupal 8.

However, I'm unable to write the if statement.

I've tried:

if ($node-&gt;type == 'events'){...}
if (Node::getType == 'events'){...}


I also saw this question, but I have no idea how to apply it.

Full code:

// Add og:event:start_date meta tag

function MY_THEME_preprocess_html(&amp;$variables) {
  if ($node-&gt;getType() == 'events'){
    $time = [
      '#tag' =&gt; 'meta',
      '#attributes' =&gt; [
        'http-equiv' =&gt; 'og:event:start_date',
        'content' =&gt;  $node-&gt;field_next_time-&gt;value,
      ],
    ];

    $variables['page']['#attached']['html_head'][] = [$time, 'og:event:start_date'];  
  }

}
You need to pull the node object out of the variables array.

$node = $variables['node'];

In certain preprocess functions this may not be available, so you can try the request service:

$node = \Drupal::request()-&gt;get('node');

The object must be of type Node. Therefore:

$node-&gt;getType() == 'events'

As Berdir mentioned, you should check and ensure that $node is a valid Node object to see if it is an instance of NodeInterface before operating on it. This will prevent fatal errors in the event that $variables['node'] is actually a node and not some ad-hoc variable created in code from which you can't call methods on:

Example 1 (using $variables array):

if (isset($variables['node']) &amp;&amp; $variables['node'] instanceof NodeInterface) {
  $node = $variables['node'];
  $title = $node-&gt;getTitle();
  $type = $node-&gt;getType();
  // more code...
}


Example 2 (using the request):

$node = \Drupal::request()-&gt;get('node');

if (isset($node) &amp;&amp; $node instanceof NodeInterface) {
  $title = $node-&gt;getTitle();
  $type = $node-&gt;getType();
  // more code...
}