You are here

function webform_entity_print_preprocess_entity_print in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_entity_print/webform_entity_print.module \webform_entity_print_preprocess_entity_print()

Implements hook_preprocess_entity_print().

File

modules/webform_entity_print/webform_entity_print.module, line 225
Provides Entity Print (PDF) integration.

Code

function webform_entity_print_preprocess_entity_print(array &$variables) {
  $webform_submission = _webform_entity_print_preprocess_entity_print_get_webform_submission($variables['content']);
  if (!$webform_submission) {
    return;
  }

  // Add webform submission to variables.
  $variables['webform_submission'] = $webform_submission;
  $webform = $webform_submission
    ->getWebform();
  $css = [];

  // Add webform CSS.
  $assets = $webform
    ->getAssets();
  if ($assets['css']) {
    $css[] = $assets['css'];
  }

  // Add webform entity print CSS.

  /** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
  $third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');

  // Append default print template CSS.
  $default_template = $third_party_settings_manager
    ->getThirdPartySetting('webform_entity_print', 'template') ?: [];
  if (!empty($default_template['css'])) {
    $css[] = $default_template['css'];
  }

  // Append webform print template CSS.
  $webform_template = $webform
    ->getThirdPartySetting('webform_entity_print', 'template') ?: [];
  if (!empty($webform_template['css'])) {
    $css[] = $webform_template['css'];
  }

  // Append a style tag to entity print CSS link tags.
  $variables['entity_print_css'] = Markup::create($variables['entity_print_css'] . PHP_EOL . '<style type="text/css" media="all">' . PHP_EOL . implode(PHP_EOL, $css) . PHP_EOL . '</style>' . PHP_EOL);

  // Append webform entity print image token to all files in the rendered
  // webform submission data.
  // NOTE: We are always rendering the webform submission data so that
  // value is consistent.
  // @see webform_entity_print_webform_submission_access();
  $content =& NestedArray::getValue($variables, [
    'content',
    0,
    0,
  ]);
  if (!$content) {
    $content =& NestedArray::getValue($variables, [
      'content',
      0,
    ]);
  }

  // Make absolute sure we are rendering and altering
  // the webform submission data.
  if (!$content || !is_array($content) || !isset($content['#theme']) || $content['#theme'] !== 'webform_submission_data') {
    return;
  }
  $webform_id = $webform
    ->id();
  $sid = $webform_submission
    ->id();

  // Render the webform submission data
  $html = (string) \Drupal::service('renderer')
    ->render($content);

  // Only matching <img src=""/>.
  if (preg_match_all('#(src\\s*=\\s*")([^"]+(?:/private|/system/files)/webform/' . $webform_id . '/' . $sid . '/[^"]+)#', $html, $matches)) {
    foreach ($matches[2] as $index => $found_uri) {
      $token_query = [
        WEBFORM_ENTITY_PRINT_IMAGE_TOKEN => _webform_entity_print_token_generate($found_uri),
      ];
      $replace_uri = $found_uri . (strpos($found_uri, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($token_query);
      $html = str_replace($matches[0][$index], $matches[1][$index] . $replace_uri, $html);
    }
  }

  // The HTML markup is safe because it has already been rendered.
  $content = [
    '#markup' => Markup::create($html),
  ];
}