如何查找包含特定字段的所有节点类型


How to find all nodetypes containing a particular field?

如何找到包含特定字段的所有节点类型?

假设我有两个节点类型,"mybio"answers"myresume",两个节点类型都包含一个字段"myphoto"。是否有一个函数将我的字段名称作为输入("myphoto")并返回包含此字段("mybio"answers"myresume")的节点类型的名称?

我不认为有一个函数,但看看CCK的数据库结构像这样的东西会做的技巧:

function content_field_get_node_types($field_name) {
  $query = db_query("SELECT DISTINCT type_name FROM {content_node_field_instance} WHERE field_name = '%s'", $field_name);
  $node_types = array();
  while ($row = db_fetch_array($query)) {
    $node_types[] = $row['type_name'];
  }
  return $node_types;
}

CCK提供了一个管理页面,您可以在其中看到内容类型和字段之间的关联:administration> content types> fields (path: www.example.com/admin/content/types/fields)

如果你看一下源代码,你会发现在这个函数中,content_fields_list() (sites'all'modules'cck'includes'content.admin.inc)就是奇迹发生的地方。

您可以在这里查看源代码中的SQL: http://drupalcontrib.org/api/drupal/contributions--cck--includes--content.admin.inc/function/content_fields_list/6