Using this widget in an entityref fields that points to, say, nodes, leads to nothing in the form and loads of error messages on saving (well I assume that was the cause of them...)

I'm thinking of how best to limit this, and I reckon the best way is to add an #element_validate handler in hook_field_widget_settings_form() and check the settings make sense there.

The alternative is to use hook_form_alter on the 'Widget type' tab, but doesn't stop a bad combination being saved in the field settings or when the field is initially created.

The validate function needs to be able to determine the referenced entity type from different fields. I've encountered this before in my reference_option_limit module, and solved it with a helper function like this:

125 /**
126 * Helper to get the entity type a reference field refers to.
127 *
128 * @param $field
129 * A field definition.
130 *
131 * @return
132 * The machine name of the entity type.
133 */
134 function reference_options_limit_get_referred_entity_type($field) {
135 switch ($field['type']) {
136 case 'taxonomy_term_reference':
137 return 'taxonomy_term';
138 case 'entityreference':
139 return $field['settings']['target_type'];
140 }
141 }

Comments

joachim’s picture

Hmm, adding a validation handler in inline_entity_form_field_widget_settings_form() still means an admin user can create a broken configuration if they go in via the 'Field settings' tab rather than the main edit tab.

There's a lot of places where an admin user can break this.

bojanz’s picture

Title: don't allow the widget to be used on entityref fields that point to unsupported entities » Errors when using IEF on an unsupported entity type.
Status: Active » Fixed

Pushed a commit, the errors should now be fixed.

As you've seen, due to "entity_type" being a setting for entityreference fields, it is really not easy to warn properly when this happens.
When it comes to alternatives, I consider an error message on the add / edit page to be a bit too much, since you might be bothering a user, not the admin who configured the fields.
I consider the "unsupported entity type" an edge case and minor, so it's enough to just document it. If you can find a smarter solution, I'm all ears.

Status: Fixed » Closed (fixed)

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