Hi all,
In a situation where the reference field is setted on the child node instead of on the parent node is simply to obtain a URL path like "parent-node-title/child-node-title" for child nodes, using the [node:field-ref:title]/[node:title] token in pathauto.
But using this module here we have the opposite: the reference field is setted on the parent node instead of on the child node. In this case tokens in pathauto doesn't works (if you know the existance of a token that can do this, please let me know)
So, how I can obtain the desidered URL path for child nodes?
Thank you very much for any help/suggestion
Comments
Comment #1
bojanz CreditAttribution: bojanz commentedYou will probably have to add a custom token through some coding.
Moving the issue to the Pathauto issue queue, they might have a better idea.
Comment #2
RogerRogers CreditAttribution: RogerRogers commentedI think I have the same problem.
I have an Entity Reference field that uses Inline Entity Form. The field is for an image gallery, and I want my URL to be something like /type_from_referencing_node/productname_from_referencing_node/imagename, but I'm unable to achieve this. When I look at the image gallery field tokens for PathAuto I get "Nodes with delta 1", "Nodes with delta 2", etc. In each of these delta rows I do have all of the related fields, but none actually generates a token value.
Comment #3
MXTNo news on this?
Please help!
Thank you
Comment #4
RogerRogers CreditAttribution: RogerRogers commentedI had to create a custom path for this, which was pretty easy really. My code, FWIW:
Comment #5
MXTThank you RogerRogers, following your suggestion I've a PARTIAL working solution.
Why PARTIAL?
Well, the problem is that creating the child node trough Inline Entity Form I have to save it 2 times to have the token replacement working. The problem is probably due to the fact that token replacement is executed BEFORE Inline Entity Form creates the child node id in the parent node nodereference field (target_id).
So we probably have:
I've tried to change my custom token module weight to 1 in the hope it would execute after Inline Entity Form but without success.
How did you managed this issue?
Thank you very much for your help
Comment #6
RogerRogers CreditAttribution: RogerRogers commentedHey MXT, unfortunately the requirements changed and I'm not actually using the code I sent before, so it's not too easy for me to test. But, if I remember correctly, I didn't have the problem you mention.
I have commented my code a bit more, because it might be confusing. My IEF is related to nodes, not 'generic' entities.
Comment #7
MXTYes RogerRogers my code is very similar to yours (thank you for sharing it) but this is not the point.
The query I / you wrote when runs for the first time returns NO results because the condition:
->fieldCondition('field_image_gallery', 'target_id', $node->nid)
can't match (field_image_gallery with target_id record is not already created by IEF at this time).
The query returns the result if runs for a second time (because at this point IEF has already created the target_id and fieldCondition can match.
I'll try to reassign this to IEF issue queue to hear Bojanz feedback
Thank you very much for collaborating until now ;-)
Comment #8
RogerRogers CreditAttribution: RogerRogers commented@MXT: gotcha. Post back what you find if you don't mind!
Comment #9
MXTI've actually found a workaround to resolve all the issues:
In addition to the custom token above, I've also implemented hook_node_insert() and hook_node_update() for the parent node.
In this implementations I call the pathauto_node_update_alias() for each child node.
In this way, every time the parent node is created or updated, all child paths are refreshed too in the right way.
This resolve all 3 critical situation (remember that my desired path is "parent-node-title/child-node-title" for child nodes):
Critical case 1: suppose you have already a parent node with a number of child nodes with all paths correctly setted. Suppose you need to change the parent node title: saving (updating) the parent node you automatically obtain that all the child nodes paths are recreated reflecting the new parent node title in the url (wow!)
Critical case 2: suppose you are creating for the first time the parent node: you are compiling all input fields and creating the child nodes with IEF: because you haven't already saved the parent node form, all child nodes can't calculate the right url (the parent title is not yet saved at the moment!). But when you finally save the parent node the hook_insert is triggered and all the child nodes paths are correctly recreated.
Critical case 3: this case is described in my #5 and #7. This case is resolved too saving the parent node: in this way you don't need to save 2 times the new created child to obtain the right child path.
I would like to know if all this stuff and workaround can be directly better managed by IEF itself.
Comment #9.0
MXTSmall mistake
Comment #10
dman CreditAttribution: dman commentedFYI, I did this with a customizable module of my own,
I've just put it up as a sandbox for reference, though i know the docs need some work, and edge cases probably abound.
https://drupal.org/sandbox/dman/2209055
This module
* provides new tokens to use with pathauto
* allowing you access to "the entity that refers to this one through entity_reference (via a named field)"
I have a number of publication issues - that link to publication 'articles' using inline_entity_form to allow an editor to do the whole thing from the top.
The pattern for my 'article' content type
Which translates to:
* when making a path for an article
* find the url path of
* the first (0th) entity that links to me
* by way of the field_pi_articles entityreference.
In this way, my articles are all given URLs derived from their parent/owner node.
THERE IS a race condition if you try to create all these nodes at once, as the children require the parent to have been given a valid URL before they save (observed above), and inline_entity_form may not always ensure that. But in general, this token that helps you build an URL based on incoming references is what I needed at that time.
I actually added a 'rule' to take care of the rest, including 'if parent is unpublished, all children are unpublished' and vice-versa. And one for 'if the parent path changes, update all child paths' But that was pure 'rules' only IIRC.
Comment #11
kcolwell CreditAttribution: kcolwell commentedThank you dman for the sandbox code... saved me a lot of time.
Thanks,
kcolwell
Comment #12
flyke CreditAttribution: flyke commentedI also thank dman for his sandbox code, it works great. My use case was to create a pattern for path aliases. I have node type A that uses inline entity form to reference nodes of node type B. For a node of node type B, I want the url to be path-to-referencing-node-a/title-of-b.
It is hard to believe that this context is not available by default. It really should be. Using dman's code, I was able to use context [node:incoming_references:field_referencingfield:0:url:path]/[node:title] as a pattern which is exactly what I was looking for.
It would be nice if drupal.org had a point system for comments so I could +1 dman's comment here.
I hope somehow this context makes it into a next version of Inline Entity Form, or probably better the entityreference module.
Comment #13
geek-merlinFor dman's module also see #2402011: Merge with entityreference_backreference, for further development also #2402027: Add formatter & widget to be used with IEF.
Comment #14
geek-merlinPeople here might interest #2401313: List comprehension tokens like ref:all:nid
Comment #15
jphelan CreditAttribution: jphelan commentedNone of the solutions above were working well for me without saving twice. It seems pathauto was not updating the path until after the hooks. I expanded on MXT's idea and just created the path myself using path_save.
Comment #16
pachabhaiya CreditAttribution: pachabhaiya as a volunteer and at Drupal Nepal commentedSolution provided by @jphelan (#15) works good for me.
Comment #17
Sebcbien CreditAttribution: Sebcbien commentedHI,
I try for drupal 8, but I can't adapt the code! i obtain http ajax error 200...
My hierarchy is:
site (ief field: field_secteur) --> secteur (ief fields: field_voie_1_l and field_voie_x_l)
I would like to get some url like that:
mydomain.com/site-name/secteur-name/voie-1-l-name
help me please..
Comment #18
dcam CreditAttribution: dcam as a volunteer commentedThe original question was answered. The same question for D8 was also asked in #2979710: How to obtain "parent-title/child-title" URL path for child nodes for D8 and answered there. Closing.