_options_values_in_use example

// Check that keys are valid for the field type.       foreach ($values as $key => $value) {
        if ($error = static::validateAllowedValue($key)) {
          $form_state->setError($element$error);
          break;
        }
      }

      // Prevent removing values currently in use.       if ($element['#field_has_data']) {
        $lost_keys = array_keys(array_diff_key($element['#allowed_values']$values));
        if (_options_values_in_use($element['#entity_type']$element['#field_name']$lost_keys)) {
          $form_state->setError($elementnew TranslatableMarkup('Allowed values list: some values are being removed while currently in use.'));
        }
      }

      $form_state->setValueForElement($element$values);
    }
  }

  /** * Extracts the allowed values array from the allowed_values element. * * @param string $string * The raw string to extract values from. * @param bool $has_data * The current field already has data inserted or not. * * @return array|null * The array of extracted key/value pairs, or NULL if the string is invalid. * * @see \Drupal\options\Plugin\Field\FieldType\ListItemBase::allowedValuesString() */

function hook_field_storage_config_update_forbid(\Drupal\field\FieldStorageConfigInterface $field_storage, \Drupal\field\FieldStorageConfigInterface $prior_field_storage) {
  if ($field_storage->getTypeProvider() == 'options' && $field_storage->hasData()) {
    // Forbid any update that removes allowed values with actual data.     $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)) {
      throw new \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException("A list field '{$field_storage->getName()}' with existing data cannot have its keys changed.");
    }
  }
}

/** * @} End of "defgroup field_types". */

/** * @defgroup field_widget Field Widget API * @{ * Define Field API widget types. * * Field API widgets specify how fields are displayed in edit forms. Fields of a * given @link field_types field type @endlink may be edited using more than one * widget. In this case, the Field UI module allows the site builder to choose * which widget to use. * * Widgets are Plugins managed by the * \Drupal\Core\Field\WidgetPluginManager class. A widget is a plugin annotated * with class \Drupal\Core\Field\Annotation\FieldWidget that implements * \Drupal\Core\Field\WidgetInterface (in most cases, by * subclassing \Drupal\Core\Field\WidgetBase). Widget plugins need to be in the * namespace \Drupal\{your_module}\Plugin\Field\FieldWidget. * * Widgets are @link form_api Form API @endlink elements with additional * processing capabilities. The methods of the WidgetInterface object are * typically called by respective methods in the * \Drupal\Core\Entity\Entity\EntityFormDisplay class. * * @see field * @see field_types * @see field_formatter * @see plugin_api */
Home | Imprint | This part of the site doesn't use cookies.