メインコンテンツに移動

メインナビゲーション

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

パンくず

  • ホーム
  • hook_views_data_alter使用時の注意点

hook_views_data_alter使用時の注意点

drupal
views
customization

Viewsの「リレーションシップ」にフィル―ドを簡単に追加

  • モジュール:Viewsにフィル―ドを「リレーションシップ」(QueryでテーブルのJOIN)、「前後関係からみたフィルター」(ユーザーID、URLのパラメータなどの環境要素フィルター)に追加したりすることが、hook_views_data_alterを使えば比較簡単的にできます。このカスタマイズがいろいろなウェブページで紹介されています。例えばDrupal本家のAPI紹介画面で「リレーションシップ」追加のサンプルがあります。
     // This example adds a relationship to table {foo}, so that 'foo' views can
      // add this table using a relationship. Because we don't want to write over
      // the primary key field definition for the {foo}.fid field, we use a dummy
      // field name as the key.
      $data['foo']['dummy_name'] = array(
        'title' => t('Example relationship'),
        'help' => t('Example help'),
        'relationship' => array(
          'base' => 'example_table', // Table we're joining to.
          'base field' => 'eid', // Field on the joined table.
          'field' => 'fid', // Real field name on the 'foo' table.
          'handler' => 'views_handler_relationship',
          'label' => t('Default label for relationship'),
          'title' => t('Title seen when adding relationship'),
          'help' => t('More information about relationship.'),
        ),
      );
  • 例として、以下のEntityオブジェクトのViewsのリレーションシップにフィールドを追加します
    • Entity Construction Kit(7.x-2.0-rc8)であるEntityタイプ(例:練習結果)を作成します
    • EntityのBundleを一つ作成します
    • Viewsで上記Entityオブジェクトの検索、出力ができますが、「リレーションシップ」などの関連付けはできません(Bundleのフィールドが汎用なので、どのテーブルとJOINするのは、事前にわからないです)
  • 上記サンプルコードを利用して、「リレーションシップ」にフィールドを追加します
        $data['field_data_field_er_question_answer_id']['field_er_question_answer_id_value']= array(
            'title' => t('Question ID'),
            'help' => t('The ID of 4-selection-questions data'),
            'relationship' => array(
                'base' => 'node',
                'base field' => 'nid' ,
                'label' => t('node id related to question id'),
                'handler' => 'views_handler_relationship',
                'title' => t('nid'),
                'help' => t('The question id of the 4-selection-questions'),
            ),
        );

「リレーションシップ」の表示にエラーが発生

  • 上記コードをカスタムモジュールについてして、システムのキャッシュをクリアします
  • Viewsの管理画面に戻って、追加したフィールドを「リレーションシップ」設定画面で追加しようと思ったが、いくつかエラーが発生しています
  • エラーを無視して、「リレーションシップ」に追加したフィールドを利用して、無理矢理に選択して、テーブルをJOINします。
    • 一応、正常に動作はします
    • 「リレーションシップ」の設定に何かの間違いがあるようです

関数dsm(Develモジュール)で問題を特定します

  • モジュールDevelを有効して、エラーを発生する場所にその値を観察してみることにしました
    • エラー発生場所:C:\develop\sources\php\ligare\sites\all\modules\views\includes\handlers.inc の277行
    • 上記277行の前に、dsm=($this); を追加して、$thisの値を確認します
  • いろいろのhandlerの値がViewsの管理画面に現れました:
  • エラーが発生した変数:definationに「group」変数が定義されていないようです。
  • hook_views_data_alterの「relationship」配列に、「group」の定義を追加します
        $data['field_data_field_er_question_answer_id']['field_er_question_answer_id_value']= array(
            'title' => t('Question ID'),
            'help' => t('The ID of 4-selection-questions data'),
            'relationship' => array(
                'base' => 'node',
                'base field' => 'nid' ,
                'label' => t('node id related to question id'),
                'handler' => 'views_handler_relationship',
                'title' => t('nid'),
                'group' => t('4 Options Question'),
                'help' => t('The question id of the 4-selection-questions'),
            ),
        );
  • システムのキャッシュをクリアして、Viewsの管理画面に戻ってみたら、問題の解決ができました
ホーム

古松

検索

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)