开发者

How to Search Twitter with Drupal and Juitter Module BUT with the search box in the home page

I am using Drupal 6.x, I installed successfully

Juitter http://drupal.org/project/juitter

which is doing exactly what my users need to do, that is Searching Twitter.

But to do so they have to navigate to the speci开发者_StackOverflowfic page where the Juitter module is invoked which is something like:

____example.com/juitter

and THEN they have to type a query and hit search to have a beautiful AJAXed live search results page.

What if I want a simple text field in another page, let's say the HOME page: _____example.com/

from where a user can type their query in a "Search Twitter" input field and being taken to something like

____example.com/juitter/searched-query-here

and the correspondent Juitter page with results?

If you have a better approach than this, would you be so kind to advice me?

Many thanks.

Q.


Interesting module/functionality - and an obvious missing feature ;)

I played a bit with it in a local testing instance and was able to get a working result using the following approach:

  1. Add functionality to prepopulate the search box on the 'juitter' page with search terms from the URL, if present. In the modules juitter_page() function (in 'juitter.module'), replace this line (137):

    $search_phrase = t(variable_get('juitter_search_text', 'Search twitter:'));
    

    with this code:

    $preset = func_get_args();
    if (!empty($preset)) {
      $search_phrase = implode(' ', $preset);
    }
    else {
      $search_phrase = t(variable_get('juitter_search_text', 'Search twitter:'));
    }
    

    With this change, a call to 'juitter/foo' will bring up the standard juitter page, but with 'foo' in the search box instead of the default search text. Calling 'juitter/foo/bar' will result in 'foo bar' in the search box, while a call to 'juitter' alone will behave as before.

  2. Adjust javascript to autosubmit the search, if it got prepopulated by the change above. In the modules 'juitter.js' file, remove the following line (22):

    juitter.get_tweets($('#juitterTabsWrapper a:first').attr('id'));
    

    and add this code at the end of the Drupal.behaviors.Juitter function (under the 'end of search box comment' - /* /Search box */, line 53) instead:

    /* Trigger default. Use search term, if not default, otherwise use first tab */
    if (juitter.search && $(".juitterSearch").val()!=Drupal.t(juitter.search.text)) {
      $("#juitterSearch").submit();
    }
    else {
      juitter.get_tweets($('#juitterTabsWrapper a:first').attr('id'));
    }
    

    This will check if the default search box text has been replaced - if yes, it will submit the search, otherwise it will behave as before and search according to the default tabs configuration.

  3. With these changes in place, all that's needed is a search form similar to Drupals search block, redirecting to the juitter URL with the search terms turned into path elements. Add the following at the end of the juitter.module file:

    /**
     * Implementation of hook_block().
     */
    function juitter_block($op = 'list', $delta = 0) {
      if ($op == 'list') {
        $blocks[0]['info'] = t('Juitter search form');
        // Not worth caching.
        $blocks[0]['cache'] = BLOCK_NO_CACHE;
        return $blocks;
      }
      else if ($op == 'view' && user_access('access content')) {
        $block['content'] = drupal_get_form('juitter_search_block_form');
        $block['subject'] = t('Search Twitter');
        return $block;
      }
    }
    
    /**
     * Callback function to generate the juitter search form
     *
     */
    function juitter_search_block_form($form_state) {
      $form['search_terms'] = array(
        '#title' => t('Search Twitter'),
        '#type' => 'textfield',
        '#size' => 15,
        '#default_value' => '',
        '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
      );
      $form['submit'] = array('#type' => 'submit', '#value' => t('Search'));
      $form['#submit'][] = 'juitter_search_block_form_submit';
    
      return $form;
    }
    
    /**
     * Process a juitter search form submission.
     */
    function juitter_search_block_form_submit($form, &$form_state) {
      // The search form relies on control of the redirect destination for its
      // functionality, so we override any static destination set in the request,
      // for example by drupal_access_denied() or drupal_not_found()
      // (see http://drupal.org/node/292565).
      if (isset($_REQUEST['destination'])) {
        unset($_REQUEST['destination']);
      }
      if (isset($_REQUEST['edit']['destination'])) {
        unset($_REQUEST['edit']['destination']);
      }
    
      $form_id = $form['form_id']['#value'];
      $form_state['redirect'] = 'juitter/'. trim($form_state['values']['search_terms']);
    }
    

    This will create a 'Juitter search form' block that you can place as you see fit.

That's all ;)

NOTE: Normally I would implement changes like this 'from outside' by building a separate module, but since this is already a feature request on the module itself, I edited the modules code directly. I'll try to roll this into a patch and submit it there. Patch is available here, thanks to theunraveler.

BTW, I wouldn't usually code up complete solutions like this for an answer - it's just that this module got me interested. So as you seem to be a new user here on SO, do not expect this as the norm ;)


Edit: There is a tiny flaw in this approach, as the module also registers the path 'juitter/ahah' for a javascript callback, used from the administration form. So if a user enters 'ahah' into the new search box, he will get a useless json encoded form element as a result. The obvious solution to this would be to change the ahah callback path to something else. As a workaround, you could add an escape mechanism when redirecting from the form, undoing the escaping when putting the text in the main search box.


I rolled Henrik's solution into a patch, available here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