0

ノードが保存されたときに、フィールド コレクション内の項目の順序/重みをプログラムで変更する方法は? どんな助けでも大歓迎です。ありがとうございました。

4

2 に答える 2

0

すでに答えを見つけました。hook_node_presaveフィールドコレクション配列を使用してから並べ替えています。以前は使用hook_node_updateしていましたが、動作しませんでした。

于 2014-10-22T10:02:22.070 に答える
0

これは、ノードを保存する前にフィールド コレクションをそのフィールドのいずれかでソートするためのコード スニペットです。

function your_module_node_presave($node){
  if ($node->type == 'foo') {
    // We must sort by this field collection field
    if (!empty($node->field_to_sort_by)) {

      // Get the values of the sorting field. We must load the fc items for this.
      $items = field_get_items('node', $node, 'field_to_sort_by');
      foreach ($items as $item) {
        $fc[] = field_collection_field_get_entity($item);
      }

      // field collection fields on nodes only contains 'value' and 'revision_id'.
      // We temporarily add the field to sort by, 
      // for using the convenient uasort() function over the array.
      $tmp_array = $node->field_to_sort_by[LANGUAGE_NONE];
      foreach ($tmp_array as $key => $item) {
        $tmp_array[$key]['sortfield'] = $fc[$key]->field_to_sort_by[LANGUAGE_NONE][0]['value'];
      }

      // Now we sort the node's field array using uasort().
      usort($tmp_array, 'my_module_sortByField_asc');

      // unset the sorting field before updating node's field collection
      foreach ($tmp_array as $key => $item) {
        unset($tmp_array[$key]['sortfield']);
      }

      $node->field_to_sort_by[LANGUAGE_NONE] = $tmp_array;
    }

  }
}

function my_module_sortByField_asc($a, $b) {
  return $a['sortfield'] - $b['sortfield'];
}

于 2015-05-17T15:30:49.383 に答える