public function processFileField(array $element, WebformSubmissionInterface $webform_submission, &$cleanvalues) {

    $key = $element['#webform_key'];
    $original_data = $webform_submission->getOriginalData();

    $value = isset($cleanvalues[$key]) ? $cleanvalues[$key] : [];
    $fids = (is_array($value)) ? $value : [$value];

    $original_value = isset($original_data[$key]) ? $original_data[$key] : [];
    $original_fids = (is_array($original_value)) ? $original_value : [$original_value];

    // Delete the old file uploads?
    // @TODO build some cleanup logic here. Could be moved to attached field hook.
    $delete_fids = array_diff($original_fids, $fids);

    // @TODO what do we do with removed files?
    // Idea. Check the fileUsage. If there is still some other than this one
    // don't remove.
    // But also, if a revision is using it? what a mess!
    // @see \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::deleteFiles

    // Exit if there is no fids.
    if (empty($fids)) {
      return;
    }

    /** @var \Drupal\file\FileInterface[] $files */
    try {
      $files = $this->entityTypeManager->getStorage('file')->loadMultiple(
        $fids
      );
    } catch (InvalidPluginDefinitionException $e) {
    } catch (PluginNotFoundException $e) {
    }
    $fileinfo_many = [];
    //@TODO refactor this urgently to a NEW TECHMD class.
    foreach ($files as $file) {
      $fileinfo = [];
      if (isset($cleanvalues['as:image'])) {
        $fileinfo = $this->check_file_in_metadata(
          $cleanvalues['as:image'],
          (int) $file->id()
        );
        if ($fileinfo) {
          $fileinfo_many[$fileinfo['dr:url']] = $fileinfo;
        }
      }
      if (!$fileinfo) {
        // Only do this is file was not previusly processed and stored.
        $uri = $file->getFileUri();
        $md5 = md5_file($uri);

        $fileinfo = [
          'type' => 'Image',
          'dr:url' => $uri,
          'url' => $uri,
          'checksum' => $md5,
          'dr:for' => $key,
          'dr:fid' => (int) $file->id(),
          'name' => $file->getFilename(),
        ];
        $relativefolder = substr($md5, 0, 3);

        $source_uri = $file->getFileUri();
        $realpath_uri = $this->fileSystem->realpath($source_uri);
        if (empty(!$realpath_uri)) {

          $command = escapeshellcmd(
            '/usr/local/bin/fido  ' . $realpath_uri . ' -pronom_only -matchprintf '
          );
          // @TODO handle the exit code from output
          $output = shell_exec($command . '"OK,%(info.puid)" -q');

        }
        // We will use the original scheme here since our HD operations are over.
        $destination_uri = $this->getFileDestinationUri(
          $element,
          $file,
          $relativefolder,
          $webform_submission
        );
        //Use the wished storagepoint as key.
        //Then the node save hook will deal with moving data.
        $fileinfo_many[$destination_uri] = $fileinfo;

      }
    }
    $cleanvalues['as:image'] = $fileinfo_many;
  }
public function processFileField(array $element, WebformSubmissionInterface $webform_submission, &$cleanvalues) {

    $key = $element['#webform_key'];
    $original_data = $webform_submission->getOriginalData();

    $value = isset($cleanvalues[$key]) ? $cleanvalues[$key] : [];
    $fids = (is_array($value)) ? $value : [$value];

    $original_value = isset($original_data[$key]) ? $original_data[$key] : [];
    $original_fids = (is_array($original_value)) ? $original_value : [$original_value];

    // Delete the old file uploads?
    // @TODO build some cleanup logic here. Could be moved to attached field hook.
    $delete_fids = array_diff($original_fids, $fids);

    // @TODO what do we do with removed files?
    // Idea. Check the fileUsage. If there is still some other than this one
    // don't remove.
    // But also, if a revision is using it? what a mess!
    // @see \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::deleteFiles

    // Exit if there is no fids.
    if (empty($fids)) {
      return;
    }

    /** @var \Drupal\file\FileInterface[] $files */
    try {
      $files = $this->entityTypeManager->getStorage('file')->loadMultiple(
        $fids
      );
    } catch (InvalidPluginDefinitionException $e) {
    } catch (PluginNotFoundException $e) {
    }
    $fileinfo_many = [];
    //@TODO refactor this urgently to a NEW TECHMD class.
    foreach ($files as $file) {
      $fileinfo = [];
      if (isset($cleanvalues['as:image'])) {
        $fileinfo = $this->check_file_in_metadata(
          $cleanvalues['as:image'],
          (int) $file->id()
        );
        if ($fileinfo) {
          $fileinfo_many[$fileinfo['dr:url']] = $fileinfo;
        }
      }
      if (!$fileinfo) {
        // Only do this is file was not previusly processed and stored.
        $uri = $file->getFileUri();
        $md5 = md5_file($uri);

        $fileinfo = [
          'type' => 'Image',
          'dr:url' => $uri,
          'url' => $uri,
          'checksum' => $md5,
          'dr:for' => $key,
          'dr:fid' => (int) $file->id(),
          'name' => $file->getFilename(),
        ];
        $relativefolder = substr($md5, 0, 3);

        $source_uri = $file->getFileUri();
        $realpath_uri = $this->fileSystem->realpath($source_uri);
        if (empty(!$realpath_uri)) {

          $command = escapeshellcmd(
            '/usr/local/bin/fido  ' . $realpath_uri . ' -pronom_only -matchprintf '
          );
          // @TODO handle the exit code from output
          $output = shell_exec($command . '"OK,%(info.puid)" -q');

        }
        // We will use the original scheme here since our HD operations are over.
        $destination_uri = $this->getFileDestinationUri(
          $element,
          $file,
          $relativefolder,
          $webform_submission
        );
        //Use the wished storagepoint as key.
        //Then the node save hook will deal with moving data.
        $fileinfo_many[$destination_uri] = $fileinfo;

      }
    }
    $cleanvalues['as:image'] = $fileinfo_many;
  }
public function processFileField ( array $ element , WebformSubmissionInterface $ webform_submission , & $ cleanvalues ) { $ key = $ element [ '#webform_key' ] ; $ original_data = $ webform_submission -> getOriginalData ( ) ; $ value = isset ( $ cleanvalues [ $ key ] ) ? $ cleanvalues [ $ key ] : [ ] ; $ fids = ( is_array ( $ value ) ) ? $ value : [ $ value ] ; $ original_value = isset ( $ original_data [ $ key ] ) ? $ original_data [ $ key ] : [ ] ; $ original_fids = ( is_array ( $ original_value ) ) ? $ original_value : [ $ original_value ] ; // Delete the old file uploads? // @TODO build some cleanup logic here. Could be moved to attached field hook. $ delete_fids = array_diff ( $ original_fids , $ fids ) ; // @TODO what do we do with removed files? // Idea. Check the fileUsage. If there is still some other than this one // don't remove. // But also, if a revision is using it? what a mess! // @see \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::deleteFiles // Exit if there is no fids. if ( empty ( $ fids ) ) { return ; } /** @var \Drupal\file\FileInterface[] $files */ try { $ files = $ this -> entityTypeManager -> getStorage ( 'file' ) -> loadMultiple ( $ fids ) ; } catch ( InvalidPluginDefinitionException $ e ) { } catch ( PluginNotFoundException $ e ) { } $ fileinfo_many = [ ] ; //@TODO refactor this urgently to a NEW TECHMD class. foreach ( $ files as $ file ) { $ fileinfo = [ ] ; if ( isset ( $ cleanvalues [ 'as:image' ] ) ) { $ fileinfo = $ this -> check_file_in_metadata ( $ cleanvalues [ 'as:image' ] , ( int ) $ file -> id ( ) ) ; if ( $ fileinfo ) { $ fileinfo_many [ $ fileinfo [ 'dr:url' ] ] = $ fileinfo ; } } if ( ! $ fileinfo ) { // Only do this is file was not previusly processed and stored. $ uri = $ file -> getFileUri ( ) ; $ md5 = md5_file ( $ uri ) ; $ fileinfo = [ 'type' => 'Image' , 'dr:url' => $ uri , 'url' => $ uri , 'checksum' => $ md5 , 'dr:for' => $ key , 'dr:fid' => ( int ) $ file -> id ( ) , 'name' => $ file -> getFilename ( ) , ] ; $ relativefolder = substr ( $ md5 , 0 , 3 ) ; $ source_uri = $ file -> getFileUri ( ) ; $ realpath_uri = $ this -> fileSystem -> realpath ( $ source_uri ) ; if ( empty ( ! $ realpath_uri ) ) { $ command = escapeshellcmd ( '/usr/local/bin/fido  ' . $ realpath_uri . ' -pronom_only -matchprintf ' ) ; // @TODO handle the exit code from output $ output = shell_exec ( $ command . '"OK,%(info.puid)" -q' ) ; } // We will use the original scheme here since our HD operations are over. $ destination_uri = $ this -> getFileDestinationUri ( $ element , $ file , $ relativefolder , $ webform_submission ) ; //Use the wished storagepoint as key. //Then the node save hook will deal with moving data. $ fileinfo_many [ $ destination_uri ] = $ fileinfo ; } } $ cleanvalues [ 'as:image' ] = $ fileinfo_many ; }
Process temp files and make them permanent

@param array $element
An associative array containing the file webform element.
@param \Drupal\webform\webformSubmissionInterface $webform_submission
@param $cleanvalues
Process temp files and make them permanent