tripal
tripal copied to clipboard
Fix Placeholder setting in fields that's not set-able
Core Tripal 4 Development Task
Branch Name: tv4g1-issue1600-placeholderSetting
During a recent review I noticed the following line in the widget:
'#placeholder' => $this->getSetting('placeholder'),
This implies there is a placeholder setting for the widget but we don't actually have widget configuration to allow this to be set...
These are the fields where this is present:
- TripalIntegerTypeWidget
- TripalTextTypeWidget
- TripalBooleanTypeWidget
- TripalStringTypeWidget
- ChadoAnalysisWidgetDefault
- ChadoSequenceWidgetDefault
- ChadoOrganismWidgetDefault
Process
For each of the above widgets, think about whether a placeholder makes sense.
If it does, use the following information to add the widget settings form, default and summary to the widget class.
If it doesn't, then simply remove the following line from the widget class:
'#placeholder' => $this->getSetting('placeholder'),
Widget Settings
We add the following code to the Field Widget class in order to support settings.
Set the default value for the placeholder in the field. Apparently this should either be an example, or a brief description of what is expected. If you are unsure, feel free to leave the default empty.
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'placeholder' => '',
] + parent::defaultSettings();
}
We need to tell Drupal how to summarize the widget settings before implementing the form because without this, Drupal just silently ignores the form 🤷♀️
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$placeholder = $this
->getSetting('placeholder');
if (!empty($placeholder)) {
$summary[] = $this
->t('Placeholder: @placeholder', [
'@placeholder' => $placeholder,
]);
}
else {
$summary[] = $this
->t('No placeholder');
}
return $summary;
}
Add a form element to the widget settings form to allow admin to set the placeholder.
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['placeholder'] = [
'#type' => 'textfield',
'#title' => $this
->t('Placeholder'),
'#default_value' => $this
->getSetting('placeholder'),
'#description' => $this
->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
];
return $element;
}
This can be checked by going to Admin > Tripal > Page Structure. Find a content type that uses the field you are modifying and then go to "Manage Form Display". Once you make the above change and rebuild the cache, you should see a little gear show up beside the field widget you are changing. Click on that gear and it should open the form added above.
At this point, whatever you set in the above form should show up as a placeholder (i.e. in grey within the form element) of this field widget. If it does not show up, check that the $element
has '#placeholder' => $this->getSetting('placeholder'),
in it.
I think an example of what is needed to add the settings (if appropriate) can be found in
core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php