簡體   English   中英

在Drupal 7中以編程方式對字段集合進行排序

[英]Sorting Field Collection Programmatically in Drupal 7

保存節點后,如何通過編程方式更改字段集合中項目的順序/權重? 任何幫助表示贊賞。 謝謝。

已經找到答案了。 我正在使用hook_node_presave ,然后對字段集合數組重新排序。 以前,我使用hook_node_update ,但是沒有用。

這是一個代碼片段,用於在保存節點之前按其任何字段對字段集合進行排序:

 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']; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM