summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJian He2017-04-12 18:08:35 (GMT)
committerJian He2017-04-12 18:08:35 (GMT)
commitafd9bc5dcca748efc32de2b38333b0524cdc239e (patch)
tree358ca834cab47a6be34b3f42b8a3ae04ad19f5d4
Initial commit.
-rw-r--r--pinyin.info.yml4
-rw-r--r--pinyin.module15
-rw-r--r--pinyin.services.yml4
-rw-r--r--src/PinyinShortcode.php40
-rw-r--r--src/Plugin/Field/FieldType/PinyinShortcodeItem.php47
5 files changed, 110 insertions, 0 deletions
diff --git a/pinyin.info.yml b/pinyin.info.yml
new file mode 100644
index 0000000..ca94566
--- /dev/null
+++ b/pinyin.info.yml
@@ -0,0 +1,4 @@
+name: Pinyin Short Code
+type: module
+description: 'Generate pinyin short code(拼音简码).'
+core: 8.x
diff --git a/pinyin.module b/pinyin.module
new file mode 100644
index 0000000..78417b5
--- /dev/null
+++ b/pinyin.module
@@ -0,0 +1,15 @@
+<?php
+
+/**
+ * Implements hook_field_formatter_info_alter().
+ */
+function pinyin_field_formatter_info_alter(array &$info) {
+ $info['string']['field_types'][] = 'pinyin_shortcode';
+}
+
+/**
+ * Implements hook_field_widget_info_alter().
+ */
+function pinyin_field_widget_info_alter(array &$info) {
+ $info['string_textfield']['field_types'][] = 'pinyin_shortcode';
+}
diff --git a/pinyin.services.yml b/pinyin.services.yml
new file mode 100644
index 0000000..3f98da3
--- /dev/null
+++ b/pinyin.services.yml
@@ -0,0 +1,4 @@
+services:
+ pinyin.shortcode:
+ class: Drupal\pinyin\PinyinShortcode
+ arguments: [null, '@module_handler']
diff --git a/src/PinyinShortcode.php b/src/PinyinShortcode.php
new file mode 100644
index 0000000..e9f4964
--- /dev/null
+++ b/src/PinyinShortcode.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Drupal\pinyin;
+
+use Drupal\Component\Transliteration\PhpTransliteration;
+
+/**
+ * Implements pinyin short code.
+ */
+class PinyinShortcode extends PhpTransliteration {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function transliterate($string, $langcode = 'en', $unknown_character = '?', $max_length = NULL) {
+ $result = '';
+ $length = 0;
+ // Split into Unicode characters and transliterate each one.
+ foreach (preg_split('//u', $string, 0, PREG_SPLIT_NO_EMPTY) as $character) {
+ $code = self::ordUTF8($character);
+ if ($code != -1) {
+ $to_add = $this->replace($code, $langcode, $unknown_character);
+
+ // Check if this exceeds the maximum allowed length.
+ if (isset($max_length)) {
+ $length += 1;
+ if ($length > $max_length) {
+ return $result;
+ }
+ }
+
+ $result .= $to_add[0];
+ }
+
+ }
+
+ return $result;
+ }
+
+}
diff --git a/src/Plugin/Field/FieldType/PinyinShortcodeItem.php b/src/Plugin/Field/FieldType/PinyinShortcodeItem.php
new file mode 100644
index 0000000..6b75ead
--- /dev/null
+++ b/src/Plugin/Field/FieldType/PinyinShortcodeItem.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Drupal\pinyin\Plugin\Field\FieldType;
+
+use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
+
+/**
+ * Defines the "pinyin_shortcode" entity field type.
+ *
+ * @FieldType(
+ * id = "pinyin_shortcode",
+ * label = @Translation("Pinyin short code"),
+ * description = @Translation("A field containing pinyin short code value."),
+ * default_widget = "string_textfield",
+ * default_formatter = "string"
+ * )
+ */
+class PinyinShortcodeItem extends StringItem {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function applyDefaultValue($notify = TRUE) {
+ parent::applyDefaultValue($notify);
+ // Field preSave is only called if have values in field.
+ $this->setValue(['value' => 'pinyin'], $notify);
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function preSave() {
+ parent::preSave();
+
+ $entity = $this->getEntity();
+ if (!$entity->isNew()) {
+ if ($entity->label() == $entity->original->label()) {
+ return;
+ }
+ }
+
+ $this->value = \Drupal::service('pinyin.shortcode')
+ ->transliterate($entity->label(), 'en', '?', $this->getSetting('max_length'));
+ }
+
+}