Drupal7:从特性/模块的自定义内容类型字段中检索数据


Drupal 7: retrieving data from fields of custom content type for a feature/module

提前感谢您的帮助!我的Drupal项目有一个名为"Gear Post"的自定义内容类型,可以让我查看自行车装备。Gear Post有一个名为"weight"的字段,用于表示物品的重量。我正在尝试编写我自己的模块,也就是功能,允许用户计算存储在网站上作为gear Post的所有装备项目的重量。我正在努力获得齿轮柱的重量特性。

到目前为止,我已经做到了:(.module文件)

<?php
 function get_content_fields_array() {
 $list = node_type_get_names(); //just for info
 echo $list['gear_post']; //this correctly gets 'Gear Post'
 $field_list = field_info_instances('node', 'gear_post');
 $allFieldKeys = array_keys($field_list);
 echo ($allFieldKeys[4]); //'field_weight'

我尝试使用array_keys,因为当我通过var_dump输出关联数组时,我发现它们很难读取。

我想做的是有一个所有权重的数组,这样我就可以迭代并显示它们,然后用它们进行数学计算。但我不知道如何获得每个Gear Post的权重字段的实际值。

非常感谢-我是Drupal的新手(第2天)

经过大量的尝试和摸索,我自己解决了这个问题。非常令人兴奋。不确定这是否是最佳实践,但它有效。这对弄清楚很有帮助!

function getWeights() {
 $query = new EntityFieldQuery();
 $query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'gear_post')
  ->propertyCondition('status', 1);
  $result = $query->execute();
if (!empty($result['node'])) {
  $nids = array_keys($result['node']);
  $nodes = node_load_multiple($nids);
  $weights = array();
   foreach ($nodes as $node) {
      $fields = field_view_field('node', $node, 'field_weight'); 
      array_push ($weights, $fields[0]['#markup']);
     }
 }
 return $weights;
}