This seems like it should be pretty simple, but for some reason I can't get this to work, and I'm not sure why. I have a node type called Project. Some projects can be subproject of other projects. However, all projects have the same information, so there is no need to have separate entity types. To create this relationship, I have an entity reference field called Parent Project (field_parent_project) that relates sub-projects to parent Project nodes; in other words, only subprojects will have a value in this field.

I have a view that list Project nodes, and I want it to display only parent projects, with subprojects listed as a field at the bottom of each Project node display. I have defined a relationship for field_parent_project, and I have added a filter where field_parent_project is NULL, with the idea that it would not list sub projects, since they have a value for field_parent_project. However, what I get is very different than what I would expect:

  1. My sub-projects are still being listed in the display, even though the filter should be keeping them from being displayed
  2. The parent project node that is referenced by the other subprojects is not displayed at all

I have tried using both the referencing and referenced versions of the relationship, but neither seems to work. I would like to just be able to use this field and not have to add a separate field just to indicate that this is a subproject. What do I need to change to get my view to work as I want? The view export is below.

Thanks.

$view = new view();
$view->name = 'projects';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'projects';
$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['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['query']['options']['distinct'] = TRUE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'grid';
$handler->display->display_options['style_options']['columns'] = '3';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: Entity Reference: Referencing entity */
$handler->display->display_options['relationships']['reverse_field_parent_project_node']['id'] = 'reverse_field_parent_project_node';
$handler->display->display_options['relationships']['reverse_field_parent_project_node']['table'] = 'node';
$handler->display->display_options['relationships']['reverse_field_parent_project_node']['field'] = 'reverse_field_parent_project_node';
$handler->display->display_options['relationships']['reverse_field_parent_project_node']['label'] = 'Parent Project - Referencing';
/* 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;
/* Field: Content: Banner Image */
$handler->display->display_options['fields']['field_banner_image']['id'] = 'field_banner_image';
$handler->display->display_options['fields']['field_banner_image']['table'] = 'field_data_field_banner_image';
$handler->display->display_options['fields']['field_banner_image']['field'] = 'field_banner_image';
$handler->display->display_options['fields']['field_banner_image']['label'] = '';
$handler->display->display_options['fields']['field_banner_image']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_banner_image']['click_sort_column'] = 'fid';
$handler->display->display_options['fields']['field_banner_image']['settings'] = array(
  'image_style' => 'project_panel',
  'image_link' => 'content',
);
/* Field: Content: Body */
$handler->display->display_options['fields']['body']['id'] = 'body';
$handler->display->display_options['fields']['body']['table'] = 'field_data_body';
$handler->display->display_options['fields']['body']['field'] = 'body';
$handler->display->display_options['fields']['body']['label'] = '';
$handler->display->display_options['fields']['body']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['body']['type'] = 'text_summary_or_trimmed';
$handler->display->display_options['fields']['body']['settings'] = array(
  'trim_length' => '600',
);
/* Field: Content: Parent Project */
$handler->display->display_options['fields']['field_parent_project']['id'] = 'field_parent_project';
$handler->display->display_options['fields']['field_parent_project']['table'] = 'field_data_field_parent_project';
$handler->display->display_options['fields']['field_parent_project']['field'] = 'field_parent_project';
$handler->display->display_options['fields']['field_parent_project']['label'] = '';
$handler->display->display_options['fields']['field_parent_project']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_parent_project']['settings'] = array(
  'link' => 0,
);
/* Sort criterion: Content revision: Updated date */
$handler->display->display_options['sorts']['timestamp']['id'] = 'timestamp';
$handler->display->display_options['sorts']['timestamp']['table'] = 'node_revision';
$handler->display->display_options['sorts']['timestamp']['field'] = 'timestamp';
$handler->display->display_options['sorts']['timestamp']['order'] = 'DESC';
/* 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(
  'project' => 'project',
);
/* Filter criterion: Content: Parent Project (field_parent_project) */
$handler->display->display_options['filters']['field_parent_project_target_id']['id'] = 'field_parent_project_target_id';
$handler->display->display_options['filters']['field_parent_project_target_id']['table'] = 'field_data_field_parent_project';
$handler->display->display_options['filters']['field_parent_project_target_id']['field'] = 'field_parent_project_target_id';
$handler->display->display_options['filters']['field_parent_project_target_id']['operator'] = 'empty';

/* Display: Project listing page */
$handler = $view->new_display('page', 'Project listing page', 'page_1');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['title'] = 'Projects';
$handler->display->display_options['defaults']['pager'] = FALSE;
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '9';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['pager']['options']['id'] = '0';
$handler->display->display_options['path'] = 'projects';
$handler->display->display_options['menu']['type'] = 'normal';
$handler->display->display_options['menu']['title'] = 'Projects';
$handler->display->display_options['menu']['description'] = 'List of MRSL projects';
$handler->display->display_options['menu']['weight'] = '0';
$handler->display->display_options['menu']['name'] = 'main-menu';
$handler->display->display_options['menu']['context'] = 0;

Comments

seanr’s picture

I need this too, see my unintentional duplicate post: #2060779: Filter based on whether node's entity reference field has a value

merlinofchaos’s picture

Can you paste the query that view generates?

seanr’s picture

In my case, I can't even figure out how to add an exposed filter based on whether an entity reference field has a value or not. Some of my items have associated case studies and other don't, so I need to let my users filter the list down to just those that do.

dajjen’s picture

Hi, as a respons to #3.
I fixed this with a exposed filter on the entityreferences field and show the operator to and then I did a hook_form_alter and removed the operators i didn't wanted (all except the "empty" and "Not empty" which are the ones filtering if the field have a reference or not) and renamed this to appropriate strings for the use case. I also added a "All" option for not filtering at all.

function my_custom_module_name_form_alter (&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'companies2') {
  unset($form['fieldname_target_id_op']['#options']['<'], 
          $form['fieldname_target_id_op']['#options']['<='],
          $form['fieldname_target_id_op']['#options']['='],
          $form['fieldname_target_id_op']['#options']['!='],
          $form['fieldname_target_id_op']['#options']['>='],
          $form['fieldname_target_id_op']['#options']['>'],
          $form['fieldname_target_id_op']['#options']['between'],
          $form['fieldname_target_id_op']['#options']['not between'],
          $form['fieldname_target_id_op']['#options']['regular_expression']
          );
   
      $form['fieldname_target_id_op']['#options']['empty'] = t('No');
      $form['fieldname_target_id_op']['#options']['not empty'] = t('Yes');
      $form['fieldname_target_id_op']['#options'] = array('all'=>t('- All -')) + $form['fieldname_target_id_op']['#options'];
}
}
Aritra’s picture

Working for me. Using the values of the content type, will solve the problem.

Hars Saraf’s picture

This is an old post, I just hit by chance. Just if anybody else is stuck with same problem. I think this might work.
U don't need relationships or contextual filters.

Use the Filter: field_parent_project set "Operator" to "Is not empty (NOT NULL) without any relationships;
And add the entity reference field (field_parent_project) to the display, link this field to the entity for ease of access.

renatog’s picture

Status: Active » Reviewed & tested by the community

Hi guys.

Make sense. I make a test and the #6 really works good for me.

Thank you very much.

renatog’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.