Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
commerce_exchanger
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
commerce_exchanger
Commits
f656b7fd
Commit
f656b7fd
authored
1 year ago
by
Valentino Međimorec
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3381018
: Add price field formatter to display converted price
parent
f547b914
No related branches found
No related tags found
1 merge request
!2
Issue #3381018: Add price field formatter to display converted price
Pipeline
#28331
passed
1 year ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
config/schema/commerce_exchanger.schema.yml
+14
-0
14 additions, 0 deletions
config/schema/commerce_exchanger.schema.yml
src/Plugin/Field/FieldFormatter/PriceExchangerFormatter.php
+150
-0
150 additions, 0 deletions
src/Plugin/Field/FieldFormatter/PriceExchangerFormatter.php
with
164 additions
and
0 deletions
config/schema/commerce_exchanger.schema.yml
+
14
−
0
View file @
f656b7fd
...
...
@@ -82,3 +82,17 @@ commerce_exchanger.latest_exchange_rates.*:
sync
:
type
:
integer
label
:
'
Sync'
field.formatter.settings.commerce_price_exchanger
:
type
:
mapping
label
:
'
Exchanger
price
formatter
settings'
mapping
:
strip_trailing_zeroes
:
type
:
boolean
label
:
'
Strip
trailing
zeroes
after
the
decimal
point'
currency_display
:
type
:
string
label
:
'
Currency
display'
target_currency
:
type
:
string
label
:
'
Target
currency'
This diff is collapsed.
Click to expand it.
src/Plugin/Field/FieldFormatter/PriceExchangerFormatter.php
0 → 100644
+
150
−
0
View file @
f656b7fd
<?php
namespace
Drupal\commerce_exchanger\Plugin\Field\FieldFormatter
;
use
CommerceGuys\Intl\Formatter\CurrencyFormatterInterface
;
use
Drupal\commerce_exchanger
\ExchangerCalculatorInterface
;
use
Drupal\commerce_price
\Plugin\Field\FieldFormatter\PriceDefaultFormatter
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Language\LanguageInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Plugin implementation of the 'commerce_price_exchanger' formatter.
*
* @FieldFormatter(
* id = "commerce_price_exchanger",
* label = @Translation("Currency converter price"),
* field_types = {
* "commerce_price"
* }
* )
*/
class
PriceExchangerFormatter
extends
PriceDefaultFormatter
{
/**
* The price exchanger.
*
* @var \Drupal\commerce_exchanger\ExchangerCalculatorInterface
*/
protected
$priceExchanger
;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected
$entityTypeManager
;
/**
* Constructs a new PriceConvertedFormatter object.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface $currency_formatter
* The currency formatter.
* @param \Drupal\commerce_exchanger\ExchangerCalculatorInterface $price_exchanger
* The price exchanger.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public
function
__construct
(
$plugin_id
,
$plugin_definition
,
FieldDefinitionInterface
$field_definition
,
array
$settings
,
$label
,
$view_mode
,
array
$third_party_settings
,
CurrencyFormatterInterface
$currency_formatter
,
ExchangerCalculatorInterface
$price_exchanger
,
EntityTypeManagerInterface
$entity_type_manager
)
{
parent
::
__construct
(
$plugin_id
,
$plugin_definition
,
$field_definition
,
$settings
,
$label
,
$view_mode
,
$third_party_settings
,
$currency_formatter
);
$this
->
priceExchanger
=
$price_exchanger
;
$this
->
entityTypeManager
=
$entity_type_manager
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$plugin_id
,
$plugin_definition
,
$configuration
[
'field_definition'
],
$configuration
[
'settings'
],
$configuration
[
'label'
],
$configuration
[
'view_mode'
],
$configuration
[
'third_party_settings'
],
$container
->
get
(
'commerce_price.currency_formatter'
),
$container
->
get
(
'commerce_exchanger.calculate'
),
$container
->
get
(
'entity_type.manager'
));
}
/**
* {@inheritdoc}
*/
public
static
function
defaultSettings
()
{
return
[
'target_currency'
=>
'USD'
]
+
parent
::
defaultSettings
();
}
/**
* {@inheritdoc}
*/
public
function
settingsForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$elements
=
parent
::
settingsForm
(
$form
,
$form_state
);
$currency_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_currency'
);
// Get all active currencies.
/** @var \Drupal\commerce_price\Entity\CurrencyInterface[] $active_currencies */
$active_currencies
=
$currency_storage
->
loadByProperties
([
'status'
=>
1
]);
$options
=
[];
foreach
(
$active_currencies
as
$active_currency
)
{
$options
[
$active_currency
->
getCurrencyCode
()]
=
$active_currency
->
getName
();
}
$elements
[
'target_currency'
]
=
[
'#type'
=>
'radios'
,
'#title'
=>
$this
->
t
(
'Target currency'
),
'#options'
=>
$options
,
'#default_value'
=>
$this
->
getSetting
(
'target_currency'
),
];
return
$elements
;
}
/**
* {@inheritdoc}
*/
public
function
settingsSummary
()
{
$summary
=
parent
::
settingsSummary
();
$target_currency
=
$this
->
getSetting
(
'target_currency'
);
$summary
[]
=
$this
->
t
(
'Target currency: @target_currency.'
,
[
'@target_currency'
=>
$target_currency
]);
return
$summary
;
}
/**
* {@inheritdoc}
*/
public
function
viewElements
(
FieldItemListInterface
$items
,
$langcode
)
{
$options
=
$this
->
getFormattingOptions
();
$elements
=
[];
foreach
(
$items
as
$delta
=>
$item
)
{
$price
=
$item
->
toPrice
();
$converted_price
=
$this
->
priceExchanger
->
priceConversion
(
$price
,
$this
->
getSetting
(
'target_currency'
));
$elements
[
$delta
]
=
[
'#markup'
=>
$this
->
currencyFormatter
->
format
(
$converted_price
->
getNumber
(),
$converted_price
->
getCurrencyCode
(),
$options
),
'#cache'
=>
[
'contexts'
=>
[
'languages:'
.
LanguageInterface
::
TYPE_INTERFACE
,
'country'
,
],
],
];
}
return
$elements
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment