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.
Currently we have 3 actions on prepopulated fields :
- "Do nothing" which do pretty well what it says :)
- "Hide field" which removes the field from the UI
- "Disable field" which let the field as is but disabled, not really aestetic.
I propose to add a new action which would hide the real field and show an "item" field instead containing the entity title.
Patch coming soon.
Comments
Comment #1
DuaelFrHere is the patch !
I added a few tests but feel free to improve them as I am not really good for it.
This patch is part of the #1day1patch initiative.
Comment #3
DuaelFrI said I was not good at patches...
Comment #4
chipway CreditAttribution: chipway commented#1: entityreference_prepopulate-add_action_option_value-2028735-1.patch queued for re-testing.
Comment #6
amitaibuCan you explain this part?
value => item
Comment #7
DuaelFrThe first part removes the entity ID to only let its title (better for UX I think).
I don't understand your second comment, the key of my option is named 'value' to make reference to the form field type used to hide the field.
Comment #8
amitaibu> The first part removes the entity ID to only let its title (better for UX I think).
Can't we just use entity_label() form the entity ID instead?
Comment #9
DuaelFrThe entity_id is extracted by entityreference with a regular expression so I think using regexp is ok.
If you think entity_label is lighter and more understandable than my method, I have no reason to refuse :)
Comment #10
RoySegall CreditAttribution: RoySegall commentedBasicly the test is ok expect for two things:
1. You try to verify that the field was replaced by the label but you weren't in the node edit/create form, so the text was not available.
2. You didn't verify in the test that the value option is applied on the edit of the node. I added this part in test.
Comment #11
amitaibuI suspect you test this only with autocomplete, and not with a select list (where we won't have the entity ID in brackets). So let's use entity_label().
Comment #12
DuaelFrThank you Roy !
If I understand what you say in your first point, the previous test "Hide the field." cannot be valid because it is missing the
$this->drupalGet('node/add/' . $this->node1->type, $options);
too.Here is a new patch to use entity_label instead of a regexp. I let you fix the "Hide the field" test in another issue.
Comment #14
DuaelFrWhat happened ?
I don't understand why this patch didn't pass...
Comment #15
RoySegall CreditAttribution: RoySegall commentedI'm trying to figure now.
Comment #16
RoySegall CreditAttribution: RoySegall commentedAfter some investigation the correct way is:
Because when creating the entity the $id don't have a value. Any way, i think this is a bit messy.
Also, have you tried this feature with select list as @amitaibu mention before? With select list the the feature don't work because element_children($form[$field_name][$lang]) is returning nothing is the list will not be a markup but remains a select list.
Comment #17
amitaibuInstead of working with the form try calling entityreference_prepopulate_get_values()
Comment #18
DuaelFr@Roy : I think $id is the edited entity id not the referenced entity id so I think I should not use it here.
I fully rewrited my function to use more
$ids
which is populated byentityreference_prepopulate_get_values()
and to find something that will work for all widgets. I think that tests will continue to work as they only test the concept (*crossing fingers*).Comment #20
amitaibumaybe $tmp_entity => $entity_item ?
XSS! :) use check_plain()
In that case don't use #markup, use the #theme => 'item_list'
Comment #21
DuaelFrDone !
Comment #23
DuaelFrTests fixed (I hope)
Comment #24
RoySegall CreditAttribution: RoySegall commentedI'm attaching a minor fix the patch: i try to verify that when the widget type is select list and the user editing the entity the label of the entities should appear - that didn't occurred. When editing, the $ids variable does not holds the populated field values. I fixed that and verified that in the simple test.
Comment #25
RoySegall CreditAttribution: RoySegall commentedFixing typo.
Comment #26
amitaibuhas => have
We can just create a new element as assign to to $form[$field_name][$lang] -- instead of iterating and unsetting the existing element.
Comment #27
RoySegall CreditAttribution: RoySegall commentedremoving the unset is working for me but for the simple test can't handle it:
Comment #28
RoySegall CreditAttribution: RoySegall commentedComment #29
DuaelFrI had styles issues without unsetting #theme on multiple fields.
You are overriding $form[$field_name][$lang] so the values will be lost on the form submission. This is the reason why I used $lang . '_label' as form field key.
Comment #30
RoySegall CreditAttribution: RoySegall commented@DuaelFr you'r right with you said. I don't know how the prepopulated save the value in the fields but i made a few changes.
Comment #31
amitaibuhas => have
Why not use $wrapper?
'und' should probably be $langcode?
XSS - make sure to use check_plain()
Comment #32
RoySegall CreditAttribution: RoySegall commentedComment #33
RoySegall CreditAttribution: RoySegall commentedComment #34
amitaibuno need for this, just do $ids = $wrapper->{$field_name}->value(array('identifier' => TRUE));
Comment #35
amitaibubtw, When would we have an $ids with values?
Comment #36
RoySegall CreditAttribution: RoySegall commentedWe have $ids when we in a URL like: node/add/article?field_node_ref=1
Comment #37
chris_h CreditAttribution: chris_h commentedI've been using this in production for nearly a year now and works great
Comment #38
jgullstr CreditAttribution: jgullstr commentedAdded #2126203: new action type in prepopulate : Display rendered entity as related issue, as this behavior is possible to replicate using a display mode.
Comment #39
hbjosemaria CreditAttribution: hbjosemaria commentedIf this works, why isn't it commited yet? Now I have to patch ERP :<
Comment #40
delacosta456 CreditAttribution: delacosta456 commentedhello ... hi please
for this issue , which patch can apply safely please ?
Comment #41
Anybody#36 was committed to 7.x-1.x-dev! :) Thanks all!