如何获得某个字段类型的字段?


13

我已经看到了如何通过实体和捆绑随取领域的名单field_info_instances()field_info_field(),但我没有看到一种方式来获得特定类型的所有领域。

我能够将其煮熟以做我想做的事情:

function field_get_fields_of_type($type) {
  $fields_info = field_info_instances();
  $return = array();
  foreach ($fields_info as $entity => $bundles) {
    foreach ($bundles as $bundle => $fields) {
      foreach ($fields as $field_name => $info) {
        $more_info_because_type_isnt_included = field_info_field($field_name);
        if ( $more_info_because_type_isnt_included['type'] == $type) {
          $return[$field_name] = $more_info_because_type_isnt_included;
        }
      }
    }
  }
  return $return;
}

dpm(field_get_fields_of_type('entityreference')); // prints out all entityreference fields

但这似乎需要大量代码,并且需要进行大量循环操作才能获得可能应通过现场API到达的内容。

有人知道Field API函数可以更有效地检索我要查找的数据吗?

Answers:


23

前几天,我碰到了这个问题,并且想起了这个问题,所以这里是一个非常未知的功能(我很高兴看到有人在博客或任何内容中提到它):

field_read_fields()

和伴随功能:

field_read_instances()

例如,获取所有分类术语字段:

$taxonomy_fields = field_read_fields(array('type' => 'taxonomy_term_reference'));

或获取机器名称为的字段的实例field_image

$image_field_instances = field_read_instances(array('field_name' => 'field_image'));
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.