Skip to content

Commit

Permalink
Kirby 4 compatibility (2.0.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainjule committed May 5, 2024
1 parent 6d78025 commit 49e1416
Show file tree
Hide file tree
Showing 22 changed files with 461 additions and 442 deletions.
Binary file added .DS_Store
Binary file not shown.
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add illustrations to radio buttons.

## Overview

> This plugin is completely free and published under the MIT license. However, if you are using it in a commercial project and want to help me keep up with maintenance, please consider [making a donation of your choice](https://www.paypal.me/sylvainjl) or purchasing your license(s) through [my affiliate link](https://a.paddle.com/v2/click/1129/36369?link=1170).
> This plugin is completely free and published under the MIT license. However, if you are using it in a commercial project and want to help me keep up with maintenance, please consider [making a donation of your choice](https://www.paypal.me/sylvainjl).
- [1. Installation](#1-installation)
- [2. Setup](#2-setup)
Expand All @@ -23,6 +23,7 @@ Add illustrations to radio buttons.
## 1. Installation

> If you are looking to use this field with Kirby 2, please switch to the `kirby-2` branch.
> Kirby 3: Up to 1.0.5. Kirby 4: 2.0.0+
Download and copy this repository to ```/site/plugins/imageradio```

Expand Down Expand Up @@ -143,18 +144,6 @@ myimageradio:
mobile: false
```

#### 4.5. `gap`

![gap](https://user-images.githubusercontent.com/14079751/48334057-a769b280-e659-11e8-95f8-175cbee67088.jpg)

Whether the field should have a `1rem` gap between each input, K2-like. Default is `false`.

```yaml
myimageradio:
type: imageradio
gap: false
```

<br/>

## 5. License
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Add illustrations to Kirby's radio buttons",
"type": "kirby-plugin",
"license": "MIT",
"version": "1.0.5",
"version": "2.0.0",
"authors": [
{
"name": "Sylvain Julé",
Expand Down
2 changes: 1 addition & 1 deletion index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added lib/.DS_Store
Binary file not shown.
17 changes: 11 additions & 6 deletions lib/fields/imageradio.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php

namespace SylvainJule;

require_once dirname(__DIR__) . '/options/imageradio-optionsapi.php';
require_once dirname(__DIR__) . '/options/imageradio-optionsquery.php';
require_once dirname(__DIR__) . '/options/imageradio-factory.php';
require_once dirname(__DIR__) . '/options/imageradio-option.php';
require_once dirname(__DIR__) . '/options/imageradio-options.php';
require_once dirname(__DIR__) . '/options/imageradio.php';


$base = require kirby()->root('kirby') . '/config/fields/radio.php';
Expand All @@ -22,9 +27,6 @@
'back' => function($back = false) {
return $back;
},
'gap' => function($gap = false) {
return $gap;
},
'mobile' => function($mobile = false) {
return $mobile;
},
Expand All @@ -39,9 +41,12 @@
--------------------------------*/

$base = array_replace_recursive($base, array(
'computed' => array(
'options' => function() {
return ImageRadioOptions::factory($this->options(), $this->props, $this->model());
'methods' => array(
'getOptions' => function() {
$props = \Kirby\Field\FieldOptions::polyfill($this->props);
$options = ImageRadio::factory($props['options']);

return $options->render($this->model());
},
),
));
Expand Down
25 changes: 25 additions & 0 deletions lib/options/imageradio-factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace SylvainJule;

class ImageRadioFactory extends \Kirby\Blueprint\Factory {
public static function apply(array $properties, array $factories): array
{

foreach ($factories as $property => $class) {
// skip non-existing properties, empty properties
// or properties that are matching objects
if (
isset($properties[$property]) === false ||
$properties[$property] === null ||
is_a($properties[$property], $class) === true
) {
continue;
}

$properties[$property] = $class::factory($properties[$property]);
}

return $properties;
}
}
46 changes: 46 additions & 0 deletions lib/options/imageradio-option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace SylvainJule;

use Kirby\Blueprint\Factory;
use Kirby\Blueprint\NodeIcon;
use Kirby\Blueprint\NodeText;
use Kirby\Blueprint\NodeString;
use Kirby\Cms\ModelWithContent;


class ImageRadioOption extends \Kirby\Option\Option {
public function __construct(
public string|int|float|null $value,
public bool $disabled = false,
public NodeIcon|null $icon = null,
public NodeText|null $info = null,
public NodeText|null $text = null,
public NodeString|null $image = null,
) {
$this->text ??= new NodeText(['en' => $this->value]);
}
public static function factory(string|int|float|null|array $props): static
{

$props = ImageRadioFactory::apply($props, [
'icon' => NodeIcon::class,
'info' => NodeText::class,
'text' => NodeText::class,
'image' => NodeString::class,
]);

return new static(...$props);
}
public function render(ModelWithContent $model): array
{
return [
'disabled' => $this->disabled,
'icon' => $this->icon?->render($model),
'info' => $this->info?->render($model),
'text' => $this->text?->render($model),
'image' => $this->image?->render($model),
'value' => $this->value
];
}
}
Loading

0 comments on commit 49e1416

Please sign in to comment.