Problem/Motivation

Generally, there are xml elements and attributes returned by the appliance that are being ignored by the google_appliance module. In my case, I'd like to decorate my search results based on mime type, which is returned as an attribute for file urls.

Proposed resolution

The simplest way to do this is to continue extending the google_appliance_parse_device_response_xml function in the .module file (e.g. as was done for keywords). A more general solution would be to provide a hook for modifying the results array.

The general solution would be more flexible. It would allow for removing results, parsing custom xml elements, and interoperability with multiple (future?) versions of google appliance that might have different DTDs.

Remaining tasks

A patch is attached. But is this the best design?

Also, test, and document.

User interface changes

None.

API changes

hook_google_appliance_results_alter(&$results, $simple_xml)

Frankly, I don't know how hooks get documented, so here is my example usage for adding a 'mime' element to each result that has the 'MIME' attribute.

/*
 * @Implements hook_google_appliance_results_alter
 *
 * @arg &$results
 *    results parsed so far, to be altered
 * @arg &$simple_xml
 *    a simplexml object containing the full results returned from the appliance
 */
function local_mods_google_appliance_results_alter(&$results, $simple_xml) {
  foreach ($results['entry'] as &$res_array) {
    $abs_url = $res_array['abs_url'];
    if ($url_element = $simple_xml->xpath("//U[text()='$abs_url']")) {
      if ($res_element = $url_element[0]->xpath("parent::*")) {
        $res_array['mime'] = $res_element[0]['MIME'];
      }
    }
  }
}
CommentFileSizeAuthor
hook_ga_results_alter.patch398 bytesrt_davies

Comments

rt_davies’s picture

Just in case anyone wants to solve the same exact problem, decorating file urls with icons, here's my complete solution. Note this is not part of the proposed patch, just a freewill offering in case it's useful to someone...

Step 1. Apply the patch
Step 2. Implement the hook, as shown in the original post (local_mods_google_appliance_results_alter(...))
Step 3. Convert the data provided by the hook into html in a theme preprocess function...

function preprocess_google_appliance_result(&$vars)
// If the result has a raw mime type value, such as "application/pdf",
// set by local_mods_google_appliance_results_alter(...).
if ($vars['mime']) {
  // Then we leverage the Drupal core file icon theming,
  // but to do that we need a psuedo file object with a 'filemime' attribute.
  $file = new stdClass;
  $file->filemime = $vars['mime'];
  // Finally, replace the raw mime type with html for a mimetype icon...
  $vars['mime'] = theme_file_icon(array('file'=>$file));
  // ...which will be rendered by ../templates/google-appliance-result.tpl.php.
}

Step 4. Copy the google-appliance-result.tpl.php from the google_appliance module into your theme.
Step 5. Modify your version of the template, printing the file icon html...

 <?php print render($title_prefix); ?>
 <h3 class="title"<?php print $title_attributes; ?>>
    <?php print $mime; ?>
    <a href="<?php print $abs_url; ?>"><?php print $title; ?></a>
 </h3>

Hope that's useful.

mpgeek’s picture

Assigned: Unassigned » mpgeek

I came up with a more streamlined implementation for what the patch does. I didn't see any harm in adding the mime type and icon to the vars in preprocess. Also made a note in the comments of the result template for how to use it in your theme. Committed to 7.x-1.x (dev)...
http://drupalcode.org/project/google_appliance.git/log/refs/heads/7.x-1.x

mpgeek’s picture

Status: Active » Closed (fixed)

Added to stable release: http://drupal.org/node/1518352

mpgeek’s picture

Issue summary: View changes

fix typo