2012-05-15 68 views
0

我需要在主頁上創建一個公司總裁可以顯示消息的區域,該消息將經常更改。我不確定是否應該爲此使用塊或某種內容類型。祕書將被分配更新信息的任務,所以我不確定我是否希望她弄糟一塊。創建新的內容類型需要在主頁中嵌入一個內容實例。主頁使用模板文件,所以我甚至可能需要添加一些PHP代碼來控制內容的顯示位置。Drupal可編輯頁面部分

或者我應該創建一個自定義視圖來完成此操作?

回答

0

我最近有一個類似的挑戰。客戶需要一種設置主頁上顯示的警報的方法。你的例子類似:

1)我不想給編輯權限編輯塊

2)只是爲了提醒創建內容類型似乎有點小題大做

的解決方案是創建一個簡單模塊執行以下操作:

1)創建一個形式輸入警報內容,並存儲在Drupal的「可變」表 (hook_admin())

2)創建一個管理該內容頁面來存放形式 (hook_menu())

3)定義要分配給可編輯的警報角色新的權限 (hook_perm())

4)將存儲在變量表中的值到page.tpl (hook_preprocess_page())

完整的模塊文件:

/** 
* Build the administration form which collects the following and store the values in the vars table: 
* 
* @toggle - whether or not an alert is active 
* @severity - Severity of the alert (low or high) 
* @title - The title of the Alert (usualy "Alert") 
* @content - Content to display on the homepage 
* @node - The associated Alert Node. The Read More link on the homepage will link to this node 
* 
*/ 
function alerts_admin() { 
    $form = array(); 

    $form['alerts_description'] = array (
    '#value' => '<p>Use this page to set an Alert. If the "Turn On Alert" checkbox is checked, a blue or red banner (depending on the Alert Severity) will be displayed on the homepage containing the copy defined here.', 
); 

    $form['alerts_toggle'] = array (
    '#type' => 'checkbox', 
    '#title' => 'Turn Alert On.', 
    '#default_value' => variable_get('alerts_toggle', 0), 
    '#size' => 2, 
    '#maxlength' => 2, 
    '#description' => t("Select this checkbox to turn on the Homepage Alert."), 
    '#required' => TRUE, 
); 

    $form['alerts_severity'] = array (
    '#type' => 'select', 
    '#title' => 'Severity', 
    '#options' => array (
     'low' => 'Low Severity - Blue', 
     'high' => 'High Severity - Red', 
    ), 
    '#default_value' => variable_get('alerts_severity', 'low'), 
    '#required' => TRUE, 
); 

    $form['alerts_title'] = array (
    '#type' => 'textfield', 
    '#title' => 'Title', 
    '#description' => 'Enter the title of the Alert (e.g. "Alert").', 
    '#default_value' => variable_get('alerts_title', 'Alert'), 
    '#required' => TRUE, 
); 

    $form['alerts_content'] = array (
    '#type' => 'textarea', 
    '#title' => 'Content', 
    '#default_value' => variable_get('alerts_content', 'Example alert content'), 
    '#required' => TRUE, 
); 

    //Build the options list for the associated node select list. We're just pulling a list of all the template6 content of the site. 
    $result = db_query("SELECT nid, title FROM {node} WHERE type = 'template6'"); 
    $i=0; 
    while ($row = db_fetch_array($result)) { 
    $key = $row['nid']; 
    $value = $row['title']; 
    $options[$key] = $value; 
    } 

    $form['alerts_node'] = array (
    '#type' => 'select', 
    '#title' => 'Associated Article', 
    '#options' => $options, 
    '#description' => 'The "Read More" link at the end of the alert will link to this Article.<p><strong>You must create the node first before you can select it here.</strong> ' . l('Click here', 'node/add/template6') . ' to create an article.', 
    '#default_value' => variable_get('alerts_node', ''), 
    '#required' => TRUE, 
); 

    return system_settings_form($form); 
} 

/** 
* Create the page for and link to the form 
*/ 
function alerts_menu() { 

    $items = array(); 

    $items['admin/settings/alerts'] = array(
    'title' => 'Site Alert Settings', 
    'description' => 'Toggle alerts on/off and configure the title and contents', 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('alerts_admin'), 
    'access arguments' => array('administer alerts settings'), 
    'type' => MENU_NORMAL_ITEM, 
    ); 

    return $items; 
} 

/** 
* Create a new perm to administer Site Alerts 
* 
* This permission can be given to any role 
*/ 
function alerts_perm() { 
    return array('administer alerts settings'); 
} 

function alerts_preprocess_page(&$vars) { 
    $alert = 0; 
    $banner_classes = array(); 
    $banner_classes[] = 'banner'; 
    if (variable_get('alerts_toggle', '0') == '1') { 
    $banner_classes[] = 'alert'; 
    $alert = 1; 
    $alert_content = array(
     'title' => variable_get('alerts_title', 'Alert'), 
     'content' =>variable_get('alerts_content', 'Example alert content'), 
     'node' => variable_get('alerts_node', ''), 
    ); 
    if (variable_get('alerts_severity', 'low') == 'high') { 
     $banner_classes[] = 'high'; 
    } 
    else $banner_classes[] = 'low'; 
    } 
    $banner_classes = implode(' ', $banner_classes); 

    $vars['banner_classes'] = $banner_classes; 
    $vars['alert'] = $alert; 
    $vars['alert_content'] = $alert_content; 
}