Commit dfb8a051 authored by jian he's avatar jian he

Add source field setting

parent bed501e4
......@@ -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'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment