在drupal6中为模块创建一个php页面视图

时间:2013-05-14 11:02:49

标签: php drupal drupal-6

我在drupal6工作,是drupal的新手。我在drupal网站上创建了一个自定义的帐户模块。在drupal中,设计集成的方式很困难,因为它们正在显示它们从回调函数返回的内容。但我的问题是我可以创建一个自定义的PHP页面并将该页面显示为我的模块视图页面吗?这有可能吗?

1 个答案:

答案 0 :(得分:0)

首先,您必须实施hook_menu,例如这样:

function YOURMODULE_menu()
{
  $items = array(); 
  // this would create a Menuitem in Adminmenu, but you can specify an other url for different locations
  $items["admin/settings/YOURMODULE"] = array(
    "title"                => "Translateable Menu Name",
    "access arguments"     => array("access administration pages"),
    "page callback"        => "drupal_get_form", // function, that will be called for this menuitem
    "page arguments"       => array("YOURMODULE_menu_admin"), // Argument givven to the page_callback function
    "type"                 => MENU_NORMAL_ITEM,
  );
  return $items;
}

有关详情,请参阅hook_menu | Drupal 6 | Drupal API。 在第一个示例中,您的函数将从drupal_get_form调用,您可以在其中创建一些表单:

function YOURMODULE_menu_admin()
{
  // YOURMODULE_element_name is the name of the form element
  $form['YOURMODULE_element_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Title of the textfield'),
    '#default_value' => variable_get('YOURMODULE_element_name', ""),
    '#size' => 2,
    '#maxlength' => 2,
    '#description' => t("Description of your formfield."),
    '#required' => TRUE,
  );
  return system_settings_form($form); 
}

Ant如果你想用自己的内容创建一个完整的自定义页面,你可以在hook_menu中指定你自己的回调函数

function YOURMODULE_menu()
{
  $items = array(); 
  // this would create a Menuitem in Adminmenu, but you can specify an other url for different locations
  $items["YOUR_CUSTOM_PATH"] = array(
    "title"                => "Translateable Menu Name",
    "access arguments"     => array("access custom module"), // you can define your own permission here
    "page callback"        => "your_custom_page", // function, that will be called for this menuitem
    "type"                 => MENU_NORMAL_ITEM,
  );
  $items["YOUR_CUSTOM_PATH_FOR_IMAGE"] = array(
    "title"                => "An image output page",
    "access arguments"     => array("access custom module"), // you can define your own permission here
    "page callback"        => "your_custom_image", // function, that will be called for this menuitem
    "type"                 => MENU_NORMAL_ITEM,
  );
  return $items;
}

然后你可以自由地实现这个功能,甚至可以输出定义自己的头文件的二进制数据。

function your_custom_page()
{
  echo "this is my custom page";
}


function your_custom_image()
{
  drupal_set_header("Content-Type: image/png");

  $im = @imagecreate(110, 20);
  $background_color = imagecolorallocate($im, 0, 0, 0);
  $text_color = imagecolorallocate($im, 233, 14, 91);
  imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
  imagepng($im);
  imagedestroy($im);

  // Exit here, because we don`t want the drupal footer in out Image output
  if(function_exists("drupal_exit")) drupal_exit();
  else
  {
    // Allow modules to react to the end of the page request before redirecting.
    // We do not want this while running update.php.
    if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
      module_invoke_all('exit', $url);
    }
    exit;
  }
}