0

I am using the Views Tree Module which allows me to create a View that illustrates parent-child relationships.

My 'associations' go like this:

I have 3 content types which entity-reference each to a single direction.

  • A Sub task has an EntityReference field pointing to a Main Task and a Main Task has an EntityReference field pointing to a Project.

      Subtask -> Main Task -> Project. (associations are done with EntityReference fields)
    

Now I'd like to embed this view as a block within a given Project's page. Naturally I use a Contextual Filter that uses the ContentID from Url setting.

I thought this would allow me to view within that Project's page the Main Tasks and Sub Tasks that belong to it, but since only the Main Tasks are directly connected with the Project itself only the Main Tasks are shown.


The question:

Is there any way to bring the Subtasks(which belong to the Main Tasks that belong to the given Project) to show as well?


Summary of my project

To sum this up:

I have 3 content types

  • Project
  • Main Task
  • Sub Task

  • A Subtask entity references a Main Task and this in turn entity references a Project
  • I use the Views Tree module to list the Main Tasks and the Subtasks below each Main Task(in a 'tree-style' view
  • I want to show this view with the appropriate Main Tasks/Subtasks inside each Project page.
  • I use the Views Tree module to construct parent-child relationship Views

1 Answer 1

0

I found a rather clean and intuitive solution, however I am very interested in other, more straightforward answers if they exist.


I created a new Computed Field, called parent_project_computed field which:

  • calculates the parent-project based on the parent_main_task field(which is entity reference) if it's a Sub Task content type
  • calculates(basically just copies) the parent_project field if it's a Main Task content type

This way both the Main Task and Sub task have a field which stores the nid value of the Parent Project. Then I use a contextual filter based on this new parent_project_computed field and both content types match this filter so they show up in the results just fine.

This is the PHP I wrote within this new computed field:

$entity_field[0]['value'] = null; //initial value null, should get overwritten in IF's below

//if field_parent_project exists just copy it's value
if (array_pop(array_pop(field_get_items($entity_type, $entity, 'field_parent_project')))) {
   $entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_parent_project')));
}

// if 'field_main_task' exists get it's value, load the node and get the E-R value within it
if (array_pop(array_pop(field_get_items($entity_type, $entity, 'field_main_task')))) {
    $nid = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_main_task')));
    $node = node_load($nid);
    $info = field_get_items('node', $node, 'field_parent_project');
    $entity_field[0]['value'] =$info[0];//ER fields store data in array, since this is a single value ER field it always sits at position '0'
}

//Note: E-R value means Entity Reference value.

If the PHP code can be made more, err bulletproof, I'd appreciate a comment below. Thanks.

Update:

On saving each Task you need a post-save custom hook to reload and resave all the Subtasks in order to update their 'Parent Project' computed field which has now changed. We've used this module which provides some nice additional post-save hooks and some more.

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.