Come together with the global Drupal community in Rotterdam, 28 Sept – 1 Oct 2026. Sessions, contribution, connection, and Early Bird savings until 8 June.
Is there a use case why we should allow it? I think 99% people don't want it, and adding a setting for it complicates things. If somebody wants self reference they can hook_query_alter()
IMO, it should prevent from self referring. this is error happen when self referring:
EntityMalformedException: Kehilangan sifat berkas pada entiti jenis taxonomy_term. in entity_extract_ids() (line 7633 of /var/www/portal/includes/common.inc).
most probable there is no problem unless if the content has node_access capability. and I'm sure it would replicate the error message when some one using back references module, extending entity references module it self.
it would be nice to have a sitewide and/or per-widget checkbox setting to allow for self-entityreferenceing, and have that self-referencing prevented by default.
Currently, you can prevent self-references by using a View for the reference field entity selection.
You have to create the View as normal, just add a Contextual filter: Node ID and default value taken from Node URL. In the filter settings, there is an option:
Exclude
If selected, the numbers entered for the filter will be excluded rather than limiting the view.
Here is a patch. I've added checkbox "Allow self reference" disabled by default on Views and Generic handlers. It adds condition <> $entity_id if checkbox is unchecked and handler instance has attached entity.
Added simpletest coverage for UI and Generic handler. I would like to add full test coverage for Views handler, but wondering if it's possible without dependencies[] = views for whole module?
+++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
@@ -68,6 +70,14 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio
+ '#title' => t('Allow to choose self reference.'),
+ '#description' => t('If checked you will be able to set reference to self entity. Disabled by default'),
titles mostly have no dots at the end, use dot for descriptions, please
The standard in core is that field settings exposed as checkboxes use int default, since checkboxes save a '0' or '1' value, and not booleans. See text_field_info() and the 'text_processing' setting for an example.
I found this functionality is useful/musthave when CER is configured.
For those who applied non-commited patch #2010898: Use tokens for entity selection view arguments may get error while patching EntityReference_SelectionHandler_Views.class.php file. So applying patch manually is must until that patch/this get committed.
Observations:
This patch only works at form level and doesn't prevent referencing self when value is submitted manually.
Either checkbox should be off or change the
If checked you will be able to set reference to self entity. Disabled by default
to
If checked you will be able to set reference to self entity. Enabled by default.
following notice is showing on node create/edit pages when entity views selection is used.
Notice: Undefined index: allow_self_reference in EntityReference_SelectionHandler_Views->initializeView() (line 111 of /public/sites/all/modules/entityreference/plugins/selection/EntityReference_SelectionHandler_Views.class.php).
I've taken @m1r1k patch at #24 and applied it to the latest 7.x-1.x-dev (2017-May-16). Added @bernig's suggestion to catch empty entity ID for new entities to be created.
Thanks! Tests passed. This needs review. Sadly, I don't have bandwidth to do it now. Hopefully someone will have a chance.
Meanwhile, does anyone know if this issue affects the version of entity_reference in D8 core? I searched the core issue queue and didn't find anything relevant. If there's an open issue, it'd be great to link it as a related or even the parent issue for this. And/or confirming if D8 already handles this or not. If not, we should open a separate issue in the core queue and cross link it here instead of moving this issue between projects as it moves through the porting/backporting hoops.
tested - this is the form that appears when you select "Views: Filter by an entity reference view" in the entity reference field's "Entity selection" section. Works as designed
Comments
Comment #1
damien tournoud commentedYes, that's by design, because there is no general reason to prevent it. Let's make this a feature request.
Comment #2
amitaibuIs there a use case why we should allow it? I think 99% people don't want it, and adding a setting for it complicates things. If somebody wants self reference they can hook_query_alter()
Comment #3
parasolx commentedIMO, it should prevent from self referring. this is error happen when self referring:
EntityMalformedException: Kehilangan sifat berkas pada entiti jenis taxonomy_term. in entity_extract_ids() (line 7633 of /var/www/portal/includes/common.inc).most probable there is no problem unless if the content has node_access capability. and I'm sure it would replicate the error message when some one using back references module, extending entity references module it self.
Comment #4
jm.federico commentedIMO it should be an option to "allow" self reference, and keep it disabled by default.
I agree with Amitaibu that 99% of the time it won't be needed.
Comment #5
myselfhimself commentedHello,
it would be nice to have a sitewide and/or per-widget checkbox setting to allow for self-entityreferenceing, and have that self-referencing prevented by default.
Comment #6
Sinan Erdem commentedCurrently, you can prevent self-references by using a View for the reference field entity selection.
You have to create the View as normal, just add a Contextual filter: Node ID and default value taken from Node URL. In the filter settings, there is an option:
Exclude
If selected, the numbers entered for the filter will be excluded rather than limiting the view.
Comment #7
m1r1k commentedComment #8
klonos...coming from #2132215: Validation for not allowing adding an issue relation to itself.
Comment #9
m1r1k commentedHere is a patch. I've added checkbox "Allow self reference" disabled by default on Views and Generic handlers. It adds condition
<> $entity_idif checkbox is unchecked and handler instance has attached entity.Added simpletest coverage for UI and Generic handler. I would like to add full test coverage for Views handler, but wondering if it's possible without
dependencies[] = viewsfor whole module?Comment #10
andypostJust a minor nitpicks,
trailing white-space
it awesome to make this inline with getInstance(), otoh does this breaks other contrib modules?
suppose better
if (isset($this->entity) && empty($this->field[....])
because setting could not exist in fields that saved before this version
check for default value! ++
wrong indent
Comment #11
m1r1k commentedComment #12
andypostmissing dot at the end
titles mostly have no dots at the end, use dot for descriptions, please
Comment #13
m1r1k commentedHere is new patch with interdiff for #9
Comment #14
andypostGreat!
Comment #15
tvn commentedThanks everyone!
Comment #16
damien tournoud commentedI think the default value should be TRUE here (and it should be a boolean, not an int):
The way the current patch is built is also going to change the behavior for existing fields, which is not acceptable.
Comment #17
dave reidThe standard in core is that field settings exposed as checkboxes use int default, since checkboxes save a '0' or '1' value, and not booleans. See text_field_info() and the 'text_processing' setting for an example.
Comment #18
m1r1k commentedSwitch default value
Comment #20
m1r1k commentedSmall test correction
Comment #21
m1r1k commentedComment #22
andypostmakes no sense!
should be if !empty then no need to change test!
Comment #23
andypostComment #24
m1r1k commentedYeh, my bad.
But we still should change test to make him expects Allowed self reference by default.
Comment #25
nithinkolekar commentedI found this functionality is useful/musthave when CER is configured.
For those who applied non-commited patch #2010898: Use tokens for entity selection view arguments may get error while patching EntityReference_SelectionHandler_Views.class.php file. So applying patch manually is must until that patch/this get committed.
Observations:
to
Comment #27
juliangb commentedWhat more needs to happen for this feature to be committed?
Comment #28
bernig commentedI had a bug with #24 where when creating a new content, the entity reference list was empty. However it worked fine when editing an existing node.
I solved that by editing EntityReference_SelectionHandler_Generic.class.php (line 280) :
$entity_idwas empty ($this->entitywas the current user, for some reason), so I added a check on $entity_id.from :
to :
I'm sorry I don't have enough time right now to issue a patch but if anyone experiences the same bug, that's how I fixed it.
Comment #29
johnrosswvsu commentedI've taken @m1r1k patch at #24 and applied it to the latest 7.x-1.x-dev (2017-May-16). Added @bernig's suggestion to catch empty entity ID for new entities to be created.
I hope this helps.
Comment #31
johnrosswvsu commentedAdding a new patch that fixes the Test fails.
Comment #32
johnrosswvsu commentedComment #33
dwwThanks! Tests passed. This needs review. Sadly, I don't have bandwidth to do it now. Hopefully someone will have a chance.
Meanwhile, does anyone know if this issue affects the version of entity_reference in D8 core? I searched the core issue queue and didn't find anything relevant. If there's an open issue, it'd be great to link it as a related or even the parent issue for this. And/or confirming if D8 already handles this or not. If not, we should open a separate issue in the core queue and cross link it here instead of moving this issue between projects as it moves through the porting/backporting hoops.
Thanks,
-Derek
Comment #34
chris matthews commentedUnassigned m1r1k
Comment #35
pdcarto commentedtested - works as designed
tested - works as designed
tested - this is the form that appears when you select "Views: Filter by an entity reference view" in the entity reference field's "Entity selection" section. Works as designed
tested - works as designed
Comment #36
pdcarto commentedComment #37
pdcarto commentedResults of running simpletests is attached. All green.
Comment #38
joseph.olstadabove patch rerolled.
Comment #40
joseph.olstadComment #41
joseph.olstadComment #42
joseph.olstadOops, this is much closer to patch 31
Comment #45
joseph.olstadok there's a core bug that is exposed by this patch.
I'm going to hold off merging this until the core bug is resolved.
There is a patch available for core.
Core patch fixes issue when using PHP 7.4, 8.0, 8.1, 8.2
#3314719-5: Notice: Trying to access array offset on value of type null in _field_write_instance()
Comment #46
joseph.olstadpostponed on related core bug affecting PHP 7.4, 8.0, 8.1, 8.2+