Score:0

Loading content in a View via a sibling entity reference

za flag

I think what I need to do is use one Drupal View's results to filter the results of another view. Here is what I am trying to accomplish.

I have 3 content types (node types): Blog, Blog Post, Biography Page (aka Blog Author)

The blog post contains an entity reference to both the blog it should be in and one or more author's biography pages.

What I need to be able to do is take the ID for a given blog and get back all the authors that are associated via an associated blog post. Then remove any duplicates to show a list of all authors under a particular "blog".

I think I might be able to use a relationship. But still fuzzy on how that would work.

How can I accomplish this?


UPDATE: To clarify, I need to start with the Blog ID and using that find the authors of any blog posts that associate to said blog entity ID. Then take that resultset and dedupe it. I'm not worried about deduping. I just need help figuring out how to load the authors via that relationship.

Here is a diagram of this:

enter image description here

Thanks!

beltouche avatar
cn flag
It doesn't sound like you need multiple Views to do this from your description. Can you give more detail? What's the starting a point - blog or blog post? Where is this list of authors appearing?
Patrick avatar
za flag
@beltouche I have added more information, as well as a diagram, that I hope helps illustrate what I am trying to accomplish. Please let me know if I can provide any other information.
Score:1
cn flag

I'm going to assume that this list of authors would appear, e.g., as a block on a blog post page, as in "Other authors associated with this post's blog." (I'm also assuming you're using Drupal 8 or 9)

You can do this with relationships:

  1. The first relationship is through the blog post's blog ID field to get the blog
  2. The next relationship uses the first with a "reverse" relationship through the same field to find all blog posts referencing that blog
  3. The third relationship uses the second to get the authors through the Author ID

Here's a step-through I recommend using fields.

  1. Add the first relationship. Give it a good readable "Administrative Title" because Drupal's built-in names become arcane very fast.
  2. To check add the title field of the blog to confirm you've got what you expect. (You can always remove it later if you don't need it.)
  3. Add the second relationship, being sure to use the first, and also give it a good name. Again use a field from the new relationship as a sanity check.
  4. Add the third relationship being sure to use the second ...

You probably want to check the "require relationship" box in each case.

Yeah, I can see you getting a ton of duplicates that you'll have to remove. You may be able to get rid of some using Views' UI, but that's always dicey. You'll probably have to employ a view hook to clean up things.

Patrick avatar
za flag
The idea is to list all of the authors on a blog page. Basically any author who has a post in the given blog should have their face show up on the blog page.
Patrick avatar
za flag
Interesting approach. It didn't occur to me to use a 3rd relationship. Gotta figure out how that part would link up. But I like the approach.
beltouche avatar
cn flag
If you're starting on the blog page, instead of a blog post, you would eliminate the first relationship I listed.
Patrick avatar
za flag
Thats kinda where I landed too. I just couldn't get Drupal to build the relationship quite right via the UI. So I had to hack it the rest of the way. But I am going to give you credit for helping me get there.
Patrick avatar
za flag
For anyone else reading this in the future, see my answer below for my final approach on this.
beltouche avatar
cn flag
I have to admit, I'm still surprised that this wasn't doable entirely within the UI (except for the dups). Thanks though. I'm glad I was able to help you get to a solution.
Patrick avatar
za flag
Maybe there is a way that I'm just missing. But honestly I think the UI suffers from trying to over simplify names of relationships too. Which confuses the matter.
Score:0
za flag

Alright, so after dissecting Drupal Views a bit more and the query from it, I did this (which honestly was inspired by the suggestion in the answer from @beltouche).

So I setup a relationship using the blog post authors reference field. Set it as required. Then I setup a second required relationship via the field that referenced the blog and had it use the first relationship. This second relationship then gets used by a contextual filter that accepts the value of the blog ID and finds results that way.

Next, I found that the query was really close and just needed two joins modified. So I hacked it using query alter (details below). Finally, I set "Use aggregation" to "Yes".

Here is my code:

<?php
function my_module_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view->id() == 'authors' && $view->current_display == 'authors_eva') {
    /*
     * Basically what I did here is forced it to build the relationships in the way I know works
     * which I tested by manually rewriting the query and testing against the database.
     */
    $table_authors_field_data = $query->getTableInfo('blog_post_authors_node_field_data');

    if (!empty($table_authors_field_data)) {
      $table_authors_field_data['join']->leftField = 'blog_post_authors_target_id';
    }

    $table_node_blog = $query->getTableInfo('blog_post_authors_node_field_data__node__blog');

    if (!empty($table_node_blog)) {
      $table_node_blog['join']->field = 'entity_id';
      $table_node_blog['join']->leftField = 'entity_id';
      $table_node_blog['join']->leftTable = 'node__blog_post_authors';
    }
  }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.