In Drupal 7, I have a content type with an entity reference field. When adding new content of this type, an entity reference view filters the available options.

A contextual filter on this entity reference view should check whether the data matches against the node ID from URL being viewed while adding the new content.

I'm not sure whether I understand correctly how Drupal is supposed to work here, as everything I've tried gives no results, unless I manually enter test data in which case it works fine.

Let's say a user is on page site.com/?q=node/75, then adds content. The new URL is site.com/?q=node/75#overlay=%3Fq%3Dnode%252Fadd%252Fcontenttype.

The contextual filter is set for "When the filter is not available" to "Provide default value" of type "Content ID from URL". This returns zero results, even though I could manually provide the filter value and it would show the actual results.

Is this because the "Content ID from URL" is coming from the underlying (originating) URL instead of the overlay new content part of the path? Or is there some other issue?

How is it possible to filter contextually against the content ID of the original page from which a user adds new content? If it could be done by passing arguments to the contextual filter with a token from the entity reference field, that would be fine too, however so far I have tried with current page node and more and run into the same problem, it does not filter any of the appropriate results.

Thank you for your help!

Comments

Stefan Lehmann’s picture

If you inspect the HTML of the URL you'd see that this is actually an iframe. A resource like: https://domain/node/add?render=overlay is loaded into the iframe, which is totally agnostic to where it's loaded from.

What I'd do is to disable the overlay and add an attribute for the current node context to the node creation link, eg:

You are on node/123. Make the link url, eg: node/add/page?context_node=123

Now the view should be able to parse the node ID from the URL by parsing the value from "context_node".

I like cookies!

eaglegamma’s picture

Thank you for the reply.

I'm not sure I totally understand your answer. When I try adding "?context_node=75" to the URL, it doesn't load the form, instead it just goes back to the basic "Add content" page. Do I have to change any settings or do anything else to make this possible?

And for parsing, how do you mean, from the raw arguments within the contextual filter or elsewhere?

eaglegamma’s picture

Should that be "/?q=node/add/contenttype&field_name=75"?

Stefan Lehmann’s picture

Ah yeah, if you don't have something like pathauto installed it's probably rather a URL like that, yes.

Also you'll have to add "field_name" or whatever to the contextual filter settings of the view. Not sure what the exact magic words there are. But I know it's possible. :D

I like cookies!

eaglegamma’s picture

OK, thanks again. I'm experimenting more, and if you have any links or other details that could help clarify what to do, I would appreciate it! :)

eaglegamma’s picture

OK, so I see that I can just put an arbitrary reference in the URL and then contextually filter by its placement in the path. Not sure how hacky or elegant this is, will continue playing with it to see how I can incorporate it into my design.

If anyone else comes across this wanting to achieve something similar, I'm adding the desired node ID into the fourth place in the path (e.g. site.com/node/add/content_type/25 where "25" is the ID of the content I want; for path positions, "node" = 1, "add" = 2, "content_type" = 3, "25" = 4). Then, in the contextual filter, "when the filter value is not available" I "provide default value" of type "Raw value from URL", selecting "path component" 4. You could choose your own URL scheme, and pick the appropriate place in the URL path. The basic idea is to put the node ID you want to at a fixed place in the URL, so that you can consistently refer to it by its path location in the contextual filter.

Next we'll see about making appropriate links to the new URL system...

eaglegamma’s picture

As an update, I made the links by creating a new block, and using the Token Filter module (https://www.drupal.org/project/token_filter) to create a new text format allowing the use of tokens. This way I could make links that automatically go to the appropriate URL containing the path information for the contextual filter described above.

Stefan Lehmann’s picture

I think you have to realize that the Drupal backend doesn't care much on which page you're in the frontend, when adding new content. So you have to actively tell it that.

My idea is that the node you're looking at provides that content creation link as part of its own output. There are multiple ways to achieve that: tokens, views, display suite etc. as the current node ID has to be injected into that content creation link.

Now you have a custom link each unique to the current node, which should enable you to access that information once you're executing that entity reference view as part of the content creation form.

Is that more understandable?

I like cookies!

eaglegamma’s picture

I'm just seeing your message after assembling a working solution, which I think basically matches what you describe (going the tokens route). Thank you again, and please let me know if you want any more details! :)