Apache Solr Views and a terrible way to handle results per content type in Drupal 7

I’m betting on there being a better way to do this.  There just has to be.  But I haven’t figured it out, so here is how we’re using (abusing?) Views PHP and ApacheSolr Views  to deliver results on a per-content type basis.

apachesolr views with per content type results

apachesolr views with per content type results

To start with we create a new view

choose the correct solr index

choose the correct solr index

After the new view is created load a bunch of fields on in – it may be useful to remember that “Bundle” will return a content type by name, the “content” will return all of the fielded content in an string, and “entity_id” has the node value.  One cool thing you can do with the node id is then pass that as an argument to another view using Views Field View - be aware that this is somewhat performance naughty, but it’ll work if you want access to images in your results and don’t feel like creating a custom module to index images.

exclude fields from display

exclude fields from display

After getting your fields loaded I set their values to “Exclude from display” in the field settings

 

Our view with a few fields loaded

Our view with a few fields loaded

And of course don’t forget to add a Global: PHP field – here’s our view now

 

In the Global PHP comes this fubar stank piece of code

<?php
$type=$row->bundle; // Here we’ll get our content types
$content=$row->content; // This is all of the content per node that’s been indexed
$title=$row->label; // This is the node title
if ($type==”mobile_resource”)
{
$stop=”URL:”;
$startI = 1;
$stopI = strpos($content, $stop, $startI);
$text=substr($content, $startI, $stopI – $startI);
$trimtext=trim($text);
$startsAt = strpos($content, “URL:”) + strlen(“URL:”);
$endsAt = strpos($content, “Platform:”, $startsAt);
$result = substr($content, $startsAt, $endsAt – $startsAt);
$trimurl=trim($result, chr(0xC2).chr(0xA0)); // This will save someone time – be sure to trim the nbsp spaces from your content when needed

echo “<a href=”$trimurl”>$title: $trimtext</a>”; // and then print stuff as you see fit

}
else if ($type == “cme_class”) // and more of the same
{
$stop=”Description:”;
$startI = 1;
$stopI = strpos($content, $stop, $startI);
$text=substr($content, $startI, $stopI – $startI);
$trimtext=trim($text);
$url=$row->path_alias;
echo “<a href=”$url”>$trimtext</a>”;

$startsAt = strpos($content, “Location:”) + strlen(“Location:”);
$endsAt = strpos($content, “Date and Time:”, $startsAt);
$location = substr($content, $startsAt, $endsAt – $startsAt);

$startsAt2 = strpos($content, “Time:”) + strlen(“Time:”);
$endsAt2 = strpos($content, “Registration”, $startsAt2);
$time = substr($content, $startsAt2, $endsAt2 – $startsAt2);

print(“<h5>Time: $time</h5>”);
print(“<h5>Place: $location</h5>”);
}
else if ($type == “employee_bio”) //etc
{
$startsAt = 1;
$endsAt = strpos($content, “My Education”, $startsAt);
$who = substr($content, $startsAt, $endsAt – $startsAt);
$url=$row->path_alias;
echo “<a href=”$url”>$who</a>”;
}
else
{
$url=$row->path_alias;
echo “<a href=”$url”>$title</a>”;
}
?>

And really… I’m pretty embarrassed by this.   It’s raunchy and rank.  It works.  The facets facet and so on… I just can’t believe that this is the way to do things.  I’ve looked at Solr Panels and feel like there must be a way to do better – maybe having multiple views pre-faceted by bundle at the view level and the made in to content panes?   Anyone with some sense of how to improve this is encouraged to chime in…. Bueller?  Bueller….