You are here

function _termcase_convert_string_to_case in Termcase 8

Same name and namespace in other branches
  1. 6 termcase.module \_termcase_convert_string_to_case()
  2. 7 termcase.module \_termcase_convert_string_to_case()

Helper function to convert the current string to the specified case.

Parameters

string $original_string: The original term to be converted.

string $case: Case of the vocabulary, the term belongs to.

Return value

string The term converted in proper case.

2 calls to _termcase_convert_string_to_case()
termcase_taxonomy_term_presave in ./termcase.module
Implements hook_ENTITY_TYPE_presave() for taxonomy_term entities.
UpdateTerms::updateAllTerms in src/UpdateTerms.php
Batch process callback.

File

./termcase.module, line 175
The Termcase module gives you the option to specify specific case-formatting on terms.

Code

function _termcase_convert_string_to_case($original_string, $case = TERMCASE_NONE) {
  switch ($case) {
    case TERMCASE_UCFIRST:
      $converted_string = Unicode::ucfirst($original_string);
      break;
    case TERMCASE_LOWERCASE:
      $converted_string = mb_strtolower($original_string);
      break;
    case TERMCASE_UPPERCASE:
      $converted_string = mb_strtoupper($original_string);
      break;
    case TERMCASE_PROPERCASE:
      $words = explode(' ', $original_string);
      foreach ($words as $key => $word) {
        $words[$key] = Unicode::ucfirst($word);
      }
      $converted_string = implode(' ', $words);
      break;
    default:
      $converted_string = $original_string;
      break;
  }

  // Allow modules to change the string before saving it.
  \Drupal::moduleHandler()
    ->alter('termcase_convert_string', $converted_string, $original_string, $case);
  return $converted_string;
}