commit 075fede0344fa79e65cb59d9a1f5bcc201d85952 Author: Chintan Date: Thu Feb 18 06:17:52 2021 +0000 Initial commit diff --git a/cdesai_tff.info.yml b/cdesai_tff.info.yml new file mode 100644 index 0000000..9cf36ff --- /dev/null +++ b/cdesai_tff.info.yml @@ -0,0 +1,7 @@ +name: CDesai - Text Formatter Demo Challenge +type: module +description: 'Demo of custom text formatters based on Unleashed Technologies challenge.' +core_version_requirement: ^8.9 || ^9 +package: 'Demo' +dependencies: + - drupal:node diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a429dfc --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "drupal/cdesai_tff", + "description": "Demo of custom text formatters based on Unleashed Technologies challenge.", + "type": "drupal-module", + "homepage": "https://wezbyte.com/tff-demo", + "keywords": ["Drupal", "Chintan", "Desai","Text", "Formatter", "Demo"], + "minimum-stability": "dev", + "authors": [ + { + "name": "Chintan Desai", + "email": "chintan@wezbyte.com" + } + ], + "require": { + "drupal/core": "^8.0 || ^9.0", + "cocur/slugify": "^4.0" + } +} diff --git a/src/DesaiTextFormatterBase.php b/src/DesaiTextFormatterBase.php new file mode 100644 index 0000000..803188c --- /dev/null +++ b/src/DesaiTextFormatterBase.php @@ -0,0 +1,50 @@ + $item) { + $elements[$delta] = [ + '#type' => 'html_tag', + '#tag' => 'span', + '#attributes' => [ + 'class' => [$this->getCssClassId()], + ], + '#value' => $this->getValue($item), + ]; + } + + return $elements; + } + +} diff --git a/src/Plugin/Field/FieldFormatter/Rot13TextFormatter.php b/src/Plugin/Field/FieldFormatter/Rot13TextFormatter.php new file mode 100644 index 0000000..8d42ef1 --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/Rot13TextFormatter.php @@ -0,0 +1,102 @@ +convertToRot13($item); + } + + /** + * Converts the given item to Rot13 format. + * + * @param \Drupal\Core\Field\FieldItemInterface $item + * Field item object that needs to be converted. + * + * @return string + * The converted string. + */ + protected function convertToRot13(FieldItemInterface $item) { + $origString = $item->getString(); + $converted = ''; + + for ($i = 0; $i < strlen($origString); $i++ ) { + $decimalValue = ord($origString{$i}); + + if (self::isUpperCase($decimalValue)) { + $newValue = self::$DECIMAL_A_UPPER + ($decimalValue - self::$DECIMAL_A_UPPER + self::$ROT_CONSTANT) % self::$NUM_ALPHABET; + $converted .= chr($newValue); + } + elseif (self::isLowerCase($decimalValue)) { + $newValue = self::$DECIMAL_A_LOWER + ($decimalValue - self::$DECIMAL_A_LOWER + self::$ROT_CONSTANT) % self::$NUM_ALPHABET; + $converted .= chr($newValue); + } + else { + $converted .= $origString{$i}; + } + } + + return $converted; + } + + /** + * Checks whether the given decimal value is of an upper case letter. + * + * @param int $decimal_value + * Decimal value of a character. + * + * @return bool + * Returns TRUE if the condition matches; FALSE otherwise. + */ + private static function isUpperCase($decimal_value) { + return $decimal_value >= self::$DECIMAL_A_UPPER + && $decimal_value < (self::$DECIMAL_A_UPPER + self::$NUM_ALPHABET); + } + + /** + * Checks whether the given decimal value is of a lower case letter. + * + * @param int $decimal_value + * Decimal value of a character. + * + * @return bool + * Returns TRUE if the condition matches; FALSE otherwise. + */ + private static function isLowerCase($decimal_value) { + return $decimal_value >= self::$DECIMAL_A_LOWER + && $decimal_value < (self::$DECIMAL_A_LOWER + self::$NUM_ALPHABET); + } + +}