Kurt Jarchow's Blog

January 28, 2009

Add Drupal Forms using AJAX

Filed under: Uncategorized — Tags: , , — Kurt Jarchow @ 12:05 pm

Something very easy to do in drupal is adding form object to the content of your page without doing a page refresh.  I needed to add functionality so that an application form was displayed to a user when they clicked the Apply link.

Using JQuery we can “intercept” a client click of a link.  Add a .js file to which ever module you are editing, and add the following:

 if (Drupal.jsEnabled) {  

  $(document).ready(function () {
    $(’a.apply-link’).click(function () {

      var submitApplication = function(data) {
        $(’div.apply-link’).html(data.html);
      }

    $.ajax({
      type: ‘POST’,
      url: this.href,
      dataType: ‘json’,
      success: submitApplication,
      data: ‘js=1′
    });
    return false;
  });
  });
}
So when a link with a class “submit-application” is clicked an ajax/json call is made to the url.  Now make sure the js is included somewhere in your module code by using drupal_add_js(). 
Now all we have to do is make sure the page renders as json if an ajax call is made.  Add this  code just before the _page exits (but not after the exit(); !).
 if (!empty($_POST['js'])) {
    $html = drupal_get_form(’job_posting_application_form’, array($node->nid, $node->title, $node->job_posting_email));
    drupal_json(array(
    ‘html’ =>  $html
    )
    );
Your link should be replaced with the form.
If you want to add a little bit of animation use the jQuery animation options.  Instead of just replacing the link, make it fade in:
$(’div.apply-link’).hide();
$(’div.apply-link’).html(data.html);
$(’div.apply-link’).fadeIn();
Credit to John K. VanDyk’s book for breaking the back of this code.

December 14, 2008

SOLR, Drupal, and GEO Spatial Results

Filed under: Uncategorized — Tags: , , , — Kurt Jarchow @ 10:05 pm

Our search needed to preform typical longitude and latitude geo-radius spatial results (I’ve mashed up all of those keywords because I really am not sure what someone will search for to find this post, apologies for the grammar!).  Since I took on SOLR as our search engine I needed to find an elegant solution.

After some search, I found the holy grail: Local SOLR.  Not finding much instruction on the official site, I found a great blog post with an explanation and example.  Its fairly easy to install and integrate into drupal.

  1. Extract the files and copy them into your solr directory (if your main solr instance is under “example”, make it “example2″.
  2. Stop your sorl instance (I’m assuming your on a dev box!) and start the local solr.
  3. Test to see if its working using the localhost cinema example.
  4. If it works find the java files and copy them into your solr project.
  5. Update your solr.config and schema files.  Sorry I don’t have the exact lines (not at work) but its pretty obvious what lines are local solr related. <edit: read below>
  6. Update the drupal code to send the extra fields (I’ll try to find the exact ones later) .
  7. You’re done!

The only gotcha I found, resulting in a few lost hairs and an increase in blood pressure, was a funny error concerning the comments field.  The error I received was a integer conversion error, which I eventually found out was the number of comments being blank.  Just make sure you adjust your apachesolr.module file to set comments to zero when it is null.

I’ll try to get the drupal developers to support local solr so we can have some official code for you to use.

That’s it for now!

<update>

As suggested I’ll post this on the Drupal website, but for anyone interested the updates to the solr.config and scheme files:

::schema.xml

<field name=”lat” type=”sdouble” indexed=”true” stored=”true”/>
<field name=”lng” type=”sdouble” indexed=”true” stored=”true”/>
<field name=”geo_distance” type=”sdouble”/>
<dynamicField name=”_local*” type=”sdouble” indexed=”true” stored=”true”/>

::solr.config

line 177

<searchComponent name=”localsolr”     class=”com.pjaol.search.solr.

component.LocalSolrQueryComponent” >
<str name=”latField”>lat</str>
<str name=”lngField”>lng</str>
</searchComponent>

<!– local lucene request handler –>
<requestHandler name=”geo” class=”org.apache.solr.handler.component.SearchHandler”>
<lst name=”defaults”>
<str name=”echoParams”>explicit</str>
</lst>
<arr name=”components”>
<str>localsolr</str>
<str>facet</str>
<str>mlt</str>
<str>highlight</str>
<str>debug</str>
</arr>
</requestHandler>

line 574
<searchComponent name=”localsolr”     class=”com.pjaol.search.solr.

component.LocalSolrQueryComponent” >
<str name=”latField”>lat</str>
<str name=”lngField”>lng</str>
</searchComponent>

<!– local lucene request handler –>
<requestHandler name=”geo” class=”org.apache.solr.handler.component.SearchHandler”>
<lst name=”defaults”>
<str name=”echoParams”>explicit</str>
</lst>
<arr name=”components”>
<str>localsolr</str>
<str>facet</str>
<str>mlt</str>
<str>highlight</str>
<str>debug</str>
</arr>
</requestHandler>

November 19, 2008

Building a Multinational Website

Filed under: Uncategorized — Tags: , , , — Kurt Jarchow @ 10:28 pm

There is nothing easy about building a multiple-location, multi-lingual website.  The options are getting better and better however to make this as smooth as possible.

I’ve been blogging about my experiences, starting from scratch, learning the LAMP stack and delivering a high quality, high performance solutions.  I’ve had my head in a few books lately; a php beginners book, a php advanced book, and a Drupal Pro book.  My eyes are heavy but I have a prototype to my end solution.

Building a solr search driven search site is completely painless, and getting faceted search options are completely customizable.  Out of the box, Drupal gives our website about 60% of our functionality.

However, be prepared: Drupal is a whole other language unto itself.  Coming from a nice SDK an a .Net environment, the vast number of function names you need to remember is quite dawnting.  .Net feels much cleaner- that being said I think I can do much more with the php code.

My next steps will be mastering the Jquery integration.  This site needs to feel real 2.0, and I plan on keeping up with key AJAX function.

That’s it for me tonight, time for some sleep.

Powered by WordPress

Bad Behavior has blocked 86 access attempts in the last 7 days.