Commit dcf07493 authored by Balazs Janos Tatar's avatar Balazs Janos Tatar Committed by Dorstenia Development
Browse files

Issue #2834414 by tatarbj: Format Special Words

parent 537876c2
Loading
Loading
Loading
Loading
+34 −2
Changes for easy_breadcrumb.module: 34 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -370,7 +370,6 @@ function _easy_breadcrumb_normalize_text($raw_text) {

  // Gets the flag saying the capitalizator mode.
  $capitalizator_mode = variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE, 'ucwords');

  if ($capitalizator_mode === 'ucwords') {
    // Transforms the text 'once a time' to 'Once a Time'.
    // List of words to be ignored by the capitalizator.
@@ -385,7 +384,40 @@ function _easy_breadcrumb_normalize_text($raw_text) {
      }
    }
    $normalized_text = join(' ', $words_arr);
  } else {
  }
  elseif ($capitalizator_mode === 'ucall') {
    // Transforms the text 'once a time' to 'ONCE A TIME'.
    $normalized_text = drupal_strtoupper($normalized_text);
  }
  elseif ($capitalizator_mode === 'ucforce') {
    // Transforms the text 'once a time' to 'once a TIME'.
    // List of words to be forced by the capitalizator.
    $forced_words = variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS, '');
    // If case sensitivity is false make all the forced words uncapitalized by default.
    if (!variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_CASE_SENSITIVITY, FALSE)) {
      $forced_words = array_map('drupal_strtolower', $forced_words);
    }
    $words_arr = explode(' ', $normalized_text);
    // Transforms the non-ignored words of the segment.
    if (variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_FIRST_LETTER, '')) {
      $words_arr[0] = drupal_ucfirst($words_arr[0]);
    }
    for ($idx_1 = 0, $words_quantity = count($words_arr); $idx_1 < $words_quantity; ++$idx_1) {
    // If case sensitivity is false make the compared word uncapitalized in order to allow the comparison well.
      if (!variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_CASE_SENSITIVITY, FALSE)) {
        $selected_word = drupal_strtolower($words_arr[$idx_1]);
      }
      else {
        $selected_word = $words_arr[$idx_1];
      }
      // Transforms this word only if it is in the list of forced words.
      if (in_array($selected_word, $forced_words)) {
        $words_arr[$idx_1] = drupal_strtoupper($selected_word);
      }
    }
    $normalized_text = join(' ', $words_arr);
  }
  else {
    // Transforms the text 'once a time' to 'Once a time' (ucfirst).
    $normalized_text = drupal_ucfirst($normalized_text);
  }
