Description

For example:

  • A content entity called tattoo_liking is created using the content_entity module
    • id
    • user_liking
    • tattoo_liked,
    • created

If you create a view that lists users, you won't be able to automatically add a relationship to the tattoo_liking table based on user_liking. You will instead need to follow the solutions provided here https://api.drupal.org/api/drupal/core%21modules%21views%21views.api.php/function/hook_views_data_alter/10.

Specifically, I needed to use this in a custom module:

function tattoo_liking_views_data_alter(&$data) {
  if (!Drupal::database()->schema()->tableExists('tattoo_like')) {
    Drupal::logger('tattoo_liking')->warning(t('Table "tattoo_like" didn\'t exist while attempting to present views information about tattoo_like entity. Views will not be able to interact properly with tattoo likes.'));
    return;
  }
  // Add a possible relationship between tattoo_like.user_liking and users.uid.
  $data['users']['users_to_tattoo_like'] = [
    'title' => t('User liking a tattoo'),
    'help' => t('Relate users to the user that liked a tattoo'),
    'relationship' => [
      'base' => 'tattoo_like',
      'base field' => 'user_liking',
      'field' => 'uid',
      // ID of relationship handler plugin to use, NOT an id of some table or field.
      'id' => 'standard',
      'label' => t('User liking a tattoo')
    ]
  ];
}

This only runs on cache rebuilds, and tells views that it can provide a relationship as described before the code block.

Notes