Skip to content

Commit

Permalink
Add button field (#1324)
Browse files Browse the repository at this point in the history
Adds support for the `type: button` field with the following options:

```yaml
button:
    type: button
    action: # button | link | popup (automatically determined by the values provider, if the value of the field is a URL or the href config is set (or the value of the field is a valid URL), then it'll be type link. If a handler is set, it'll be type popup. All other cases will be type: button)
    buttonLabel: # The label of the button itself.
    buttonType: # default | primary | success | info | warning | danger | link
    path: # Use a custom partial to render the button
    handler: # popup action only
    href: # link action only
    target: # link action only
    request: # button action only
    loading: # message to display while waiting for the request
    icon: 'icon-pencil'
```

Examples:

Simple button with AJAX request:
```yaml
_sync_ics:
    label: Actions
    buttonLabel: Sync
    type: button
    request: onSyncICS
    icon: 'icon-rotate'
    loading: Syncing...
```

Simple Link button:
```yaml
places_url:
    type: button
    buttonType: primary
    buttonLabel: View on Google Maps
    icon: icon-map-location-dot
    target: _blank
```

Button triggering popup:
```yaml
_btn_autofill:
    label: Autofill
    buttonLabel: Autofill Club Information
    type: button
    handler: onRenderAutofillPopup
    buttonType: primary
    icon: icon-map-location-dot
```

Documented by wintercms/docs@960255d
  • Loading branch information
LukeTowers authored Mar 5, 2025
1 parent 87557b4 commit 6f56935
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions modules/backend/widgets/form/partials/_field-button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* @var \Winter\Storm\Database\Model $formModel
* @var \Backend\Classes\FormField $field
*/
$action = 'button';
$handler = null;
$href = null;
$target = null;

if (!empty($field->config['href']) || filter_var($field->value, FILTER_VALIDATE_URL)) {
$action = 'link';
$href = $field->config['href'] ?? '';
$target = $field->config['target'] ?? null;
if ($formModel->hasAttribute($href)) {
$href = $formModel->getAttribute($href);
}
if (filter_var($field->value, FILTER_VALIDATE_URL)) {
$href = $field->value;
}
} elseif (!empty($field->config['handler'])) {
$action = 'popup';
$handler = $field->config['handler'];
}

$element = $action === 'link' ? 'a' : 'button';
$label = $field->config['buttonLabel'] ?? '';
$buttonType = $field->config['buttonType'] ?? 'default';
$classes = implode(' ', array_filter([
"btn btn-$buttonType",
$field->config['buttonCssClass'] ?? ''
]));
$request = $field->config['request'] ?? '';

$loadingText = $field->config['loading'] ?? '';
$icon = $field->config['icon'] ?? '';
?>
<div class="loading-indicator-container">
<?php if ($field->path): ?>
<?= $this->controller->makePartial($field->path, [
'formWidget' => $this,
'formModel' => $formModel,
'formField' => $field,
'formValue' => $field->value,
'model' => $formModel,
'field' => $field,
'value' => $field->value,
'action' => $action,
'element' => $element,
'label' => $label,
'buttonType' => $buttonType,
'classes' => $classes,
'handler' => $handler,
'request' => $request,
'href' => $href,
'target' => $target,
'loading' => $loadingText,
'icon' => $icon,
]) ?>
<?php else: ?>
<<?= e($element); ?>
class="<?= e($classes); ?>"
data-load-indicator<?= !empty($loadingText) ? '="' . e(trans($loadingText)) . '"' : ''; ?>
<?= $action === 'popup' ? 'data-control="popup"' : ''; ?>
<?= !empty($handler) ? 'data-handler="' . e($handler) . '"' : ''; ?>
<?= !empty($request) ? 'data-request="' . e($request) . '"' : ''; ?>
<?= !empty($href) ? 'href="' . e($href) . '"' : ''; ?>
<?= !empty($target) ? 'target="' . e($target) . '"' : ''; ?>
>
<?= !empty($icon) ? '<i class="' . e($icon) . '"></i>' : '' ?>
<?= e(trans($label)); ?>
</<?= e($element); ?>>
<?php endif; ?>
</div>

0 comments on commit 6f56935

Please sign in to comment.