web-dev-qa-db-ja.com

プログラムでフィールドコレクションの画像フィールドにどのようにデータを入力しますか?

フィードがうまく機能できないほど複雑な大規模なインポートを実行しようとしているので、カスタムインポートスクリプトを作成しています。フィールドコレクションを介して関連付けられている画像の読み込みを除いて、すべてが機能します。私はノードと分類法の参照を含むフィールドコレクションを問題なく持っていますが、画像フィールドには運がありません。

さまざまなメディアタイプのフィールドコレクションを参照するfield_signature_mediaというノードにフィールドがあります。フィールドコレクションには、画像を処理するためのfield_signature_imageがあります。

試行1:entity_metadata_wrapperを使用:

    // Image file created in earlier part of the process and loaded into $file
    // The node that's being worked on has been created in $node
    // Create the field collection
    $fc_item = entity_create('field_collection_item', array('field_name' => 'field_signature_media'));
    $fc_item->setHostEntity('node', $node);
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);

    $field = array('alt' => '',
          'title' => '',
          'fid' => $file->fid,
          'display' => 1,
          'width' => $file->image_dimensions['width'],
          'height' => $file->image_dimensions['height'],
          'image_field_caption' => array
              (
                  'value' => '',
                  'format' => 'full_html',
              ),              
    );

    $fc_wrapper->field_signature_image->set($field);
    $fc_wrapper->save(true);  // <-- at this point it enters infinite recursion.
    field_attach_update('node', $node);

ラッパーなしで2つの試行:

    $fc_item = entity_create('field_collection_item', array('field_name' => 'field_signature_media'));
    $fc_item->field_signature_image[LANGUAGE_NONE][] = 
                        array('alt' => '',
                              'title' => '',
                              'fid' => $file->fid,
                              'display' => 1,
                              'width' => $file->image_dimensions['width'],
                              'height' => $file->image_dimensions['height'],
                              'image_field_caption' => array
                                  (
                                      'value' => '',
                                      'format' => 'full_html',
                                  ),
                        );
    $fc_item->setHostEntity('node', $node);
    $fc_item->save(TRUE);
    field_attach_update('node', $node); // <-- At this point field_collection cannot find an array it was expecting.

ラッパーを使用してフィールドデータを手動で作成せずに3回試行します。

    $fc_item = entity_create('field_collection_item', array('field_name' => 'field_signature_media'));
    $fc_item->setHostEntity('node', $node);
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);  
    $fc_wrapper->field_signature_image->set($file->fid);
    $fc_wrapper->save(true);  // <-- at this point it enters infinite recursion.
    field_attach_update('node', $node);
2
acrosman

ノードのフィールドの設定とフィールドコレクションアイテムの違いはありません。

これはうまくいくはずです

_$fc_wrapper->field_signature_image->file->set($file);
_

これも機能するはずです

_$fc_wrapper->field_signature_image = (array) $file;
_

_$file_が有効なファイルオブジェクトであると仮定します。ファイルの追加に問題がある場合、通常はプロパティが不足していることが原因です。その場合、UIを介してノードとフィールドのコレクションアイテムを作成し、次にdpm()を使用して不足しているものを確認します。半分の時間は_$file->display_で、残りは通常_$file->description_です。私の推測では、_$file->description_が問題です。

私が覚えていないのは、 File Entity を使用してこれを変更するかどうかです。上記の両方の行は、このモジュールが無効になっているインポートスクリプトに基づいています。

2
mpdonadio