しばらく前、Drupalでサイト検索を整理し、素材の種類によるフィルタリングとユーザープロファイルによる検索機能を整理する必要がありました。 既存のソリューションは、多くの理由で適合しませんでした。 2つのオプションがすぐに思い浮かびました。検索を作成する(phpMorphyなどの既成のライブラリを使用する)か、Googleを使用します。 2番目に停止しました。
ロジックは単純です:Googleでは、特定のサイトで検索できます。 つまり、
www.example.com 、
www.example.com/first 、
www.example.com/secondなどで検索できます。 特定のタイプのマテリアルを検索するには、異なるタイプのマテリアルに異なるパスを設定し、それらを
Googleカスタム検索に埋め込むだけです。
次に、その手順を簡単に説明します(多くのコード、写真なし)。
- Pathautoモジュールをオンにします( Tokenモジュールをプルします)
- admin / build / path / pathautoページで、さまざまな種類のマテリアルに異なるパスを構成します(これは検索が正しく機能するために必要です)
- 同じ場所で、サイト上のすべての素材の同義語を大幅に再フォーマットします
- Googleカスタム検索モジュール
- メニューをフックします(以降、すべてのコードを提供するのではなく、主要なポイントのみを提供します)
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
function google_custom_search_menu() { // $config = variable_get( "google_custom_search" ,array()); $pagelink = "search/google-custom-search" ; $type = MENU_LOCAL_TASK; if ($config[ 'page' ]) { $pagelink = $config[ 'page' ]; $type = MENU_SUGGESTED_ITEM; } $items[$pagelink] = array( 'title' => ($config[ 'pagename' ]?$config[ 'pagename' ]: 'Google Custom Search' ), 'page callback' => 'google_custom_search_page' , 'page arguments' => array($config), 'access arguments' => array( 'use google custom search' ), 'type' => $type, ); return $items; } * This source code was highlighted with Source Code Highlighter .
- 材料の種類を選択します
- 関数google_custom_search_main_form(){
- // Pathautoを接続します
- _pathauto_include();
- // Pathauto設定
- $ all_settings = module_invoke_all( 'pathauto' 、 'settings' );
- //材料タイプ
- $ types = node_get_types( 'types' );
- //ループでは、利用可能なすべての種類の素材、ブログ、ユーザーを取得します
- foreach ($設定としての $ all_settings){
- $ module = $ settings-> module;
- スイッチ ($モジュール){
- ケース 「ユーザー」 :
- // ...
- 休憩 ;
- ケース 「ノード」 :
- foreach ($ settings-> patternitems as $ itemname => $ itemlabel){
- // ...
- }
- 休憩 ;
- ケース 「ブログ」 :
- // ...
- 休憩 ;
- }
- }
- //フォーム上のすべてを表示します
- $ form = array();
- $ form [ 'materials' ] = array(
- '#type' => 'fieldset' 、
- '#title' => '高度な検索' 、
- '#collapsible' => TRUE、
- '#collapsed' => TRUE、
- );
- foreach ($ final as $ i => $ v){
- $ form [ 'materials' ] [$ names [$ i]] = array(
- '#type' => 'チェックボックス' 、
- '#title' => $ v、
- '#default_value' => 0、
- '#name' => $ i、
- '#attributes' =>配列( 'class' => 'google_custom_search_checkbox' )、
- );
- }
- $フォームを返します。
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
- 検索ページを形成します
- 関数google_custom_search_page($ config){
- if (!$ config){
- drupal_set_message( "Googleカスタム検索モジュールが設定されていません。管理者に連絡してください。" 、 '警告' );
- FALSEを返します。
- }
- $ output = drupal_get_form( 'google_custom_search_main_form' );
- $ key = $ config [ 'key' ];
- $ host = $ _SERVER [ 'HTTP_HOST' ];
- if ($ config [ 'host' ]){
- $ host = $ config [ 'host' ];
- }
- $ style = $ config [ 'style' ];
- $ string = isset($ _ POST [ 'google_custom_search_string' ])? htmlspecialchars($ _ POST [ 'google_custom_search_string' ]): "" ;
- $ output。= <<< THIS
- <script type = "text / javascript" src = "http://www.google.com/jsapi?key=$key" > </ script>
- <script type = "text / javascript" >
- google.load( "search" 、 "1" 、{ "nocss" : true });
- 関数OnLoad(){
- // Google CSEをダウンロードします
- searchHost = "$ host /" ;
- searchControl = new google.search.SearchControl();
- //出力のタイプを設定します
- searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
- var siteSearch = new google.search.WebSearch();
- siteSearch.setUserDefinedClassSuffix( "siteSearch" );
- siteSearch.setSiteRestriction(searchHost);
- //デフォルトでは、サイト全体を検索します
- siteSearch.setUserDefinedLabel( "すべての結果" );
- searchControl.addSearcher(siteSearch);
- var drawOptions = new google.search.DrawOptions();
- //モジュール設定に応じた出力スタイル(タブまたは次々)
- drawOptions.setDrawMode(google.search.SearchControl。$ style);
- searchControl.draw(document.getElementById( " searchcontrol " )、drawOptions);
- searchControl.execute( "$ string" );
- }
- //素材の種類を選択するときにGoogle CSEを更新します(素材の種類ごとに同じ、ただし異なるタブ)
- $( '.google_custom_search_checkbox' ).change(function(){
- searchControlを削除します。
- searchControl = new google.search.SearchControl();
- searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
- var siteSearch = new google.search.WebSearch();
- siteSearch.setUserDefinedClassSuffix( 'siteSearch' );
- siteSearch.setSiteRestriction(searchHost);
- siteSearch.setUserDefinedLabel( 'すべての結果' );
- searchControl.addSearcher(siteSearch);
- //選択したすべてのタイプを調べます
- $( '.google_custom_search_checkbox' ).each(function(){
- if ($( this ).attr( 'checked' )){
- var siteSearch = new google.search.WebSearch();
- siteSearch.setUserDefinedClassSuffix( 'siteSearch' );
- var newSearchName = $( this ).parent()。text()。replace(/ / g、 '' ).replace(/(^ \ s +)|(\ s + $)/ g、 '' );
- var newSearchHost = searchHost + $( this ).attr( 'name' ).replace(/ / g、 '' ).replace(/(^ \ s +)|(\ s + $)/ g、 '' );
- siteSearch.setSiteRestriction(newSearchHost);
- siteSearch.setUserDefinedLabel(newSearchName);
- searchControl.addSearcher(siteSearch);
- }
- });
- var drawOptions = new google.search.DrawOptions();
- drawOptions.setDrawMode(google.search.SearchControl。$ style);
- searchControl.draw(document.getElementById( ' searchcontrol ' )、drawOptions);
- });
- google.setOnLoadCallback(OnLoad、 true );
- </ script>
- これ;
- $出力を返します。
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
- モジュール設定ページなどの小さなものを作成します
ここまたは
ここから結果をダウンロードできます
。批判は大歓迎です。