-
Pointing All Taxonomy Term Links to ApacheSolr Search Results
I’ve been working on a project that leverages ApacheSolr for many of the displays on the site. One of the requirements is that every instance of a taxonomy term link on the site needs to point to search results pre-filtered (using FacetAPI) on the term. The other requirement is that if a taxonomy term’s vocabulary doesn’t have an associated search facet, the user should end up on the search page with results as if the user had searched for the term’s name in the search text box.
- If you see a “Climate Change” term (tid = 153) link from the “Topics” vocabulary (vid = 8), it should render as <a href="/search/site?f[0]=im_field_tr_topics%3A153">Climate Change</a> in the DOM.
- If you see a “Blog” term (tid = 123) link from the “Keywords” vocabulary (vid = 6), it should render as <a href="/search/site/blog">Blog</a> in the DOM.
-
Indexing Specific Field Collection Fields in Apache Solr for Drupal 7
I’m not sure how much of this is possible without Display Suite, but here’s the setup I’ve used to index specific fields within a field collection as part of the parent entity. I use the “Search index” view mode to specify which fields I want to index on each content type. Here’s what that looks like:
As you can see, the “Versions” field and the “Associated people / organizations” field are field collections. In this example I only wanted to index a couple of fields from the “Versions” field collection as part of the search index for each “Book” node.
-
Quickly Implement a Block as a Paragraph in Drupal 7
Disclaimer: As with most tasks in Drupal, there are several ways to do this. My goal here was to see how quickly it could be done without writing code and without introducing unnecessary complexity.
Goal — quickly implement a newsletter listing (using Views) that can be dropped into any paragraph field and can have a unique title per use.
The output would be a simple block from Views. We already use Display Suite, so naturally we use a DS-centric approach.
-
Bulk Add URL Redirects in Drupal 7
Here’s a quick example showing how to add URL redirects (for the redirect module) in bulk.
Instead of a separate page for each list of “fellows”, we built a single view with exposed filters to choose a semester.
The original paths looked like “/fall-2000-fellows” and the new path looks like “/fellows/list?semester=5493”.
123456789101112131415161718192021222324252627282930313233343536// Semester will be passed as a query param. Redirect uses strings if you save// a redirect through the URL Redirects gui, so we'll use strings too.$redirects = array('former-fellows-year' => array('path' => 'fellows/list'),'fall-2010-fellows' => array('path' => 'fellows/list', 'semester' => '5433'),'fall-2011-fellows' => array('path' => 'fellows/list', 'semester' => '5411'),'fall-2012-fellows' => array('path' => 'fellows/list', 'semester' => '5408'),'fall-2013-fellows' => array('path' => 'fellows/list', 'semester' => '5405'),'fall-2014-fellows' => array('path' => 'fellows/list', 'semester' => '5403'),'fall-2015-fellows' => array('path' => 'fellows/list', 'semester' => '5401'),'spring-2010-fellows' => array('path' => 'fellows/list', 'semester' => '5434'),'spring-2011-fellows' => array('path' => 'fellows/list', 'semester' => '5410'),'spring-2012-fellows' => array('path' => 'fellows/list', 'semester' => '5409'),'spring-2013-fellows' => array('path' => 'fellows/list', 'semester' => '5406'),'spring-2014-fellows' => array('path' => 'fellows/list', 'semester' => '5404'),'spring-2015-fellows' => array('path' => 'fellows/list', 'semester' => '5402'),'spring-2016-fellows' => array('path' => 'fellows/list', 'semester' => '5400'),);foreach ($redirects as $src => $dest) {$redirect = new stdClass();$redirect_options = array();if ($dest['semester']) {$redirect_options = array('query' => array('semesters' => $dest['semester']));}module_invoke('redirect', 'object_prepare', $redirect,array('source' => $src,'source_options' => array(),'redirect' => $dest['path'],'redirect_options' => $redirect_options,'language' => LANGUAGE_NONE,));module_invoke('redirect', 'save', $redirect);} -
Update Field Value for All Nodes
I recently added a “Show in Listing” boolean on/off checkbox field to a content type (default value is to be “checked”). There were 133 nodes when I created that field. I needed to update all of those nodes to be “checked”.
Here’s how I updated all of them using entity_metadata_wrapper(). It’s much quicker than any alternative I could think of (feeds, rules, etc.).
1234567891011$nids = db_query('SELECT nid FROM {node} WHERE type = :type', array(':type' => 'blogpost'))->fetchCol();foreach ($nids as $nid) {$node = node_load($nid);if ($node) {$wrapper = entity_metadata_wrapper('node', $node);if (isset($wrapper->field_islisted)) {$wrapper->field_islisted->set(1);$wrapper->save();}}}In this particular case I’m targeting a single content type. I could’ve used a similar query and some slight modifications to set the value on any node whose bundle (content type) supports that field_islisted field.
Lastly, if you have a lot of nodes, you might consider using node_load_multiple() instead of node_load() .
-
Adding a “Filter by year” Facet for a Date Field in Apachesolr for Drupal 7
A Drupal 7 site I’m working on makes use of the Apache Solr Search module and FacetAPI. Some of the content has a date field named field_s_date. I was tasked with providing a sidebar “Filter by year” facet block to filter the content by the year value of this date field. I’m pretty sure I could have done this a few different ways, but I settled on adding the year value as a separate field to the solr index and adding an associated facet using hook_facetapi_facet_info() .
First, to add the field, I used hook_apachesolr_index_document_build() in a custom module as follows:
-
Rendering an Image in Apache SOLR Search results (Drupal 7)
Introduction
There are a few ways to get extra information into the output of your Apache Solr Search results. The most efficient ways involve getting the information into the solr index and pulling it directly from the solr index. We do not want to introduce extra node_load() calls to render our search results. We do not want to have to use Views either.
So, using just apachesolr and some functions, I’m going to show how to get an image field into the search index and pull it back out in our result output. I know I’m not handling alt and title attributes in this example. I’ll let you figure that one out.
This is a step-by-step breakdown of my experience getting this to work. In this example I’m attempting to render an image field called field_forum_image for the forum (content type) nodes in my search results. You can use the same procedure for adding other fields, but please make sure you understand the solr prefixes (e.g., ss, ts, tm, etc.) before doing so. You can find these in the schema.xml file with in the apachesolr module.
-
Importing Paragraph Content with Feeds in Drupal 7
As I write this, there isn’t an established way to import data into Paragraphs using Feeds.
This solution only caters to my particular fields, but it should serve as an example of one way to import HTML content into a basic “textarea” paragraph.
Goal
My goal was to import HTML content into a Basic page content type as a basic textarea paragraph item. The original data is from a Drupal 7 typical “body” field, and I want to import the data into a paragraphs-based body field.
-
Auto-create URL Redirects in Feeds
I’m working on a website redesign. The original site used awful URL aliases where nearly everything got a URL like /content/title-of-page-would-end-up-looking-like-this. There are no “sections” to speak of in the URL structure, and there are several thousand nodes! I’m migrating most of the content from the “old” site to the new one using Feeds. Rather than manually mess with rewrite rules, I figured I should just create a redirect at the time of migration. We typically handle redirects/rewrites using the redirect module. So, how do you do this?
-
Adding Metatag Keywords to Search API Index
For a great explanation of the implementation below, please visit http://enzolutions.com/articles/2015/06/02/how-to-add-customs-fields-to-solr-index-using-entity-api-amp-search-api/
The following demonstrates how to add metatag keywords (from the Metatag module) to your Search API fields list, so that you can index them and make them searchable.