Coming from #1911080: Replace menu node form additions with entity reference field, I think this little helper function could be split as a small subtask.

Files: 
CommentFileSizeAuthor
#5 entity-reference-util-1924774.5.interdiff.txt3.88 KBlarowlan
#5 entity-reference-util-1924774.5.patch4 KBlarowlan
PASSED: [[SimpleTest]]: [MySQL] 52,367 pass(es).
[ View ]
#1 1924774.patch2.58 KBamateescu
PASSED: [[SimpleTest]]: [MySQL] 52,295 pass(es).
[ View ]

Comments

Status:Active» Needs review
StatusFileSize
new2.58 KB
PASSED: [[SimpleTest]]: [MySQL] 52,295 pass(es).
[ View ]

Here's how I think it should look like.

At minimum, the $entity_type and $bundle parameters should come first, I think.

Can't comment on the architectural idea of this right now, as I'm not up to speed on entity reference field yet.

Status:Needs review» Needs work

This can be handy, there are many places in the tests we can use.

+++ b/core/modules/entity_reference/entity_reference.moduleundefined
@@ -420,6 +420,65 @@ function entity_reference_query_entity_reference_alter(AlterableInterface $query
+function entity_reference_create_instance($field_name, $field_label, $entity_type, $bundle, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = array()) {

Indeed, lets move $entity_type and $bundle before. Also, maybe we $taget_entity_type => $target_type

+++ b/core/modules/entity_reference/entity_reference.moduleundefined
@@ -420,6 +420,65 @@ function entity_reference_query_entity_reference_alter(AlterableInterface $query
+  // If a field type we know should exist isn't found, clear the field cache.
+  if (!field_info_field_types('entity_reference')) {
+    field_cache_clear();

When will we have something like this?

+++ b/core/modules/entity_reference/entity_reference.moduleundefined
@@ -420,6 +420,65 @@ function entity_reference_query_entity_reference_alter(AlterableInterface $query
+ // If a field type we know should exist isn't found, clear the field cache.
+ if (!field_info_field_types('entity_reference')) {
+ field_cache_clear();
When will we have something like this?

This can happen if someone uses an install profile that enables both entity_reference and creates a field in the same process from memory, commerce_* modules uses it fairly consistently

Status:Needs work» Needs review
StatusFileSize
new4 KB
PASSED: [[SimpleTest]]: [MySQL] 52,367 pass(es).
[ View ]
new3.88 KB

This patch re-orders the params as per #2 but leaves $target_entity_type intact, I think $target_type is not as self-evident as $target_entity_type.

Also refactors Drupal\entity_reference\Tests\EntityReferenceItemTest::setUp() to use this utility so we have some test coverage.

Status:Needs review» Reviewed & tested by the community

Seems ok.

I am leaving this at RTBC 'cos it's fine as it is. Still, perhaps it could be even better? Should this leave in .install as it's extremely likely to be used install time? Orshould this live in a class with one named setter for each argument like

<?php
$erCreate
 
->entityType($entity_type)
  ->
bundle($bundle)...

?>

or is that overkill?

Status:Reviewed & tested by the community» Fixed

Hm. Don't think overkill, but (at least in the Old World Order™) it's more common to have wrapper functions for this type of thing than classes you need to dig through 27 directories to find. :)

Committed and pushed to 8.x. Thanks!

This is very useful, i am already using it over at #15266: Replace aggregator category system with taxonomy but i wish there was a way to pass widget info..right now it uses the default autocomplete one, so i have to load back the instance to change the widget:( still though, a lot less lines of code i have to write:)

That part will be solved by a (hopefully) future version of entity_get_form(), similar to entity_get_display(), basically form modes :)

Status:Fixed» Closed (fixed)

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