Skip to content

Instantly share code, notes, and snippets.

@dasginganinja
Last active December 30, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dasginganinja/7776240 to your computer and use it in GitHub Desktop.
Save dasginganinja/7776240 to your computer and use it in GitHub Desktop.
I absolutely love GoTos and I don't know why..
<?php
/**
* Retrieves the title of the Content Item with the given filepath
*/
function km_zipcart_get_title($file) {
// Start our query with searching for the file
$qf = db_select('file_managed','fm');
$qf->condition('fm.filename',$file,'=');
// Get the fields we need
$qf->fields('fm', array('fid','filename'));
$qf->fields('fd', array('entity_id'));
$qf->fields('n', array('title'));
// Need to make copies of this (one for images, one for files)
$qi = clone $qf;
// Join in the appropriate databases
$qi->leftJoin('field_data_field_upload_image','fd','(fm.fid = fd.field_upload_image_fid AND NOT ISNULL(fd.entity_id))');
$qf->leftJoin('field_data_field_upload_file', 'fd','(fm.fid = fd.field_upload_file_fid AND NOT ISNULL(fd.entity_id))');
$qi->leftJoin('node','n','n.nid = fd.entity_id');
$qf->leftJoin('node','n','n.nid = fd.entity_id');
// Prepare For Battle
$info = array();
$query = $qf;
// Execute
RATRACE:
$result = $query->execute();
if ($result) {
while ($row = $result->fetchAssoc()) {
if (empty($info)) $info = $row;
else {
if (empty($info['title'])) {
$info['title'] = $row['title'];
}
if (empty($info['entity_id'])) {
$info['entity_id'] = $row['entity_id'];
}
}
}
}
if ($query === $qf) {
$query = $qi;
goto RATRACE;
}
drupal_add_http_header('Content-Type','text/plain');
print_r($info);
}
@dasginganinja
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment