Skip to content

Commit

Permalink
Implement currency content type
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnatter committed Aug 24, 2021
1 parent 7fdb4b6 commit 99921a6
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions assets/admin/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
// Add project specific javascript code here:
import {fieldRegistry} from 'sulu-admin-bundle/containers';
import Currency from "./fields/Currency";

fieldRegistry.add('currency', Currency);
43 changes: 43 additions & 0 deletions assets/admin/fields/Currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import {Number} from 'sulu-admin-bundle/components';

class Currency extends React.Component{
handleInputChange = (floatAmount) => {
const centAmount = floatAmount !== undefined ?
Math.floor(floatAmount * 100)
: undefined;

const allowNegativeAmount = this.props.schemaOptions.allowNegativeAmount !== undefined
? this.props.schemaOptions.allowNegativeAmount.value
: false;

const normalizedCentAmount = !allowNegativeAmount && centAmount !== undefined
? Math.max(0, centAmount)
: centAmount;

this.props.onChange(normalizedCentAmount);
this.props.onFinish();
};

render() {
const {dataPath, disabled, error, value: centAmount} = this.props;

const floatAmount = centAmount !== undefined
? centAmount / 100
: undefined;

return (
<Number
disabled={!!disabled}
icon="su-dollar"
id={dataPath}
onChange={this.handleInputChange}
step={0.01}
valid={!error}
value={floatAmount}
/>
);
}
}

export default Currency;
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ services:

App\Content\Type\AlbumSelection:
tags: [{name: 'sulu.content.type', alias: 'album_selection'}]

App\Content\Type\Currency:
tags: [ { name: 'sulu.content.type', alias: 'currency' } ]
11 changes: 11 additions & 0 deletions config/templates/pages/homepage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@
</properties>
</section>

<property name="price" type="currency">
<meta>
<title lang="en">Price</title>
<title lang="de">Preis</title>
</meta>

<params>
<param name="allowNegativeAmount" value="true"/>
</params>
</property>

<section name="content">
<meta>
<title lang="en">Content</title>
Expand Down
15 changes: 15 additions & 0 deletions src/Content/Type/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Content\Type;

use Sulu\Component\Content\SimpleContentType;

class Currency extends SimpleContentType
{
public function __construct()
{
parent::__construct('currency');
}
}
2 changes: 2 additions & 0 deletions templates/pages/homepage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
{% endblock %}

{% block contentBody %}
Price Content Type Value: {{ content.price|default('not set') }}

{% include 'includes/blocks.html.twig' %}
{% endblock %}

0 comments on commit 99921a6

Please sign in to comment.