2015-01-16 58 views
0

所以我想讓我的模塊首先創建頁面。但是簡單頁面的鏈接應該是用戶輸入的。 我正在Drupal 7上做這件事。 我在_menu函數中做了一個設置表單。用Drupal模塊動態創建一個自定義頁面的鏈接

$items['admin/config/content/settings'] = array(
      'title' => 'Setings', 
      'description' => 'A form to change settings', 
      'page callback' => 'drupal_get_form', 
      'page arguments' => array('settings_form'), 
      'access arguments' => array('access administration pages'), 
      'type' => MENU_NORMAL_ITEM, 
); 

然後我說這個:

function settings_form($form, &$form_state){ 
    $form['locationUrl'] = array(
     '#type' => 'textfield', 
     '#title' => t("URL kādu vēlaties?"), 
     '#description' => t("Example. about"), 
     '#required' => true 
    ); 
    return system_settings_form($form); 
} 

然後我加入到_menu功能:

$items[variable_get('admin/config/content/settings','locationUrl')] = array(//this creates a URL that will call this form at "examples/form-example" 
    'title' => '', //page title 
    'page callback' => 'DGMap', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form 
    'access arguments' => array('access simple page'), 
); 

而且我補充一點:

function page(){ 
    return array('#markup' => '<h1>Hello?</h1>'); 
} 

我救它所有的。清除緩存。然後轉到設置並保存locationUrl,例如'about'。然後嘗試/ drupal /關於它給我的頁面沒有發現異常。

任何人都可以請幫助我嗎?我希望這是可以理解的即時通訊設法。

感謝您的幫助。

我想要做的最終目的是讓我的模塊可以創建一個帶有自定義JavaScript的頁面。如果任何人都可以將我鏈接到一個非常棒的教程,我會非常感激。如果我的模塊有一種方法可以在頁面中放置自定義創建的JavaScript,那也可以。

回答

0

不確定動態路由是否是一個很好的方法,但是您在hook_menu()中得到的第二個鏈接錯誤。事實上,你的路線是YourDrupal/locationUrl,而不是你的示例YourDrupal/about

使用此第二個菜單鏈接(與上設置是否額外的檢查被保存):

if (!empty($setting = variable_get('locationUrl', ''))) { 
    $items[$setting] = array( 
     'title' => '', //page title 
     'page callback' => 'page', // use the same name as your pagecallback 
     // 'access arguments' => array('access simple page'), 
     'access arguments' => array('access content'), // temporarily use this 
    ); 
} 

您可能也有必要在現有航線的支票。不知道如果用戶用locationUrl保存了您的表單,例如「admin」給出了已存在的YourDrupal/admin的鏈接,會發生什麼情況。

+0

它的工作!我唯一應該補充的是,在完成配置保存部分之後,我不得不再次清除緩存。非常感謝您的幫助! :)你有什麼想法我怎麼可以自動化呢?以便保存後清除緩存。 – DaveLV

+0

'cache_clear_all();'可能會在提交處理程序中執行此操作。但是,我認爲你不得不放棄由'system_settings_form($ form)提供的自動化;'編寫自定義表單來保存你的配置。 ** DrupalStackExchange **用戶可以給你更好的答案:) –

+0

感謝您的幫助! :) – DaveLV

相關問題