2

I try to configure a custom table in hook_views_data. I need to join the same table (node) twice. The problem is that i can't have the same key for 2 entries in my array :

$data['maz_composition']['table']['join'] = array(
    // Index this array by the table name to which this table refers.
    // 'left_field' is the primary key in the referenced table.
    // 'field' is the foreign key in this table.
    'node' => array(
        'left_field' => 'nid',
        'field' => 'aid',
    ),
);

Is there a solution for that ? Thanx and sorry for my bad english.

1

I know it is a bit late. But I had the same problem and ran into this page. So here is how I solved it. It is with my own case, but solution is generic, I hope it will help someone.

I join wootoon_cached_sets twice, the key in $data['whatever'] is only the alias in the query. The actual table is given by the table property. This is confusing because if ignored, this property defaults to the table key.

I end up With the following query:

LEFT JOIN {wowtoon_cached_sets} wowtoon_cached_sets_main 
ON   wowtoon_owners.toon_key = wowtoon_cached_sets_main.toon_key 
AND  wowtoon_cached_sets_main.active = '1' 
LEFT JOIN {wowtoon_cached_sets} wowtoon_cached_sets_second 
ON   wowtoon_owners.toon_key = wowtoon_cached_sets_second.toon_key 
AND  wowtoon_cached_sets_second.active = '0'

Using the below code:

  $data['wowtoon_cached_sets_main']['table']['group']  = $data['avatar']['table']['group'];
  $data['wowtoon_cached_sets_main']['table']['join'] = array(
      'avatar' => array(
        'left_table' => 'wowtoon_owners',
        'left_field' => 'toon_key',
        'field' => 'toon_key',
        'table' => 'wowtoon_cached_sets',
        'extra'      => array(
          array(
            'field'    => 'active',
            'value'    => 1,
          ),
        )
    )
  );

  $data['wowtoon_cached_sets_second']['table']['group']  = "second spec";
  $data['wowtoon_cached_sets_second']['table']['join'] = array(
      'avatar' => array(
        'left_table' => 'wowtoon_owners',
        'left_field' => 'toon_key',
        'field' => 'toon_key',
        'table' => 'wowtoon_cached_sets',
        'extra'      => array(
          array(
            'field'    => 'active',
            'value'    => 0,
          ),
        )
    )
  );

  $data['wowtoon_cached_sets_main']['spec'] = array(
          'field' => array(
            'field' => 'spec',
            'title' => t('Spec'),
            'click sortable' => TRUE,
            'help' => t('wowtoon_cached_sets Spec'),
            'handler' => 'views_handler_field',
    ),
  );
  $data['wowtoon_cached_sets_second']['spec'] = array(
          'field' => array(
            'field' => 'spec',
            'title' => t('Spec 2'),
            'click sortable' => TRUE,
            'help' => t('wowtoon_cached_sets Spec 2'),
            'handler' => 'views_handler_field',
    ),
  );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.