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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amateescu’s picture

Status: Active » Needs review
FileSize
2.58 KB

Here's how I think it should look like.

sun’s picture

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.

amitaibu’s picture

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?

larowlan’s picture

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

larowlan’s picture

Status: Needs work » Needs review
FileSize
4 KB
3.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.

amitaibu’s picture

Status: Needs review » Reviewed & tested by the community

Seems ok.

chx’s picture

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

$erCreate
  ->entityType($entity_type)
  ->bundle($bundle)...
 

or is that overkill?

webchick’s picture

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!

ParisLiakos’s picture

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

amateescu’s picture

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.