2

I spend hours to figure this out but no result. I want to auto-populate a field using entity references and views.

  • A field has a user entity reference
  • Using filters, I made a view narrowing down the results to 1 (the result I want to populate the field with)
  • I linked the field with the search result using "Views : ..." as a reference method.

The only thing that's preventing me from succeeding is that the widget always shows 2 results instead of 1 (SEE SCREENSHOT): - None - (Default) My result (Dreece in the screenshot)

so the "-None-" is preventing me from succeeding to do the auto-populate.

Any way to (NON PROGRAMMATICALLY) ommit the "-None-" (or put it as the second option)? - By configuring the View - By using View arguments?

Many thanks in advance!!!enter image description here

3

As soon as you make this field required it will have no - None - option anymore.

1

You can use function hook_element_info_alter D7:

function yourmodule_element_info_alter(array &$types) {
  if (isset($types['select'])) {
    $types['select']['#process'][] = 'remove_element_na';
  }
}

function remove_element_na($element) {
  if (isset($element['#field_name']) && $element['#field_name'] == 'field_name') {
    unset($element['_none']);
  }
  return $element;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.