1

I am trying to figure out how add a relationship in a view_data...

I have a hook_views_data which sets up the base table and a group called List. It has two tables

  1. Main List
  2. List Contents

And they are related via a list_id. I have a table -> join to join the table with the list contents and all that fun stuff and that all seems to be working. But when I add a relationship for List Contents it won't show any List Contents fields.

So I can get List Name and List ID no problem, I add a relationship to List and a contextual filter for List ID but after I add a relationship I still cannot access any of the $data['list'] array members (so product id or nid or what not).

Any idea what I am missing?

  $data['lists']['table']['group']  = t('Lists');

  $data['lists']['table']['base'] = array(
    'field' => 'list_id',
    'title' => t('Lists'),
    'help' => t('TODO:'),
    'entity type' => 'commerce_product',
    'access query tag' => 'commerce_product_access',
  );

  $data['lists']['table']['join'] = array(
    'commerce_List' => array(
      'left_field' => 'list_id',
      'field' => 'list_id'
    )
  );

  // Expose the List id.
  $data['lists']['list_id'] = array(
    'title' => t('List ID'),
    'help' => t("The List ID."),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
    'relationship' => array(
      'title' => t('List'),
      'help' => t("Relate this List to its contents"),
      'handler' => 'views_handler_relationship',
      'base' => 'commerce_List',
      'base field' => 'list_id',
      'field' => 'list_id',
      'label' => t('List contents'),
    ),
  );

  // Expose the List name.
  $data['lists']['name'] = array(
    'title' => t('List Name'),
    'help' => t('List name'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    )
  );

  // Expose the product ID.
  $data['list']['product_id'] = array(
    'title' => t('Product ID'),
    'help' => t('The unique internal identifier of the product.'),
    'field' => array(
      'handler' => 'commerce_product_handler_field_product',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'relationship' => array(
      'title' => t('Product'),
      'help' => t("Relate this List to its owner's user account"),
      'handler' => 'views_handler_relationship',
      'base' => 'commerce_product',
      'base field' => 'product_id',
      'field' => 'product_id',
      'label' => t('List product display'),
    ),
  );

  // Expose the product type.
  $data['list']['nid'] = array(
    'title' => t('Nid'),
    'help' => t('The human-readable name of the type of the product.'),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'relationship' => array(
      'title' => t('Product display (node)'),
      'help' => t("Relate this List to its owner's user account"),
      'handler' => 'views_handler_relationship',
      'base' => 'node',
      'base field' => 'nid',
      'field' => 'nid',
      'label' => t('List product display'),
    ),
  );

  // Expose the creator uid.
  $data['lists']['uid'] = array(
    'title' => t('Uid'),
    'help' => t("The owner's user ID."),
    'field' => array(
      'handler' => 'views_handler_field_user',
      'click sortable' => TRUE,
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_user_uid',
      'name field' => 'name', // display this field in the summary
    ),
    'filter' => array(
      'title' => t('Owner'),
      'handler' => 'views_handler_filter_user_name',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'relationship' => array(
      'title' => t('Owner'),
      'help' => t("Relate this List to its owner's user account"),
      'handler' => 'views_handler_relationship',
      'base' => 'users',
      'base field' => 'uid',
      'field' => 'uid',
      'label' => t('List owner'),
    ),
  );

  // Expose links to operate on the product.
  $data['list']['delete_List'] = array(
    'field' => array(
      'title' => t('Delete'),
      'help' => t('Provide a simple link to delete the product from the List.'),
      'handler' => 'commerce_List_handler_field_product_link_delete',
    ),
  );

1 Answer 1

0

The problem was I was missing some parts for my second table. Any table used in this views data hook needs to have a group. Adding

  $data['list']['table']['group']  = t('List');

  $data['list']['table']['join'] = array(
    'lists' => array(
      'left_field' => 'list_id',
      'field' => 'list_id'
    )
  );

Fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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