欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

function list_allowed_values

7.x list.modulelist_allowed_values($field, $instance = NULL, $entity_type = NULL, $entity = NULL)

Returns the array of allowed values for a list field.

The strings are not safe for output. Keys and values of the array should be sanitized through field_filter_xss() before being displayed.

Parameters

$field: The field definition.

$instance: (optional) A field instance array. Defaults to NULL.

$entity_type: (optional) The type of entity; e.g. 'node' or 'user'. Defaults to NULL.

$entity: (optional) The entity object. Defaults to NULL.

Return value

The array of allowed values. Keys of the array are the raw stored values (number or text), values of the array are the display labels.

3 calls to list_allowed_values()
2 string references to 'list_allowed_values'

File


  • modules/

    field/

    modules/

    list/

    list.module, line 235

  • Defines list field types that can be used with the Options module.


Code

function list_allowed_values($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {  $allowed_values = &drupal_static(__FUNCTION__, array());  if (!isset($allowed_values[$field['id']])) {    $function = $field['settings']['allowed_values_function'];    // If $cacheable is FALSE, then the allowed values are not statically    // cached. See list_test_dynamic_values_callback() for an example of    // generating dynamic and uncached values.    $cacheable = TRUE;    if (!empty($function) && function_exists($function)) {      $values = $function($field, $instance, $entity_type, $entity, $cacheable);
    }    else {      $values = $field['settings']['allowed_values'];
    }    if ($cacheable) {      $allowed_values[$field['id']] = $values;
    }    else {      return $values;
    }
  }  return $allowed_values[$field['id']];
}

Comments

hermes_costell’s picture

<?php
/*
assuming there's a field named machine name is 'field_my_field_name' whose type is a list (float, integer or text), and you want to get the values that were entered into the "Allowed values list" textarea on the edit screen for the field
*/
$all_fields_on_my_website = field_info_fields();
$allowed_values= list_allowed_values($all_fields_on_my_website["field_my_field_name"]);

/*
$allowed_values is now an array of $key=>value pairs that can be dropped into the '#options' value for a form field array, for instance
*/
//example:
$form = array();
$form['my_form_element'] = array(
'#type' => 'select',
'#title' => t('Choose something, sucka!'),
'#description' => t('Descriptive text under form elements rather than above them is a great UX choice, really.'),
'#options' => $allowed_values,
);
return $form;

a.milkovsky’s picture

nice title '#title' => t('Choose something, sucka!') :)

ksemihin’s picture

Crazy russian developer :)

cdmo’s picture

Thanks for the tip on field_info_fields()

jamesmorrish’s picture

According to the documentation on field_info_fields(): "Use of this function should be avoided when possible, since it loads and statically caches a potentially large array of information".

Instead you can use:

$my_field = field_info_field('field_my_field_name');
$allowed_values= list_allowed_values($my_field);

dongtian’s picture

Thanks for your tips

coconutkitty’s picture

Yes, thank you! This was perfect for one field.

TanvirAhmad’s picture

This comes without None option.
How can we bring none with it.

TanvirAhmad’s picture

dark11star’s picture

When I try this (exactly) I get these messages:

Notice: Undefined index: allowed_values_function in list_allowed_values() (line 239 of /home/llpa/public_html/devel/modules/field/modules/list/list.module).
Notice: Undefined index: allowed_values in list_allowed_values() (line 248 of /home/llpa/public_html/devel/modules/field/modules/list/list.module).

Peter Törnstrand’s picture

To get a localized list of allowed values use: i18n_field_translate_allowed_values($field, $langcode)

SylvainM’s picture

In Drupal 8, you can use \Drupal\field\Entity\FieldConfig::loadByName('entity_type', 'bundle', 'field_name')->getSetting('allowed_values');

kamalMaroc’s picture

Not working:

Call to a member function getSetting() on null

albertski’s picture

I had the same issue but it turns out my bundle was wrong. This worked for me:

$region = FieldConfig::loadByName('user', 'user', 'field_region')->getSetting('allowed_values');

来自 https://api.drupal.org/api/drupal/modules%21field%21modules%21list%21list.module/function/list_allow...

普通分类: