メインコンテンツに移動

メインナビゲーション

  • ホーム
  • サイトマップ
  • ビデオ
  • ご連絡

パンくず

  • ホーム
  • Drupalのモジュール(Feeds SQL)の実行エラー

Drupalのモジュール(Feeds SQL)の実行エラー

drupal
bug
module usage

問題点:モジュール(Feeds)でデータインポートする際にエラーが発生

  • 環境: Drupal7.54、Feeds7.x-3.0、Feeds SQL7.X-1.0
  • Feedsでのデータインポート時に以下のエラーが発生
    Warning: substr() expects parameter 1 to be string, array given FeedsFetcherResult->sanitizeRaw() (/virtual/drills/public_html/drupal7/sites/all/modules/feeds/plugins/FeedsFetcher.inc ファイル 75行).

    モジュール(Feeds SQL)実行エラー

バグ報告とパッチ

  • ネットで調べてみたが、ほかのところでも同じエラーも発生しました(Feeds Fetcher sanitizeRaw() method expects string)
  • パッチも提供され、パッチ当てをすれば、エラーは消えます
drupal
development
entity type

やりたいこと:複数のEntityreference値をコンテンツにインポート

  • 以下の実例:単語と例文
    EntityReferenceモジュールで一つ単語に複数の例文を参照
  • 構成は:単語コンテンツタイプ、例文コンテンツタイプをそれぞれ作成します
  • 単語コンテンツタイプにEnityreferenceで例文コンテンツタイプを参照します
  • Enityreferenceに関する詳細は記事:「 Drupalのモジュール(Entity Reference)で作成した記事からほかの記事への参照リンク作成 」を参考にしてください
  • 二つのテーブルにあるデータはそれぞれインポートされたものので、関連付けはありません
  • 単語に複数の例文参照できるようにする場合以下のステップが必要
    • 単語を用いて例文テーブルで単語が含まる例文をすべて取得
    • 取得した例文を単語IDをコンテンツにEntityreferenceフィールドに保存

実現手法:FeedsAPIを利用してカスタムモジュールを作成して、単語に複数の例文参照データをインポートします

  • 単一のEntityreference値をコンテンツにインポートするサブモジュールがあります( FeedsモジュールでEnityにデータインポート )
  • 複数のEnityreference値をコンテンツにインポートするサブモジュールはありません
  • ここで、FeedsAPIを利用して、カスタムモジュールを作成することにします
  • 必要なモジュール:Feeds SQL7.x-1.0
    • 単語テーブルをスキャンして、単語のIDと単語を取得します
  • カスタムモジュール(hook_feeds_processor_targets_alter)で各単語の例文を探して、単語のEntityreferenceフィールドにセットします
    • hook_feeds_processor_targets_alterを実装して、ノードプロセッサーのマッピングターゲットに専用フィールドを追加します
      /**
       * Implements hook_feeds_processor_targets_alter().
       */
      function my_module_feeds_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle)
      {
        if( $entity_type=="node"&&$bundle=="chinese_word"){
          $field_info = field_info_instances("node", "chinese_word") ;
          if( array_key_exists('field_ch_word_sentences', $field_info ) &&
              field_info_field( 'field_ch_word_sentences' )['type'] == "entityreference" ){
            $targets["field_ch_word_sentences:drills_hander"] = array(
              'name' => check_plain( $field_info['field_ch_word_sentences']['label'] ).t(' (Import multiple value instance field)'),
              'callback' => 'drills_custom_feeds_set_target_entity_reference',
              'description' => t('This filed for converting Chinese Examples to word reference only.Do not use it for other fields!!'),
            );
          }
        }
      }
      
    • Feedsの管理画面(ホーム » 管理 » サイト構築 » Feedsインポーター)で上記コードで追加されたフィールド(
      例句 (Import multiple value instance field) (field_ch_word_sentences:drills_hander)
      )は以下のようカスタムモジュールがFeedsAPIを利用してマッピングターゲットに専用のフィールドを作成
    • 単語を用いて例文を検索、取得して、単語コンテンツにあるEntityreferenceフィールドにセット(専用フィールドのコールバック関数:drills_custom_feeds_set_target_entity_reference)
      function drills_custom_feeds_set_target_entity_reference($source, &$entity, $target, $value, $mapping){
        if (empty($value)) {
          return;
        }
        $target = str_replace(':drills_hander', '', $target);
        $query = db_query("select title, nid, CHAR_LENGTH(title) as char_num from node where title like '%".$value[0]."%' and type='example_sentences'  order by char_num limit 100");
        $sentences = $query->fetchAll();
        $field = array();
        foreach( $sentences as $i => $sentence ){
          $field[LANGUAGE_NONE][$i]['target_id'] = $sentence->nid;
        }
        $entity->{$target} = $field;
      }

       

ホーム

古松

検索

Article Category

  • apache(7)
  • css(19)
  • drupal(295)
  • Electron(4)
  • html(34)
  • javascript(27)
  • laravel(4)
  • linux(5)
  • macOS(2)
  • mysql(13)
  • php(19)
  • python(4)
  • SEO(12)
  • video(72)
  • Visual Studio Code(4)
  • windows(13)
  • wordpress(32)