drupal 一个复杂的 profile 任务

我们的所有定制模块都已经安装了。但是在本书中,有好几个地方我们使用了管理接口来配置这些模块。

当然,我们希望在自己的安装大纲中自动完成这个过程。准确地说,我们要完成以下工作:

  • 创建我们在第四章中定义的 quote 内容类型;
  • 为第八章中定义的 sitenews_send_action 动作添加一个触发器,与 Node API 的 presave 事件相关联;
  • 让用户可以选择把 descartes 而不是 garland 设置为默认主题。

最后一步需要与用户进行交互,因此我们将利用任务系统来设计自己的定制任务。

与往常一样,我们先从整体上看一看这个函数。然后我们先介绍前面两点。实现自己的任务稍微有点复杂,我们稍后再详细介绍。
 

/**
 * Walk through final installation tasks
 */
function philosopherbios_profile_tasks(&$task, $url) {
  if ($task == 'profile') {
    // This is why we required default profile.
    default_profile_tasks(&$task, $url);
    // Quote content from chapter 4:
    $quote_type = array(
                        'name' => st('Quote'), // <- st() is t() for installer
                        'type' => 'quote',
                        'description' => st('Quotations and witticisms'),
                        'module' => 'node',
                        'has_title' => TRUE,
                        'title_label' => 'Origin',
                        'body_label' => 'Text',
                        'has_body' => TRUE,
                        'custom' => FALSE,
                        'modified' => TRUE,
                        'locked' => FALSE,
                        'is_new' => TRUE,
                        'help' => '',
                        'min_word_count' => 0,
                        );
    node_type_save((object)$quote_type);
    // Pre-configure our trigger from ch. 8:
    // (see trigger.admin.inc)
    $aid = 'sitenews_send_action';
    $hook = 'nodeapi';
    $op = 'presave';
    $sql = 'INSERT INTO {trigger_assignments} '
       ."VALUES ('%s', '%s', '%s', 1)";
    db_query($sql, $hook, $op, $aid);
    // Rebuild the menu
    menu_rebuild();
    // Do the form:
    $task = 'philosopherbios_pick_theme';
    $form = drupal_get_form('philosopherbios_theme_form', $url);
    return $form;
  }
  // Because of this, we must create
  // the philosopherbios_profile_task_list() function
  if ($task == 'philosopherbios_pick_theme') {
    $form = drupal_get_form('philosopherbios_theme_form', $url);
    // See if the form was processed:
    if (variable_get('philosopherbios_theme', FALSE)) {
      variable_del('philosopherbios_theme');
      $task = 'profile-finished';
    }
    else {
      return $form; // try again.
    }
  }
}

这个函数处理两项任务:profile 和 philosopherbios_pick_theme。第一个负责创建内容类型和新的触发器。这是由第一条 if 语句处理的。我们先研究它。

注解:用 if 还是 switch?
处理任务时,一般用 if 比用 switch 好些。为什么呢?因为我们需要这样一种能力,先用一条语句改变 $task 的值,然后让其它语句处理那个任务。用多重if 语句可以更简洁地做到这一点。

评论

发表新评论

此内容将保密,不会被其他人看见。
  • 允许HTML标签:<a> <img><em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p>
  • 自动断行和分段。

更多关於格式化选项的信息

Image CAPTCHA
验证码
|