You are here

function _webform_parse_file_uuids in Webform 8.5

Same name and namespace in other branches
  1. 6.x includes/webform.editor.inc \_webform_parse_file_uuids()

Parse text for any linked files with data-entity-uuid attributes.

Parameters

string $text: The text to parse.

Return value

array An array of all found UUIDs.

See also

_editor_parse_file_uuids()

3 calls to _webform_parse_file_uuids()
TextFormat::postDelete in src/Plugin/WebformElement/TextFormat.php
Delete any additional value associated with an element.
TextFormat::postSave in src/Plugin/WebformElement/TextFormat.php
Acts on a saved webform submission element before the insert or update hook is invoked.
_webform_get_array_file_uuids in includes/webform.editor.inc
Finds all files referenced (data-entity-uuid) in an associative array.

File

includes/webform.editor.inc, line 241
Webform module editor file upload hooks.

Code

function _webform_parse_file_uuids($text) {
  if (strpos($text, 'data-entity-uuid') === FALSE) {
    return [];
  }
  $uuids = [];

  // Look through all images and hyperlinks for files.
  if (preg_match_all('/<[^>]+data-entity-type[^>]+>/', $text, $matches)) {
    foreach ($matches[0] as $match) {

      // Cleanup quotes escaped via YAML.
      // Please note, calling stripslashes() twice because elements are
      // double escaped.
      $match = stripslashes(stripslashes($match));

      // Look for a file and record UUID when found.
      $dom = Html::load($match);
      $xpath = new \DOMXPath($dom);
      $nodes = $xpath
        ->query('//*[@data-entity-type="file" and @data-entity-uuid]');
      if (count($nodes) && $nodes
        ->item(0)) {
        $uuids[] = $nodes
          ->item(0)
          ->getAttribute('data-entity-uuid');
      }
    }
  }

  // Use array_unique() to collect one uuid per uploaded file.
  // This prevents cut-n-pasted uploaded files from having multiple usages.
  return array_unique($uuids);
}