diff --git a/src/Plugin/Field/FieldType/PinyinShortcodeItem.php b/src/Plugin/Field/FieldType/PinyinShortcodeItem.php index 6b75ead07e54955ddbbc6d23ae6ae23007673fcb..e354d4c806b9cad4ba5b13bb10689b156c3f7c54 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. @@ -17,6 +18,29 @@ use Drupal\Core\Field\Plugin\Field\FieldType\StringItem; */ 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} */ @@ -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')); } }