+15 −0
Changes for includes/EasyBreadcrumbConstants.inc: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -70,6 +70,21 @@ class EasyBreadcrumbConstants {
   */
  const DB_VAR_CAPITALIZATOR_IGNORED_WORDS = 'easy_breadcrumb-capitalizator_ignored_words';

  /**
   * List of words to be forced by the 'capitalizator'. E.g.: your brand's name.
   */
  const DB_VAR_CAPITALIZATOR_FORCED_WORDS = 'easy_breadcrumb-capitalizator_forced_words';

  /**
   * List of words to be forced by the 'capitalizator'. E.g.: your brand's name.
   */
  const DB_VAR_CAPITALIZATOR_FORCED_WORDS_FIRST_LETTER = 'easy_breadcrumb-capitalizator_forced_words_first_letter';

  /**
   * Logical value to 'Make the first letters of each segment capitalized'.
   */
  const DB_VAR_CAPITALIZATOR_FORCED_WORDS_CASE_SENSITIVITY = 'easy_breadcrumb-capitalizator_forced_words_case_sensitivity';

  /**
   * Default list of excluded paths.
   *
+70 −3
Changes for includes/easy_breadcrumb.admin.inc: 70 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -99,6 +99,8 @@ function _easy_breadcrumb_general_settings_form() {
      'none' => t('None'),
      'ucwords' => t("Capitalize the first letter of each word in the segment"),
      'ucfirst' => t("Only capitalize the first letter of each segment"),
      'ucall' => t("Capitalize all the letters of each word in the segment"),
      'ucforce' => t("Capitalize only the words that are set below"),
    ),
    '#description' => t("Choose the transformation mode you want to apply to the segments' titles. E.g.: 'blog/once-a-time' -> 'Home >> Blog >> Once a Time'."),
    '#default_value' => variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE, 'ucwords'),
@@ -114,8 +116,66 @@ function _easy_breadcrumb_general_settings_form() {
    '#type' => 'textarea',
    '#rows' => 3,
    '#title' => t("Words to be ignored by the 'capitalizator'"),
    '#description' => t("Enter a space separated list of words to be ignored by the 'capitalizator'. This will be applyed only to the words not at the beginning of each segment. E.g.: of and."),
    '#description' => t("Enter a space separated list of words to be ignored by the 'capitalizator'. This will be applied only to the words not at the beginning of each segment. E.g.: of and."),
    '#default_value' => $capitalizator_ignored_words,
    '#states' => array(
      'visible' => array(
        ':input[name="' . EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE . '"]' => array('value' => 'ucwords'),
      ),
      'invisible' => array(
        ':input[name="' . EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE . '"]' => array('!value' => 'ucwords'),
      )
    )
  );

  // Formats the forced-words array as space separated list of words
  // (word1 word2 wordN) before displaying them.
  $capitalizator_forced_words_arr = variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS, '');
  $capitalizator_forced_words = @join(' ', $capitalizator_forced_words_arr);

  $fieldset_general[EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS] = array(
    '#type' => 'textarea',
    '#rows' => 3,
    '#title' => t("Words to be forced to capitalized by the 'capitalizator'"),
    '#description' => t("Enter a space separated list of words to be forced by the 'capitalizator'. This will be applied only to the words that are listed. E.g.: if you want to capitalize your brand's name."),
    '#default_value' => $capitalizator_forced_words,
    '#states' => array(
      'visible' => array(
        ':input[name="' . EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE . '"]' => array('value' => 'ucforce'),
      ),
      'invisible' => array(
        ':input[name="' . EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE . '"]' => array('!value' => 'ucforce'),
      )
    )
  );

  $fieldset_general[EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_CASE_SENSITIVITY] = array(
    '#type' => 'checkbox',
    '#title' => t("Use case sensitivity when matching words to be forced to capitalization."),
    '#default_value' => variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_CASE_SENSITIVITY, FALSE),
    '#states' => array(
      'visible' => array(
        ':input[name="' . EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE . '"]' => array('value' => 'ucforce'),
      ),
      'invisible' => array(
        ':input[name="' . EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE . '"]' => array('!value' => 'ucforce'),
      )
    ),
    '#description' => t("If checked, it matches drupal with drupal, druPAL with druPAL. Unchecked, it matches drupal with Drupal, drupal with druPAL.")
  );

  $fieldset_general[EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_FIRST_LETTER] = array(
    '#type' => 'checkbox',
    '#title' => t("Make the first letters of each segment capitalized."),
    '#default_value' => variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_FIRST_LETTER, TRUE),
    '#states' => array(
      'visible' => array(
        ':input[name="' . EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE . '"]' => array('value' => 'ucforce'),
      ),
      'invisible' => array(
        ':input[name="' . EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE . '"]' => array('!value' => 'ucforce'),
      )
    )
  );

  $form = array();
@@ -172,11 +232,18 @@ function _easy_breadcrumb_admin_submit($form, &$form_state) {
    $excluded_paths_arr[$path] = $path;
  }

  $forced_words_arr = array();
  $forced_words = $form_state['values'][EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS];
  $forced_words = preg_replace('/\r*\n+/', ' ', $forced_words);
  $forced_words = trim($forced_words);
  $forced_words_arr_aux = $forced_words === '' ? array() : preg_split('/\s+/', $forced_words);
  foreach ($forced_words_arr_aux as $word) {
    $forced_words_arr[$word] = $word;
  }
  // Updates the $form_state before passing it to the Drupal system.
  $form_state['values'][EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_IGNORED_WORDS] = $ignored_words_arr;

  // Updates the $form_state before passing it to the Drupal system.
  $form_state['values'][EasyBreadcrumbConstants::DB_VAR_EXCLUDED_PATHS] = $excluded_paths_arr;
  $form_state['values'][EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS] = $forced_words_arr;

  // Delegates persistence to the existing Drupal system.
  system_settings_form_submit($form, $form_state);