简体   繁体   English

Drupal 7删除节点的字段集合

[英]Drupal 7 Delete field collection for node

I just found a script to programatically delete a field collection to a specific node : 我刚发现一个脚本以编程方式将字段集合删除到特定节点:

<?php
  $node = node_load(1);
  $field_collection_item_value = $node->field_page_collection1[LANGUAGE_NONE][0]['value']; // Take field collection item value.
  entity_delete_multiple('field_collection_item', array($field_collection_item_value)); // Delete field collection item.
?>

Unfortunately as i see it , it only delete first field collection, I need to select which one i want to delete. 不幸的是,我看到它,它只删除第一个字段集合,我需要选择我要删除的那个。

Here is my structure : 这是我的结构:

Multiple field collection who have : a reference to another node and two selects 多字段集合,具有:对另一个节点的引用和两个选择

I have the reference nid in the url so I can use it, but I don't have any idea how to select the right field collection with that. 我在url中有引用nid所以我可以使用它,但我不知道如何选择正确的字段集合。

Thanks 谢谢

Try to use this: 试着用这个:

$node = node_load(1);
$searhed_nid = '2';
$field_page_collection1 = field_get_items('node', $node, 'field_page_collection1');
foreach ($field_page_collection1 as $item) {
  $field_collection = entity_load_single('field_collection_item', $item['value']);
  $fc_item_wrapper = entity_metadata_wrapper('field_collection_item', $field_collection);
  // lets take name the field with ref field_ref_nid.
  $field_val = $fc_item_wrapper->field_ref_nid->raw();
  if ($field_val == $searhed_nid) {
    $field_collection->delete();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM