summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJian He2018-06-05 07:43:26 (GMT)
committerJian He2018-06-05 07:43:26 (GMT)
commitdfb8a0513a0a43b93e6a585d7475a30a105401bc (patch)
treed25339804a46551611b6946d78e0e9752b2140a8
parentbed501e4cb985e50e4ef15baa0349a0a21a9f19b (diff)
Add source field settingHEAD8.x-1.x
-rw-r--r--src/Plugin/Field/FieldType/PinyinShortcodeItem.php31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/Plugin/Field/FieldType/PinyinShortcodeItem.php b/src/Plugin/Field/FieldType/PinyinShortcodeItem.php
index 6b75ead..e354d4c 100644
--- a/src/Plugin/Field/FieldType/PinyinShortcodeItem.php
+++ b/src/Plugin/Field/FieldType/PinyinShortcodeItem.php
@@ -3,6 +3,7 @@
namespace Drupal\pinyin\Plugin\Field\FieldType;
use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
+use Drupal\Core\Form\FormStateInterface;
/**
* Defines the "pinyin_shortcode" entity field type.
@@ -20,6 +21,29 @@ class PinyinShortcodeItem extends StringItem {
/**
* {@inheritdoc}
*/
+ public static function defaultStorageSettings() {
+ return [
+ 'source_field' => NULL,
+ ] + parent::defaultStorageSettings();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
+ $element = parent::storageSettingsForm($form, $form_state, $has_data);
+
+ $element['source_field'] = [
+ '#type' => 'textfield',
+ '#title' => t('Source field'),
+ ];
+
+ return $element;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function applyDefaultValue($notify = TRUE) {
parent::applyDefaultValue($notify);
// Field preSave is only called if have values in field.
@@ -34,14 +58,17 @@ class PinyinShortcodeItem extends StringItem {
parent::preSave();
$entity = $this->getEntity();
+ if (!$source_field = $this->getSetting('source_field')) {
+ $source_field = $entity->getEntityType()->getKey('label');
+ }
if (!$entity->isNew()) {
- if ($entity->label() == $entity->original->label()) {
+ if ($entity->$source_field->value == $entity->original->$source_field->value) {
return;
}
}
$this->value = \Drupal::service('pinyin.shortcode')
- ->transliterate($entity->label(), 'en', '?', $this->getSetting('max_length'));
+ ->transliterate($entity->$source_field->value, 'en', '?', $this->getSetting('max_length'));
}
}