Follow up to #1541792: Enable dynamic allowed list values function with additional context.
It's possible now to receive entity_type and entity in entityreference_options_list().

Files: 
CommentFileSizeAuthor
#16 1555894-entityreference-entity-context.patch11.84 KBDamien Tournoud
#13 1555894-entityreference-entity-context-13.patch11.78 KBamitaibu
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]
#13 interdiff.patch4.91 KBamitaibu
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch interdiff_9.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#10 1555894-entityreference-entity-context-10.patch7.93 KBamitaibu
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]
#10 interdiff.patch1.9 KBamitaibu
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch interdiff_8.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#9 1555894-entityreference-entity-context.patch7.54 KBDamien Tournoud
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]
#5 store_entity-1555894-5.patch4.87 KBitamar
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]
#1 store_entity-1555894-1.patch4.85 KBitamar
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]

Comments

itamar’s picture

Status:Active» Needs review
StatusFileSize
new4.85 KB
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]

Patch alters entityreference_options_list to accept entity_type and entity arguments.

Damien Tournoud’s picture

That makes sense.

<?php
-  protected function __construct($field, $instance) {
+  protected function
__construct($field, $instance, $entity_type, $entity) {
?>

Let's make those optional too, to make sure people understand that they might not be available.

itamar’s picture

The $entity_type and $entity arguments in the constructor actually are optional, because the constructor is invoked using getInstance() which has default values for them;

<?php
+ public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
?>
Damien Tournoud’s picture

I know, but let's make those optional too, to make sure people understand that they might not be available :)

itamar’s picture

StatusFileSize
new4.87 KB
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]

Amending the previous patch; Setting default values in EntityReference_SelectionHandler_Generic::_construct().

amitaibu’s picture

Damien Tournoud’s picture

<?php
-function entityreference_options_list($field, $instance = NULL) {
-  return
entityreference_get_selection_handler($field, $instance)->getReferencableEntities();
+function
entityreference_options_list($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
+  return
entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->getReferencableEntities();
?>

Thinking about this more, shouldn't we just add $entity_type and $entity as (optional) parameters to the ->getReferencableEntities()? Amitai, any thoughts?

amitaibu’s picture

> shouldn't we just add $entity_type and $entity as (optional) parameters to the ->getReferencableEntities()?

Indeed, at the beginning I thought about this option, however, I was thinking it is possible that one would call also validateReferencableEntities() needing the entity. Maybe also on entityFieldQueryAlter().
So I think that the proposed solution is more future proof to feature requests.

Damien Tournoud’s picture

StatusFileSize
new7.54 KB
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]

Ok, in that case, we need to fix or remove the caching in entityreference_get_selection_handler(). I just removed it in the attached patch and added some more context where we have it.

We still need to figure out how to pass the entity context to entityreference_autocomplete_callback().

amitaibu’s picture

StatusFileSize
new1.9 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch interdiff_8.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
new7.93 KB
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]

> we need to fix or remove the caching in entityreference_get_selection_handler()

Damz, what do you think about this caching? (attached interdiff)

Status:Needs review» Needs work

The last submitted patch, interdiff.patch, failed testing.

Damien Tournoud’s picture

Status:Needs work» Needs review

Damz, what do you think about this caching? (attached interdiff)

I started writing something like that, then decided that it was not worth it. We can decide later, can we focus on the autocomplete callback?

amitaibu’s picture

StatusFileSize
new4.91 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch interdiff_9.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
new11.78 KB
PASSED: [[SimpleTest]]: [MySQL] 66 pass(es).
[ View ]

Patch (based on #9 -- without objet cache) adds optional $entity to autocomplete.

Status:Needs review» Needs work

The last submitted patch, interdiff.patch, failed testing.

Damien Tournoud’s picture

Status:Needs work» Needs review

#13 looks good to me on visual inspection. Let's move forward with it and tag a rc2.

Damien Tournoud’s picture

Status:Needs review» Fixed
StatusFileSize
new11.84 KB

I ended up committing the following patch with some minor fixes.

amitaibu’s picture

Status:Fixed» Needs work
+++ b/entityreference.moduleundefined
@@ -680,25 +673,36 @@ function entityreference_field_widget_form(&$form, &$form_state, $field, $instan
+    unset($entity->nid);

Is this a debug leftover?

amitaibu’s picture

Status:Needs work» Fixed
Damien Tournoud’s picture

It was, I quickly tested what happens with unsaved entities. I caught it in time :)

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.