Chris@0: id() == 'field_option') { Chris@0: // Change the label of the empty option. Chris@0: $options['_none'] = t('== Empty =='); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provide the allowed values for a 'list_*' field. Chris@0: * Chris@0: * Callback for options_allowed_values(). Chris@0: * Chris@0: * 'list_*' fields can specify a callback to define the set of their allowed Chris@0: * values using the 'allowed_values_function' storage setting. Chris@0: * Chris@0: * That function will be called: Chris@0: * - either in the context of a specific entity, which is then provided as the Chris@0: * $entity parameter, Chris@0: * - or for the field generally without the context of any specific entity or Chris@0: * entity bundle (typically, Views needing a list of values for an exposed Chris@0: * filter), in which case the $entity parameter is NULL. Chris@0: * This lets the callback restrict the set of allowed values or adjust the Chris@0: * labels depending on some conditions on the containing entity. Chris@0: * Chris@0: * For consistency, the set of values returned when an $entity is provided Chris@0: * should be a subset of the values returned when no $entity is provided. Chris@0: * Chris@0: * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition Chris@0: * The field storage definition. Chris@0: * @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity Chris@0: * (optional) The entity context if known, or NULL if the allowed values are Chris@0: * being collected without the context of a specific entity. Chris@0: * @param bool &$cacheable Chris@0: * (optional) If an $entity is provided, the $cacheable parameter should be Chris@0: * modified by reference and set to FALSE if the set of allowed values Chris@0: * returned was specifically adjusted for that entity and cannot not be reused Chris@0: * for other entities. Defaults to TRUE. Chris@0: * Chris@0: * @return array Chris@0: * The array of allowed values. Keys of the array are the raw stored values Chris@0: * (number or text), values of the array are the display labels. If $entity Chris@0: * is NULL, you should return the list of all the possible allowed values in Chris@0: * any context so that other code (e.g. Views filters) can support the allowed Chris@0: * values for all possible entities and bundles. Chris@0: * Chris@0: * @ingroup callbacks Chris@0: * @see options_allowed_values() Chris@0: * @see options_test_allowed_values_callback() Chris@0: * @see options_test_dynamic_values_callback() Chris@0: */ Chris@0: function callback_allowed_values_function(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) { Chris@0: if (isset($entity) && ($entity->bundle() == 'not_a_programmer')) { Chris@0: $values = [ Chris@0: 1 => 'One', Chris@0: 2 => 'Two', Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $values = [ Chris@0: 'Group 1' => [ Chris@0: 0 => 'Zero', Chris@0: 1 => 'One', Chris@0: ], Chris@0: 'Group 2' => [ Chris@0: 2 => 'Two', Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: return $values; Chris@0: }