cms-niceties/src/Extensions/PlaceholderFormExtension.php

58 lines
1.7 KiB
PHP
Raw Normal View History

2021-06-19 21:30:03 +02:00
<?php
namespace A2nt\CMSNiceties\Extensions;
2022-06-27 16:00:40 +02:00
use SilverStripe\Core\Config\Config;
2021-06-19 21:30:03 +02:00
use SilverStripe\Core\Extension;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
2023-10-24 21:53:40 +02:00
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\FormField;
2021-06-19 21:30:03 +02:00
2022-05-10 13:09:06 +02:00
/**
* Class \A2nt\CMSNiceties\Extensions\PlaceholderFormExtension
*
* @property \A2nt\CMSNiceties\Extensions\PlaceholderFormExtension $owner
*/
2021-06-19 21:30:03 +02:00
class PlaceholderFormExtension extends Extension
{
public function updateFormFields(FieldList $fields)
{
foreach ($fields as $field) {
$this->setPlaceholder($field);
}
}
private function setPlaceholder($field)
{
2023-10-24 21:53:40 +02:00
if (is_a($field, TextField::class) || is_a($field, TextareaField::class)) {
2021-07-09 15:04:46 +02:00
if (!$field->getAttribute('placeholder')) {
$placeholder = $field->Title() .($field->hasClass('requiredField') ? '*' : '');
2022-06-28 11:04:34 +02:00
if (!Config::inst()->get(\get_class($this->owner), 'no_placeholders')) {
$field->setAttribute(
'placeholder',
$placeholder
);
}
2022-06-27 16:00:40 +02:00
/*
* SilverStripe\UserForms\Form\UserForm:
* show_labels: false
*/
if (!Config::inst()->get(\get_class($this->owner), 'show_labels')) {
$field->setTitle('');
}
2021-07-09 15:04:46 +02:00
}
2021-06-19 21:30:03 +02:00
}
if (is_a($field, CompositeField::class)) {
$children = $field->getChildren();
foreach ($children as $child) {
$this->setPlaceholder($child);
}
}
}
}