【问题标题】:Drupal 6 - How to implement a form element of (CCK) type 'nodereference_autocomplete'?Drupal 6 - 如何实现(CCK)类型'nodereference_autocomplete'的表单元素?
【发布时间】:2012-09-13 01:06:30
【问题描述】:

为了寻找一个看似简单问题的答案,花了几个小时在 Drupal 社区页面上搜索,但到目前为止没有任何结果,因此希望您能提供帮助!

谁能描述如何在自定义表单中使用 FAPI 实现“nodereference_autocomplete”类型的输入元素?对于初学者来说,这是一个 AJAX 装饰的文本字段,它在 CCK 模块提供的匹配引用节点的字段上自动完成。我想在我自己的 Drupal 6 模块中利用这个功能。

提交的值必须是被引用节点的nid。此外,非常感谢有关将自动完成路径限制为仅包含“文章”和“博客文章”类型节点的说明。

感谢您对这个最基本的问题的帮助!

【问题讨论】:

    标签: drupal drupal-6 drupal-modules drupal-fapi drupal-forms


    【解决方案1】:

    我相信,由于您没有直接使用 CCK,因此您需要在模拟 CCK 行为的自定义模块中编写一些代码。您可以使用 FAPI 的自动完成功能。

    您的 form_alter 或表单定义代码可能如下所示:

    $form['item'] = array(   
      '#title' => t('My autocomplete'),
      '#type' => 'textfield',   
      '#autocomplete_path' => 'custom_node/autocomplete'  
    ); 
    

    由于您需要按节点类型进行限制,您可能还需要创建自己的自动完成回调。看起来像这样:

    function custom_node_autocomplete_menu() {
      $items = array();
      $items['custom_node/autocomplete'] = array(
          'title' => '',
          'page callback' => 'custom_node_autocomplete_callback',
          'access arguments' => array('access content'),
          'type' => MENU_CALLBACK,
        );
      return $items;
    }
    
    function custom_node_autocomplete_callback($string = '') {
      $matches = array();
      if ($string) {
        $result = db_query_range("SELECT title, nid FROM {node} WHERE type IN('article', 'blogpost') AND LOWER(title) LIKE LOWER('%s%%')", $string, 0, 5);
        while ($data = db_fetch_object($result)) {
          $matches[$data->title] = check_plain($data->title) . " [nid:" . $data->nid . "]";
        }
      }
      print drupal_to_js($matches);
      drupal_exit();
    }
    

    然后您需要编写代码以从提交的值中提取节点 ID。这是 CCK 用来执行此操作的代码:

    preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $value, $matches);
    if (!empty($matches)) {
      // Explicit [nid:n].
      list(, $title, $nid) = $matches;
      if (!empty($title) && ($n = node_load($nid)) && trim($title) != trim($n->title)) {
        form_error($element[$field_key], t('%name: title mismatch. Please check your selection.', array('%name' => t($field['widget']['label']))));
      }
    }
    else {
      // No explicit nid.
      $reference = _nodereference_potential_references($field, $value, 'equals', NULL, 1);
      if (empty($reference)) {
        form_error($element[$field_key], t('%name: found no valid post with that title.', array('%name' => t($field['widget']['label']))));
      }
      else {
        // TODO:
        // the best thing would be to present the user with an additional form,
        // allowing the user to choose between valid candidates with the same title
        // ATM, we pick the first matching candidate...
        $nid = key($reference);
      }
    }
    

    【讨论】:

    • 感谢您的大力帮助!编写我自己的自动完成回调,就像你提供的一样,是我最终要做的。效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多