Verified Commit fc676da8 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

task: #2171397 Deprecate remaining options.module functions

By: yched
By: plopesc
By: sun
By: berdir
By: hardik_patel_12
By: ankithashetty
By: joachim
By: vsujeetkumar
By: claudiu.cristea
By: dhirendra.mishra
By: geek-merlin
By: nicxvan
By: smustgrave
By: larowlan
By: oily
By: quietone
By: longwave
By: daffie
parent bbe11d6d
Loading
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -453,6 +453,11 @@ function drupal_static_reset($name = NULL): void {
      \Drupal::service('cache.memory')->invalidateTags(['file_references']);
      break;

    case 'options_allowed_values':
      @trigger_error("Calling drupal_static_reset() with 'options_allowed_values' as argument is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use \Drupal::cache('memory')->delete('options.allowed_values') instead. See https://www.drupal.org/node/3572185", E_USER_DEPRECATED);
      \Drupal::cache('memory')->delete('options.allowed_values');
      break;

  }
  drupal_static($name, NULL, TRUE);
}
+7 −1
Original line number Diff line number Diff line
@@ -140,7 +140,13 @@ function hook_field_storage_config_update_forbid(FieldStorageConfigInterface $fi
    $allowed_values = $field_storage->getSetting('allowed_values');
    $prior_allowed_values = $prior_field_storage->getSetting('allowed_values');
    $lost_keys = array_keys(array_diff_key($prior_allowed_values, $allowed_values));
    if (_options_values_in_use($field_storage->getTargetEntityTypeId(), $field_storage->getName(), $lost_keys)) {
    $result = \Drupal::entityQuery($field_storage->getTargetEntityTypeId())
      ->condition($field_storage->getName() . '.value', $lost_keys, 'IN')
      ->count()
      ->accessCheck(FALSE)
      ->range(0, 1)
      ->execute();
    if ($result) {
      throw new FieldStorageDefinitionUpdateForbiddenException("A list field '{$field_storage->getName()}' with existing data cannot have its keys changed.");
    }
  }
+2 −2
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ function hook_options_list_alter(array &$options, array $context) {
/**
 * Provide the allowed values for a 'list_*' field.
 *
 * Callback for options_allowed_values().
 * Callback for \Drupal\options\OptionsAllowedValuesInterface::allowedValues().
 *
 * 'list_*' fields can specify a callback to define the set of their allowed
 * values using the 'allowed_values_function' storage setting.
@@ -76,7 +76,7 @@ function hook_options_list_alter(array &$options, array $context) {
 *   values for all possible entities and bundles.
 *
 * @ingroup callbacks
 * @see options_allowed_values()
 * @see \Drupal\options\OptionsAllowedValuesInterface::getAllowedValues()
 * @see \Drupal\options_test\OptionsAllowedValues::simpleValues()
 * @see \Drupal\options_test\OptionsAllowedValues::dynamicValues()
 */
+14 −31
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\options\OptionsAllowedValuesInterface;

/**
 * Returns the array of allowed values for a list field.
@@ -27,45 +28,27 @@
 *   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.
 *
 * @see callback_allowed_values_function()
 * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use
 *   \Drupal\options\OptionsAllowedValuesInterface::allowedValues() instead.
 *
 * @see https://www.drupal.org/node/3572185
 */
function options_allowed_values(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL) {
  $allowed_values = &drupal_static(__FUNCTION__, []);

  $cache_keys = [$definition->getTargetEntityTypeId(), $definition->getName()];
  if ($entity) {
    $cache_keys[] = 'entity';
  }
  $cache_id = implode(':', $cache_keys);

  if (!isset($allowed_values[$cache_id])) {
    $function = $definition->getSetting('allowed_values_function');
    // If $cacheable is FALSE, then the allowed values are not statically
    // cached. See OptionsAllowedValues::dynamicValues() for an example of
    // generating dynamic and uncached values.
    $cacheable = TRUE;
    if (!empty($function)) {
      $values = $function($definition, $entity, $cacheable);
    }
    else {
      $values = $definition->getSetting('allowed_values');
    }

    if ($cacheable) {
      $allowed_values[$cache_id] = $values;
    }
    else {
      return $values;
    }
  }

  return $allowed_values[$cache_id];
  @trigger_error(__METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use \Drupal\options\OptionsAllowedValuesInterface::allowedValues() instead. See https://www.drupal.org/node/3572185', E_USER_DEPRECATED);
  return \Drupal::service(OptionsAllowedValuesInterface::class)->getAllowedValues($definition, $entity);
}

/**
 * Checks if a list of values are being used in actual field values.
 *
 * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. The logic
 *   has been moved into \Drupal\options\Hook\OptionsHooks::hasValuesInUse()
 *   protected method and no replacement is provided.
 *
 * @see https://www.drupal.org/node/3572185
 */
function _options_values_in_use($entity_type, $field_name, $values) {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. The logic has been moved into \Drupal\options\Hook\OptionsHooks::hasValuesInUse() protected method and no replacement is provided. See https://www.drupal.org/node/3572185', E_USER_DEPRECATED);
  if ($values) {
    $result = \Drupal::entityQuery($entity_type)
      ->condition($field_name . '.value', $values, 'IN')
+5 −0
Original line number Diff line number Diff line
parameters:
  options.skip_procedural_hook_scan: true

services:
  Drupal\options\OptionsAllowedValuesInterface:
    class: Drupal\options\OptionsAllowedValues
    autowire: true
Loading