Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
commerce
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
Merge requests
!42
#3216713
:Add formatter and static caching
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
#3216713
:Add formatter and static caching
issue/commerce-3216713:3216713-add_formatter_and_caching
into
8.x-2.x
Overview
8
Commits
4
Pipelines
0
Changes
3
4 unresolved threads
Hide all comments
Open
Vitaliy Marchuk
requested to merge
issue/commerce-3216713:3216713-add_formatter_and_caching
into
8.x-2.x
4 years ago
Overview
8
Commits
4
Pipelines
0
Changes
3
4 unresolved threads
Hide all comments
Expand
Closes
#3216713
0
0
Merge request reports
Compare
8.x-2.x
version 3
af1fa6dc
4 years ago
version 2
eba27cc7
4 years ago
version 1
6a404c9b
4 years ago
8.x-2.x (base)
and
latest version
latest version
4173a7ac
4 commits,
4 years ago
version 3
af1fa6dc
3 commits,
4 years ago
version 2
eba27cc7
2 commits,
4 years ago
version 1
6a404c9b
1 commit,
4 years ago
3 files
+
177
−
22
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
Conflict: This file was added both in the source and target branches, but with different contents. Ask someone with write access to resolve it.
modules/promotion/src/Plugin/Field/FieldFormatter/UsageLimitFormatter.php
0 → 100644
+
82
−
0
Options
<?php
namespace
Drupal\commerce_promotion\Plugin\Field\FieldFormatter
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\Field\FormatterBase
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Plugin implementation of the 'commerce_usage_limit' formatter.
*
* @FieldFormatter(
* id = "commerce_usage_limit",
* label = @Translation("Usage limit"),
* field_types = {
* "integer",
* },
* )
*/
class
UsageLimitFormatter
extends
FormatterBase
{
/**
* The usage.
*
* @var \Drupal\commerce_promotion\PromotionUsageInterface
*/
protected
$usage
;
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
$instance
=
parent
::
create
(
$container
,
$configuration
,
$plugin_id
,
$plugin_definition
);
$instance
->
usage
=
$container
->
get
(
'commerce_promotion.usage'
);
return
$instance
;
}
/**
* {@inheritdoc}
*/
public
function
viewElements
(
FieldItemListInterface
$items
,
$langcode
)
{
$elements
=
[];
$field_name
=
$items
->
getName
();
$entity
=
$items
->
getEntity
();
// Get usage of promotions/coupons from the static cache
// populated in PromotionUsage service.
$current_usage
=
$entity
->
getEntityTypeId
()
==
'commerce_promotion'
?
$this
->
usage
->
load
(
$entity
)
:
$this
->
usage
->
loadByCoupon
(
$entity
);
// For the "usage_limit" output actual usage / limit.
if
(
$field_name
==
'usage_limit'
)
{
$usage_limit
=
$entity
->
getUsageLimit
();
$usage_limit
=
$usage_limit
?:
$this
->
t
(
'Unlimited'
);
$usage
=
$current_usage
.
' / '
.
$usage_limit
;
}
else
{
// Output the actual limit (if not empty), otherwise output "Unlimited".
$usage
=
$current_usage
?:
$this
->
t
(
'Unlimited'
);
}
$elements
[
0
]
=
[
'#markup'
=>
$usage
,
];
return
$elements
;
}
/**
* {@inheritdoc}
*/
public
static
function
isApplicable
(
FieldDefinitionInterface
$field_definition
)
{
$entity_type
=
$field_definition
->
getTargetEntityTypeId
();
$field_name
=
$field_definition
->
getName
();
$applicable_entity_type
=
in_array
(
$entity_type
,
[
'commerce_promotion'
,
'commerce_promotion_coupon'
]);
$applicable_field_name
=
in_array
(
$field_name
,
[
'usage_limit'
,
'usage_limit_customer'
]);
return
$applicable_entity_type
&&
$applicable_field_name
;
}
}
Loading