0

For a required field using select list widget, if there is no options, the field will show only one "-Select one-" value on the node add/edit form page. So, how to know that by coding?

0

It will depend on the field type. For a list field with text/numeric values, the output of the field API function field_info_field() contains allowed values within its settings element. This field data can be fed into list_allowed_values() to retrieve either the static or dynamic list of allowed values for the field.

For example:

$field = field_info_field('field_myfield');
$values = list_allowed_values($field);
if (count($values) > 0) {
  // There are allow values
}
else {
  // There are no allowed values
}
  • It use your code snippet in a hook_form_alter to unset the submit button if there is no valid options in a required select list. This select list form element is an entity reference field populated by a entity reference views output. Unfortunately, even there is valid options in the select list, the submit button is still unset, and the page shows errors as below: – fishfree Oct 20 '16 at 2:27
  • : Notice: Undefined index: allowed_values_function in list_allowed_values() (line 239 of /home/wzzhang/www/reg/modules/field/modules/list/list.module). Notice: Undefined index: allowed_values in list_allowed_values() (line 248 of /home/wzzhang/www/reg/modules/field/modules/list/list.module). – fishfree Oct 20 '16 at 2:35
  • Then I change my custom module's execution weight to 999 with SQL: UPDATE system SET weight=999 WHERE name = 'mycustommodule'; The error still exists. – fishfree Oct 20 '16 at 2:37
  • This was tested with a text/numeric list, you should update the question so that it mentions it's an entity ref field. – Shawn Conn Oct 20 '16 at 4:27

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.