-1

我需要在主页上创建一个区域,公司总裁可以在其中显示会经常更改的消息。我不确定是否应该为此使用块或某种内容类型。秘书将被分配更新消息的任务,所以我不确定我是否希望她弄乱一个块。创建新的内容类型需要一个需要嵌入主页的内容实例。主页使用模板文件,因此我什至可能需要添加一些 PHP 代码来控制内容的显示位置。

或者我应该创建一个自定义视图来完成这个?

4

1 回答 1

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;
}
于 2012-05-23T19:58:09.480 回答