Comments

damien tournoud’s picture

Category: bug » feature

Yes, that's by design, because there is no general reason to prevent it. Let's make this a feature request.

amitaibu’s picture

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()

parasolx’s picture

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.

jm.federico’s picture

IMO 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.

myselfhimself’s picture

Hello,

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.

Sinan Erdem’s picture

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.

m1r1k’s picture

Assigned: Unassigned » m1r1k
Issue summary: View changes
Status: Active » Needs work
klonos’s picture

m1r1k’s picture

Status: Needs work » Needs review
StatusFileSize
new15.36 KB

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?

andypost’s picture

Just a minor nitpicks,

  1. +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
    @@ -149,6 +150,13 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select
    +    );
    +    ¶
    

    trailing white-space

  2. +++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
    @@ -9,12 +9,14 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio
    +  protected function __construct($field, $instance, $entity_type = NULL, $entity = NULL) {
    

    it awesome to make this inline with getInstance(), otoh does this breaks other contrib modules?

  3. +++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
    @@ -76,6 +86,11 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio
    +    $allow_self_reference = $this->field['settings']['handler_settings']['allow_self_reference'];
    ...
    +    if (!$allow_self_reference && isset($this->entity)) {
    

    suppose better
    if (isset($this->entity) && empty($this->field[....])

    because setting could not exist in fields that saved before this version

  4. +++ b/tests/entityreference.admin.test
    @@ -73,8 +77,10 @@ class EntityReferenceAdminTestCase extends DrupalWebTestCase {
    +    $this->assertNoFieldChecked('edit-field-settings-handler-settings-allow-self-reference');
    

    check for default value! ++

  5. +++ b/tests/entityreference.handlers.test
    @@ -594,4 +599,175 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
    +      'node_1' => (object) array(
    +          'type' => 'article',
    +          'status' => 1,
    +          'title' => 'First node',
    +          'uid' => 1,
    ...
    +      'node_2' => (object) array(
    +          'type' => 'article',
    +          'status' => 1,
    +          'title' => 'Second node',
    +          'uid' => 1,
    

    wrong indent

m1r1k’s picture

andypost’s picture

  1. +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
    @@ -269,6 +277,12 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select
    +    // Exclude self reference
    

    missing dot at the end

  2. +++ 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

m1r1k’s picture

Here is new patch with interdiff for #9

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Great!

tvn’s picture

Thanks everyone!

damien tournoud’s picture

Status: Reviewed & tested by the community » Needs work

I think the default value should be TRUE here (and it should be a boolean, not an int):

+      'allow_self_reference' => 0,

The way the current patch is built is also going to change the behavior for existing fields, which is not acceptable.

dave reid’s picture

and it should be a boolean, not an int

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.

m1r1k’s picture

Status: Needs work » Needs review
StatusFileSize
new1.53 KB
new17.86 KB

Switch default value

Status: Needs review » Needs work

The last submitted patch, 18: entityreference-prevent-reference-self-1364802-18.patch, failed testing.

m1r1k’s picture

Small test correction

m1r1k’s picture

Status: Needs work » Needs review
andypost’s picture

+++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
@@ -75,7 +75,8 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio
-      '#default_value' => empty($field['settings']['handler_settings']['allow_self_reference']) ? 0 : 1,
+      '#default_value' => empty($field['settings']['handler_settings']['allow_self_reference'])
+        ? 1 : $field['settings']['handler_settings']['allow_self_reference'],

makes no sense!

should be if !empty then no need to change test!

andypost’s picture

Status: Needs review » Needs work
m1r1k’s picture

Status: Needs work » Needs review
StatusFileSize
new17.79 KB
new1.64 KB

Yeh, my bad.
But we still should change test to make him expects Allowed self reference by default.

nithinkolekar’s picture

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:

  1. This patch only works at form level and doesn't prevent referencing self when value is submitted manually.
  2. 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.
  3. 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).

juliangb’s picture

What more needs to happen for this feature to be committed?

bernig’s picture

I 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_id was empty ($this->entity was the current user, for some reason), so I added a check on $entity_id.

from :

    // Exclude self reference.
    if (empty($this->field['settings']['handler_settings']['allow_self_reference']) && isset($this->entity)) {
      list($entity_id,,) = entity_extract_ids($this->entity_type, $this->entity);
      $query->entityCondition('entity_id', $entity_id, '<>');
    }

to :

    // Exclude self reference.
    if (empty($this->field['settings']['handler_settings']['allow_self_reference']) && isset($this->entity)) {
      list($entity_id, ,) = entity_extract_ids($this->entity_type, $this->entity);

      if (!empty($entity_id)) {
        $query->entityCondition('entity_id', $entity_id, '<>');
      }
    }

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.

johnrosswvsu’s picture

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.

I hope this helps.

Status: Needs review » Needs work

The last submitted patch, 29: entityreference-prevent-reference-self-1364802-29.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

johnrosswvsu’s picture

Adding a new patch that fixes the Test fails.

johnrosswvsu’s picture

dww’s picture

Status: Needs work » Needs review

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.

Thanks,
-Derek

chris matthews’s picture

Assigned: m1r1k » Unassigned

Unassigned m1r1k

pdcarto’s picture

  1. +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
    @@ -149,6 +150,13 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select
    +    $form['allow_self_reference'] = array(
    

    tested - works as designed

  2. +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
    @@ -273,6 +281,15 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select
    +    // Exclude self reference.
    

    tested - works as designed

  3. +++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
    @@ -88,6 +88,15 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio
    +    $form['allow_self_reference'] = array(
    

    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

  4. +++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
    @@ -96,6 +105,11 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio
    +    $entity_id_to_exclude = FALSE;
    

    tested - works as designed

pdcarto’s picture

Status: Needs review » Reviewed & tested by the community
pdcarto’s picture

Results of running simpletests is attached. All green.

joseph.olstad’s picture

above patch rerolled.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 38: entityreference-prevent-reference-self-1364802-38.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

joseph.olstad’s picture

StatusFileSize
new15.73 KB
joseph.olstad’s picture

Status: Needs work » Needs review
joseph.olstad’s picture

StatusFileSize
new14.5 KB

Oops, this is much closer to patch 31

The last submitted patch, 40: entityreference-prevent-reference-self-1364802-40.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 42: entityreference-prevent-reference-self-1364802-41.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

joseph.olstad’s picture

Status: Needs work » Reviewed & tested by the community

ok 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()

joseph.olstad’s picture

Status: Reviewed & tested by the community » Postponed

postponed on related core bug affecting PHP 7.4, 8.0, 8.1, 8.2+