2

A website I maintain can't view revisions without receiving an unexpected error. The log file points to this code in the .theme file but I have no idea what it does. When I commented it out, the website seems to function fine and revisions are viewable.

function themename_preprocess_page(&$variables) {  
  $current_path = \Drupal::service('path.current')->getPath();
  $path_args = explode('/', $current_path);
  $node = \Drupal::routeMatch()->getParameter('node');
  if($node) {
    $node_info = \Drupal\node\Entity\Node::load($node->id());
    if($node_info->hasField('field_component')) {
      $paragraph = $node_info->field_component->getValue();
      if($paragraph) {
        foreach ( $paragraph as $element ) {
          $p = \Drupal\paragraphs\Entity\Paragraph::load( $element['target_id'] );
        }
        $variables['hero_banner'] = $p;
      }
    }
  }
}

The log particularly points to the 6th line above as the error. It mentions "Error: Call to a member function id()".

Does anyone have any clue? Thanks!

1 Answer 1

2

You need to add a check and also no need to $node_info = Node::load as the node is already loaded. The first two lines inside the function are not even getting used, so I removed them.

function themename_preprocess_page(&$variables) {  
  $node = \Drupal::routeMatch()->getParameter('node');
  if ($node instanceof \Drupal\node\NodeInterface) { // check it's a node
    if($node->hasField('field_component')) {
      $paragraph = $node->field_component->getValue();
      if($paragraph) {
        foreach ( $paragraph as $element ) {
          $p = \Drupal\paragraphs\Entity\Paragraph::load( $element['target_id'] );
        }
        $variables['hero_banner'] = $p;
      }
    }
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.