You are here

public static function WebformNodeElement::preRenderWebformNodeElement in Webform Node Element 8

Add the rendered node to the element.

Parameters

array $element: An associative array containing the properties of the element.

Return value

array The $element.

File

src/Element/WebformNodeElement.php, line 35

Class

WebformNodeElement
Provides a render element to display a node.

Namespace

Drupal\webform_node_element\Element

Code

public static function preRenderWebformNodeElement(array $element) {
  $element['#markup'] = "";
  $nid = $element['#webform_node_element_nid'];
  $element_id = $element['#webform_id'];

  // Allow event subscribers to set the nid and display mode.
  $dispatcher = \Drupal::service('event_dispatcher');
  $event = new WebformNodeElementPreRender($element_id, $nid, 'webform_element');
  $dispatcher
    ->dispatch(WebformNodeElementPreRender::PRERENDER, $event);
  $nid = $event
    ->getNid();

  // This guarantees the nid is valid.
  $nid_array = \Drupal::entityQuery('node')
    ->condition('nid', $nid)
    ->execute();
  $nid = reset($nid_array);
  $display_mode = $event
    ->getDisplayMode();
  if ($nid && $display_mode) {
    $node = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->load($nid);
    if ($node
      ->access('view')) {
      $view_builder = \Drupal::entityTypeManager()
        ->getViewBuilder('node');
      if ($node && $view_builder) {
        if ($render_array = $view_builder
          ->view($node, $display_mode)) {
          $element['#markup'] = \Drupal::service('renderer')
            ->render($render_array, false);
        }
      }
    }
  }
  else {
    \Drupal::logger('webform_node_element')
      ->notice('webform_node_element @element_id not rendered because nid was set to "@nid" and display_mode was set to "@display_mode"', [
      '@element_id' => $element_id,
      '@nid' => $nid,
      '@display_mode' => $display_mode,
    ]);
  }
  return $element;
}