Initial commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Drupal\cdesai_tff;
|
||||
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
|
||||
abstract class DesaiTextFormatterBase extends FormatterBase {
|
||||
|
||||
/**
|
||||
* Generates a string to be used a CSS class.
|
||||
*
|
||||
* @return string
|
||||
* The formatted string.
|
||||
*/
|
||||
abstract function getCssClassId();
|
||||
|
||||
/**
|
||||
* Formats the given item.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldItemInterface $item
|
||||
* Field item object that needs to be converted.
|
||||
*
|
||||
* @return string
|
||||
* The formatted string.
|
||||
*/
|
||||
abstract function getValue(FieldItemInterface $item);
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = [];
|
||||
foreach ($items as $delta => $item) {
|
||||
$elements[$delta] = [
|
||||
'#type' => 'html_tag',
|
||||
'#tag' => 'span',
|
||||
'#attributes' => [
|
||||
'class' => [$this->getCssClassId()],
|
||||
],
|
||||
'#value' => $this->getValue($item),
|
||||
];
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Drupal\cdesai_tff\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\cdesai_tff\DesaiTextFormatterBase;
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
|
||||
/**
|
||||
* Implementation of custom Rot13 text formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "cdesai_tff_rot13",
|
||||
* label = @Translation("Custom Rot13 Formatter"),
|
||||
* field_types = {
|
||||
* "string",
|
||||
* "text"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class Rot13TextFormatter extends DesaiTextFormatterBase {
|
||||
|
||||
private static $DECIMAL_A_UPPER = 65;
|
||||
private static $DECIMAL_A_LOWER = 97;
|
||||
private static $NUM_ALPHABET = 26;
|
||||
private static $ROT_CONSTANT = 13;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function getCssClassId() {
|
||||
return 'cdesai-tff-rot13';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function getValue(FieldItemInterface $item) {
|
||||
return $this->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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user