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 Oct 21, 2020
1 parent 5abdbf9 commit c1072bd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
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;
4 changes: 4 additions & 0 deletions assets/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import 'sulu-snippet-bundle';
import 'sulu-website-bundle';

// Implement custom extensions here
import {fieldRegistry} from 'sulu-admin-bundle/containers';
import Currency from "./fields/Currency";

fieldRegistry.add('currency', Currency);

// Start admin application
startAdmin();
Expand Down
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,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 @@ -60,6 +60,17 @@
</meta>
</property>

<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>

<!-- @see https://docs.sulu.io/en/2.1/book/templates.html#including-other-templates -->
<xi:include href="../includes/blocks.xml"
xpointer="xmlns(sulu=http://schemas.sulu.io/template/template)xpointer(/sulu:properties/sulu:block)"/>
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 c1072bd

Please sign in to comment.