I want to list all the nodes that have the currently viewed node as their parent.

To make this more tangible and less abstract, let's do an example:


I have a content type Locations which has an Entity Reference field called parent_location with an unlimited number of values.

For example:

- USA (3)                             [country level]
  - New York (4)                        [state level]
    - New York City (6)                   [city level]
      - Central Park (8)                    [landmark level]
      - Empire State Building (9)           [landmark level]
      - Times Square (10)                   [landmark level]
  - Illinois (5)                        [state level]
    - Chicago (7)                         [city level]
      - Sears Tower (11)                    [landmark level]
      - That bean looking thing (12)        [landmark level]

So when I view the content for USA (3), I wanna list all the children, meaning New York (4) and Illinois (5).

When I view New York City (6) I want to list Central Park (8), Empire State Building (9), and Times Square (10).

I want these to show up when I view /node/6 and be clickable, so that I can browse this nested child/parent structure.

It's already possible by default to traverse UP on the tree. Meaning I can easily get from Times Square (10) all the way up to USA (3).


However, I can't figure out how to list all children of the appropriate level!

I did create a block view called List Child Locations. And I've placed this view on the block Content. It shows up correctly, but it does not list the appropriate children.

Listing too many children


When it comes to editing my view, I have no clue what I'm doing or how...

I see there's two options:

admin/structure/views/view/list_child_locations/edit/block_1

and

admin/structure/views/view/list_child_locations/edit/entity_reference_1

I think I have to do something with contextual filters, while I'm in the Entity Reference Display. But I can't find anything that looks like what I need...

View Settings

Out of all those many options, which one do I chose to make sure it does something like

SELECT * from Locations WHERE parent_location = %current_location% ?

What's the right settings for my entity display to only list the children?

有帮助吗?

解决方案

Finally, I figured it out!

First, I learned more about contextual filters and relationships, and what they each mean and do (relationships basically relates to the FROM query, contextual filters to the WHERE query)

https://www.youtube.com/watch?v=mRWtpcUm9pI

Another user had a similar question and solved it like so:

https://www.drupal.org/project/views/issues/1724396

So all I had to do was:

/admin/structure/views/add

  • show content of type "Locations"
  • Create a block

Then under "advanced" add a contextual filter, check "parent_location (field_parent_location)", press the button to "Add and configure".

Then under "When no filter is available" say "provide a default value" and select "content ID from URL"

Hit apply, save the view.

Under /admin/structure/block place the block to the content region and make it visible for Locations.

This was super simple! :-)

许可以下: CC-BY-SA归因
scroll top