【问题标题】:Drupal: Completely override search functionality but use Drupal's template system (from a module)Drupal:完全覆盖搜索功能,但使用 Drupal 的模板系统(来自模块)
【发布时间】:2013-03-26 10:36:01
【问题描述】:

我正在编写一个自定义 Drupal 7 模块,它将完全覆盖网站的搜索页面和搜索方法。这是我目前所拥有的:

/**
 * Custom search.
 */
function mymodule_search_page() {
  drupal_add_css('css/search.css');

  // Perform a search (not important how)
  $result = do_custom_search('foo');

  return '<p>Results:</p>';
}

现在,如您所见,它还没有完成。我不知道如何从中正确返回结构化 HTML。我将如何使用 Drupal 的内置模板系统来呈现结果?

【问题讨论】:

标签: drupal drupal-7 drupal-modules drupal-theming


【解决方案1】:

你必须使用drupal 的内置函数。我希望你正在寻找这样的东西http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_render/7

【讨论】:

  • 看起来不错...但是你能告诉我一个在我的情况下如何使用drupal_render 的例子吗?我不确定是否应该将它与主题模板覆盖或我的数组应该包含的内容结合起来。
  • 你有任何关于drupal编码的先验知识吗?如果不是,我希望你通过这个drupal.org/developing/modules
  • 是的,我已经制作了几个较小的网站,但我从未制作过自定义的 Drupal 模块。我对 Drupal 7 也比较陌生,我之前只用过 6。
【解决方案2】:

这就是我最终做的:

/**
 * Implements hook_menu().
 */
function mymodule_search_menu() {
  $items = array();
  $items['search'] = array('page callback' => 'mymodule_search_page',
                       'access callback' => TRUE);
  return $items;
}

/**
 * Mymodule search page callback.
 */
function mymodule_search_page() {
  $variables = array();

  // Add stuff to $variables.  This is the "context" of the file,
  // e.g. if you add "foo" => "bar", variable $foo will have value
  // "bar".
  ...

  // This works together with `mymodule_search_theme'.
  return theme('mymodule_search_foo', $variables);
}

/**
 * Idea stolen from: http://api.drupal.org/comment/26824#comment-26824
 *
 * This will use the template file custompage.tpl.php in the same
 * directory as this file.
 */
function mymodule_search_theme() {
  return array ('mymodule_search_foo' =>
                array('template' => 'custompage',
                      'arguments' => array()));
}

希望这对某人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    相关资源
    最近更新 更多