Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Would it be possible by using a behavior plugin to extend the entityreference field with another textfield?
Comments
Comment #0.0
SebCorbin CreditAttribution: SebCorbin commentedUpdated issue summary.
Comment #1
Andre-Banything on this?
Comment #2
SebCorbin CreditAttribution: SebCorbin commentedFirst, create a behavior:
And create a directory
behavior
within another namedplugins
. Add a file name for exemple your_field.inc in it:And add a file in the same
behavior
directory named after the plugin,YourFieldBehavior.class.php
:The schema part is now done, flush the caches and the behavior should appear in the entityreference field settings.
Now we need to alter the widget, return to
mymodule.module
:Yup that's it, the textfield will be saved along with the reference!
You may want to add it to the display:
Now maybe this should be added to the documentation?
Comment #4
dgtlmoon CreditAttribution: dgtlmoon commentedI'de like to re-open this issue because it appears that the above approach only works correctly when the widget is of type 'autocomplete', using 'select' widget the $element structure that is passed-in doesn't seem to allow extra fields to be appended to its array in such a fashion - they are ignored
Comment #5
Andre-BI got an implementation here as well: https://www.drupal.org/sandbox/baumeier.it/2078043 I did not diff my with the one provided in #2 though
Comment #6
arcaicI have been trying to do this with no success.....
I have implemented the code of SebCorbin and got the behaviors working in that I can create an entity reference in a content type, select the behavior (and have its settings form displayed), I can see the schema_alter firing and I can create a node and enter the text in the text field I've added (employee_name) and choose a node for the reference.
Problem is nothing is saved. No errors no nothing. Not even the node reference is saved.
Thing is I don't really understand how or where the data I enter in my text field is supposed to get saved. Schema alter for the behavior doesn't actually modify the database, right? If correct then where should I add the code that modifies the database table for the created fields?
Ideally I would have thought it should be done when the behavior is chosen when the field is created or at the very least when the field is saved. How to do this though?
My use case is to allow a user to select a node reference from a content type of employee but if the employee is not on the site allow their name as text instead.
Any help would be much appreciated.
Many thanks
Andy