Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Example] Create, register and use custom field-type and content-type #79

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 and import of additional bundles 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:
Copy link
Contributor Author

@niklasnatter niklasnatter Feb 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a field-type is used inside of a page template, Sulu looks for a content-type that matches the name of the field-type. A content-type is registered as Symfony service and is responsible for loading and storing the data of the field to the page.

If a field-type is not used inside of page templates, it is not necessary to implement a matching content-type.

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