【问题标题】:How to do get multiple field collection value in a drop-down of same node in Drupal 8?如何在 Drupal 8 的同一节点的下拉列表中获取多个字段集合值?
【发布时间】:2018-10-04 10:41:23
【问题描述】:

我有一个内容类型,并且我用字段组(水平选项卡)分隔了字段。有2个字段集合。我想从一个字段集合中获取字段值并设置为第二个字段集合中的选择列表。

【问题讨论】:

    标签: drupal-modules drupal-8


    【解决方案1】:

    您可以使用下面的代码来做同样的事情:这只是一个例子,请谨慎使用内容类型和字段集合的机器名称。

    $nid = 1234; //Dummy Value
    
    //loading node based on nid
    $node = \Drupal\node\Entity\Node::load($nid);  
    
    //Here I used field_collection_first_field as machine name of the field collection name on your content type
    if(!($node->get('field_collection_first_field')->isEmpty())){
    
        //Field collection 1
        $fcID1 = $node->get('field_collection_first_field')->getValue()[0]['value'];
    
        if(!empty($fcID1)){
            $fc1 = \Drupal\field_collection\Entity\FieldCollectionItem::load($fcID);
            $fcFieldValue = $fc1->get('field_comments')->getValue()[0]['value'];
        }
    }
    
    if(isset($fcFieldValue)){
    
        if(!($node->get('field_collection_second_field')->isEmpty())){
    
            //Field collection 2
            $fcID1 = $node->get('field_collection_second_field')->getValue()[0]['value'];
    
            if(!empty($fcID1)){
                $fc2 = \Drupal\field_collection\Entity\FieldCollectionItem::load($fcID);
    
                $fc->set('field_comments',$fcFieldValue);
    
                if(!($fc->save())){
                    echo 'success'; //Work Done
                }
            }
        }
    }
    

    如果您遇到任何其他问题,请告诉我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2021-06-04
      • 1970-01-01
      • 2018-05-29
      • 2020-04-30
      相关资源
      最近更新 更多