Skip to content

Instantly share code, notes, and snippets.

@arpitr
Created June 7, 2018 14:28
Show Gist options
  • Save arpitr/6bdf5611cdab89e954ade6986652df00 to your computer and use it in GitHub Desktop.
Save arpitr/6bdf5611cdab89e954ade6986652df00 to your computer and use it in GitHub Desktop.
public function image_upload($image_path, $filename, $is_image_name_set = FALSE) {
$output = [];
$fid = ParagraphImportProcessParagraph::getFidFromHashing($image_path, $this->configuration);
if (!$fid) {
$fid = ParagraphImportProcessParagraph::create_image($filename, $image_path, $this->configuration);
}
if ($fid) {
$output = ParagraphImportProcessParagraph::attach_image_info($fid, '', '');
}
return $output;
}
public function getFidFromHashing($filename, $configuration) {
$image_hashing = isset($configuration['image_hashing']['validate']) ? $configuration['image_hashing']['validate'] : '';
if (!$image_hashing) {
// We have to check from row if image_hashing configuration is set globally.
$row = isset($configuration['row']) ? $configuration['row'] : '';
if (is_object($row)) {
$image_hashing = $row->getSourceProperty('image_hashing');
}
}
if ($image_hashing) {
$algorithm = (isset($configuration['image_hashing']['algorithm'])) ? $configuration['image_hashing']['algorithm'] : 'md5';
$hash_key = ParagraphImportProcessParagraph::getFileHashKey($algorithm, $filename);
$fid = ParagraphImportProcessParagraph::getFidFromHashKey($hash_key);
if ($fid) {
return $fid;
}
}
}
/**
* Get file information from the file_managed schema.
*
* @param string $filename
* Name of the file.
*
* @return integer
* Returns file fid if present.
*/
public static function get_file_fid($filename) {
$filename = trim($filename);
$query = \Drupal::database()->select('file_managed', 'p');
$query->addField('p', 'fid');
$query->condition('p.filename', $filename, 'like');
$fid = $query->execute()->fetchField();
return $fid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment