Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
commerce-3474801
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
Issue forks
commerce-3474801
Commits
0b2a4db4
Commit
0b2a4db4
authored
10 months ago
by
Jonathan Sacksick
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3180541
followup: Commit the missing event subscriber.
parent
7a6a73cb
Branches
8.x-3.x
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
modules/product/src/EventSubscriber/VariationFieldComponentSubscriber.php
+73
-0
73 additions, 0 deletions
...src/EventSubscriber/VariationFieldComponentSubscriber.php
with
73 additions
and
0 deletions
modules/product/src/EventSubscriber/VariationFieldComponentSubscriber.php
0 → 100644
+
73
−
0
View file @
0b2a4db4
<?php
namespace
Drupal\commerce_product\EventSubscriber
;
use
Drupal\commerce_product
\Entity\ProductInterface
;
use
Drupal\commerce_product
\Entity\ProductVariationInterface
;
use
Drupal\commerce_product
\Plugin\Block\VariationFieldBlock
;
use
Drupal\Core\Plugin\Context\Context
;
use
Drupal\Core\Plugin\Context\EntityContextDefinition
;
use
Drupal\Core\StringTranslation\StringTranslationTrait
;
use
Drupal\layout_builder
\Event\SectionComponentBuildRenderArrayEvent
;
use
Drupal\layout_builder
\LayoutBuilderEvents
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
/**
* Add variation context for block build based on product context.
*
* @internal
* Tagged services are internal.
*/
class
VariationFieldComponentSubscriber
implements
EventSubscriberInterface
{
use
StringTranslationTrait
;
/**
* {@inheritdoc}
*/
public
static
function
getSubscribedEvents
():
array
{
return
[
LayoutBuilderEvents
::
SECTION_COMPONENT_BUILD_RENDER_ARRAY
=>
[
'onBuildRender'
,
110
,
],
];
}
/**
* Set variation context when it is possible for VariationFieldBlock.
*
* @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event
* The section component render event.
*/
public
function
onBuildRender
(
SectionComponentBuildRenderArrayEvent
$event
):
void
{
$block
=
$event
->
getPlugin
();
if
(
!
(
$block
instanceof
VariationFieldBlock
))
{
return
;
}
$contexts
=
$event
->
getContexts
();
if
(
isset
(
$contexts
[
'layout_builder.entity'
]))
{
$product
=
$contexts
[
'layout_builder.entity'
]
->
getContextValue
();
if
(
!
(
$product
instanceof
ProductInterface
)
||
$product
->
isNew
()
||
!
$product
->
getDefaultVariation
()
)
{
return
;
}
$variation_context_name
=
'@commerce_product.product_variation_route_context:commerce_product_variation'
;
$variation_context
=
$contexts
[
$variation_context_name
]
->
getContextValue
();
if
(
!
(
$variation_context
instanceof
ProductVariationInterface
)
||
$variation_context
->
isNew
()
)
{
$context_definition
=
new
EntityContextDefinition
(
'entity:commerce_product_variation'
,
$this
->
t
(
'Product variation'
));
$context
=
new
Context
(
$context_definition
,
$product
->
getDefaultVariation
());
$block
->
setContext
(
'entity'
,
$context
);
}
}
}
}
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