Last active
December 30, 2015 04:29
-
-
Save dasginganinja/7776240 to your computer and use it in GitHub Desktop.
I absolutely love GoTos and I don't know why..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://xkcd.com/292/