1

I'm attempting to use a view to display the body of a content type that is referenced based on a field referencing another content type as long as that content type is referenced by another field. Confusing I know. Here's a diagram of what I'm trying to accomplish.

enter image description here

I need to display the body and title of all 'news' that reference a 'horse' that an 'owner' references as long as that 'owner' is referencing the current user.

I understand that there would be much better ways to structure my content type fields to get them to play more nicely, and I understand that I could easily accomplish this by not using views and making a custom module but that isn't an option available to me. I am required to use views. I wouldn't mind creating a custom views filter plugin, or use a hook though. I understand how to perform entity queries that can accomplish this task. However I'm not sure how view filter plugins work.

Is there a way to accomplish this specifically with views?

  • I need to display the body and title of all 'news' that reference a 'horse' I am assuming this is a View Block, where are you displaying this? When a Horse content type is viewed? OR when a Owner content type is viewed? – No Sssweat Jun 7 '17 at 7:19
  • It's a View Block that displays all news items when a specific page is visited. Basically there's a news page that only displays news items that pertain to a horse that the user has ownership of. – Matt Jun 7 '17 at 13:30
  • So it's dependant on the logged in user? So an anonymous user would not see anything because they don't own anything, correct? – No Sssweat Jun 7 '17 at 17:03
  • correct. This is for logged in users only – Matt Jun 7 '17 at 18:27
  • I'm too lazy right now to answer your question. Here is what you need to do. 1) Programmatic ally get the current user ID (look that up) 2) Once you have their user ID, query the DB where content type is owner and entity reference field is User ID and get the value of entity ref horse (horse node id). 3) In Views, under Advanced, add entity reference horse field as a contextual filter to your view 4) use template preprocess function to add a variable of views_embed_view('view_name','block_1', $horse_nid); – No Sssweat Jun 8 '17 at 6:10
1

So assuming there's a content type with machine name new for News, owner for Owner, horse for horse.

The news content type has a field named field_horses referencing horses

The owner content type has an instance of the same field field_horses and a reference field to User, field_user

Create a view listing Content of type News

Add a relationship

Entity Reference: Referenced Entity A bridge to the Content entity that is referenced via field_horses

require this relationship

add a second relationship

Entity Reference: Referencing entity A bridge to the Content entity that is referencing Content via field_horses

have this second one use the first relationship "Content Entity referenced from field_horses"

require this relationship

add a third relationship

Entity Reference: Referenced Entity A bridge to the User entity that is referenced via field_user

have this third relationship use the second relationship "Content referencing content from field_horses"

require this relationship

For filters you need two content type filters One for Content type = News, using no relationships another for Content Type = Owner using the "Content referencing content from field_horses" relationship

One user filter, Current User (yes), using the "User entity referenced from field_user

enter image description here

$view = new view();
$view->name = 'news';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'News';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'News';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: Entity Reference: Referenced Entity */
$handler->display->display_options['relationships']['field_horses_target_id']['id'] = 'field_horses_target_id';
$handler->display->display_options['relationships']['field_horses_target_id']['table'] = 'field_data_field_horses';
$handler->display->display_options['relationships']['field_horses_target_id']['field'] = 'field_horses_target_id';
$handler->display->display_options['relationships']['field_horses_target_id']['required'] = TRUE;
/* Relationship: Entity Reference: Referencing entity */
$handler->display->display_options['relationships']['reverse_field_horses_node']['id'] = 'reverse_field_horses_node';
$handler->display->display_options['relationships']['reverse_field_horses_node']['table'] = 'node';
$handler->display->display_options['relationships']['reverse_field_horses_node']['field'] = 'reverse_field_horses_node';
$handler->display->display_options['relationships']['reverse_field_horses_node']['relationship'] = 'field_horses_target_id';
$handler->display->display_options['relationships']['reverse_field_horses_node']['required'] = TRUE;
/* Relationship: Entity Reference: Referenced Entity */
$handler->display->display_options['relationships']['field_user_target_id']['id'] = 'field_user_target_id';
$handler->display->display_options['relationships']['field_user_target_id']['table'] = 'field_data_field_user';
$handler->display->display_options['relationships']['field_user_target_id']['field'] = 'field_user_target_id';
$handler->display->display_options['relationships']['field_user_target_id']['relationship'] = 'reverse_field_horses_node';
$handler->display->display_options['relationships']['field_user_target_id']['required'] = TRUE;
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'news' => 'news',
);
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type_1']['id'] = 'type_1';
$handler->display->display_options['filters']['type_1']['table'] = 'node';
$handler->display->display_options['filters']['type_1']['field'] = 'type';
$handler->display->display_options['filters']['type_1']['relationship'] = 'reverse_field_horses_node';
$handler->display->display_options['filters']['type_1']['value'] = array(
  'owner' => 'owner',
);
/* Filter criterion: User: Current */
$handler->display->display_options['filters']['uid_current']['id'] = 'uid_current';
$handler->display->display_options['filters']['uid_current']['table'] = 'users';
$handler->display->display_options['filters']['uid_current']['field'] = 'uid_current';
$handler->display->display_options['filters']['uid_current']['relationship'] = 'field_user_target_id';
$handler->display->display_options['filters']['uid_current']['value'] = '1';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'horse-news';

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.