33namespace Drupal \commerce_product ;
44
55use Drupal \commerce_product \Controller \ProductVariationController ;
6+ use Drupal \commerce_product \Controller \ProductVariationTranslationController ;
7+ use Drupal \Core \Entity \EntityFieldManagerInterface ;
68use Drupal \Core \Entity \EntityTypeInterface ;
9+ use Drupal \Core \Entity \EntityTypeManagerInterface ;
710use Drupal \Core \Entity \Routing \DefaultHtmlRouteProvider ;
11+ use Drupal \Core \Extension \ModuleHandlerInterface ;
12+ use Symfony \Component \DependencyInjection \ContainerInterface ;
813use Symfony \Component \Routing \Route ;
914
1015/**
1116 * Provides routes for the product variation entity.
1217 */
1318class ProductVariationRouteProvider extends DefaultHtmlRouteProvider {
1419
20+ /**
21+ * The module handler.
22+ *
23+ * @var \Drupal\Core\Extension\ModuleHandlerInterface
24+ */
25+ protected $ moduleHandler ;
26+
27+ /**
28+ * Constructs a new ProductVariationRouteProvider.
29+ *
30+ * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
31+ * The entity type manager.
32+ * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
33+ * The entity field manager.
34+ * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
35+ * The module handler.
36+ */
37+ public function __construct (EntityTypeManagerInterface $ entity_type_manager , EntityFieldManagerInterface $ entity_field_manager , ModuleHandlerInterface $ module_handler ) {
38+ parent ::__construct ($ entity_type_manager , $ entity_field_manager );
39+
40+ $ this ->moduleHandler = $ module_handler ;
41+ }
42+
43+ /**
44+ * {@inheritdoc}
45+ */
46+ public static function createInstance (ContainerInterface $ container , EntityTypeInterface $ entity_type ) {
47+ return new static (
48+ $ container ->get ('entity_type.manager ' ),
49+ $ container ->get ('entity_field.manager ' ),
50+ $ container ->get ('module_handler ' )
51+ );
52+ }
53+
1554 /**
1655 * {@inheritdoc}
1756 */
@@ -21,6 +60,91 @@ public function getRoutes(EntityTypeInterface $entity_type) {
2160 $ collection ->add ('entity.commerce_product_variation.duplicate_form ' , $ duplicate_route );
2261 }
2362
63+ // Core can't generate the translation routes until #3004038 gets fixed.
64+ if ($ this ->moduleHandler ->moduleExists ('content_translation ' )) {
65+ $ default_parameters = [
66+ 'commerce_product ' => [
67+ 'type ' => 'entity:commerce_product ' ,
68+ ],
69+ 'commerce_product_variation ' => [
70+ 'type ' => 'entity: ' . 'commerce_product_variation ' ,
71+ ],
72+ ];
73+
74+ $ overview_route = new Route ($ entity_type ->getLinkTemplate ('drupal:content-translation-overview ' ));
75+ $ overview_route
76+ ->addDefaults ([
77+ '_controller ' => ProductVariationTranslationController::class . '::overview ' ,
78+ 'entity_type_id ' => 'commerce_product_variation ' ,
79+ ])
80+ ->setRequirements ([
81+ '_entity_access ' => 'commerce_product_variation.view ' ,
82+ '_access_content_translation_overview ' => 'commerce_product_variation ' ,
83+ ])
84+ ->setOption ('parameters ' , $ default_parameters )
85+ ->setOption ('_admin_route ' , TRUE );
86+
87+ $ add_route = new Route ($ entity_type ->getLinkTemplate ('drupal:content-translation-add ' ));
88+ $ add_route
89+ ->addDefaults ([
90+ '_controller ' => ProductVariationTranslationController::class . '::add ' ,
91+ 'source ' => NULL ,
92+ 'target ' => NULL ,
93+ '_title ' => 'Add ' ,
94+ 'entity_type_id ' => 'commerce_product_variation ' ,
95+ ])
96+ ->setRequirements ([
97+ '_entity_access ' => 'commerce_product_variation.view ' ,
98+ '_access_content_translation_manage ' => 'create ' ,
99+ ])
100+ ->setOption ('parameters ' , $ default_parameters + [
101+ 'source ' => [
102+ 'type ' => 'language ' ,
103+ ],
104+ 'target ' => [
105+ 'type ' => 'language ' ,
106+ ],
107+ ])
108+ ->setOption ('_admin_route ' , TRUE );
109+
110+ $ edit_route = new Route ($ entity_type ->getLinkTemplate ('drupal:content-translation-edit ' ));
111+ $ edit_route
112+ ->addDefaults ([
113+ '_controller ' => ProductVariationTranslationController::class . '::edit ' ,
114+ 'language ' => NULL ,
115+ '_title ' => 'Edit ' ,
116+ 'entity_type_id ' => 'commerce_product_variation ' ,
117+ ])
118+ ->setRequirement ('_access_content_translation_manage ' , 'update ' )
119+ ->setOption ('parameters ' , $ default_parameters + [
120+ 'language ' => [
121+ 'type ' => 'language ' ,
122+ ],
123+ ])
124+ ->setOption ('_admin_route ' , TRUE );
125+
126+ $ delete_route = new Route ($ entity_type ->getLinkTemplate ('drupal:content-translation-delete ' ));
127+ $ delete_route
128+ ->addDefaults ([
129+ '_entity_form ' => 'commerce_product_variation.content_translation_deletion ' ,
130+ 'language ' => NULL ,
131+ '_title ' => 'Delete ' ,
132+ 'entity_type_id ' => 'commerce_product_variation ' ,
133+ ])
134+ ->setRequirement ('_access_content_translation_manage ' , 'delete ' )
135+ ->setOption ('parameters ' , $ default_parameters + [
136+ 'language ' => [
137+ 'type ' => 'language ' ,
138+ ],
139+ ])
140+ ->setOption ('_admin_route ' , TRUE );
141+
142+ $ collection ->add ('entity.commerce_product_variation.content_translation_overview ' , $ overview_route );
143+ $ collection ->add ('entity.commerce_product_variation.content_translation_add ' , $ add_route );
144+ $ collection ->add ("entity.commerce_product_variation.content_translation_edit " , $ edit_route );
145+ $ collection ->add ("entity.commerce_product_variation.content_translation_delete " , $ delete_route );
146+ }
147+
24148 return $ collection ;
25149 }
26150
0 commit comments