@@ -148,6 +148,9 @@ public function one(array $data, array $options = [])
148
148
}
149
149
150
150
$ hasTranslations = $ this ->_hasTranslations ($ options );
151
+ if ($ hasTranslations ) {
152
+ $ options = $ this ->_addTranslationsToFieldList ($ options );
153
+ }
151
154
152
155
$ errors = $ this ->_validate ($ data , $ options , true );
153
156
$ properties = [];
@@ -169,7 +172,10 @@ public function one(array $data, array $options = [])
169
172
$ converter = Type::build ($ columnType );
170
173
$ value = $ converter ->marshal ($ value );
171
174
} elseif ($ key === '_translations ' && $ hasTranslations ) {
172
- $ value = $ this ->_mergeTranslations (null , $ value , $ errors , $ options );
175
+ list ($ value , $ translationsErrors ) = $ this ->_mergeTranslations (null , $ value , $ options );
176
+ if (!empty ($ translationsErrors )) {
177
+ $ errors += $ translationsErrors ;
178
+ }
173
179
}
174
180
$ properties [$ key ] = $ value ;
175
181
}
@@ -454,48 +460,56 @@ protected function _loadBelongsToMany($assoc, $ids)
454
460
}
455
461
456
462
/**
457
- * Call translation merge. Validations errors during merge will be added to `$errors` param
463
+ * Call translation merge
458
464
*
459
465
* @param \Cake\Datasource\EntityInterface $original The original entity
460
466
* @param array $data key value list of languages with fields to be merged into the translate entity
461
- * @param array $errors array with entity errors
462
467
* @param array $options list of options
463
- * @return array|null
468
+ * @return array
464
469
*/
465
- protected function _mergeTranslations ($ original , array $ data , array & $ errors , array $ options = [])
470
+ protected function _mergeTranslations ($ original , array $ data , array $ options = [])
466
471
{
467
472
$ result = $ this ->_table ->mergeTranslations ($ original , $ data , $ this , $ options );
468
473
474
+ $ errors = [];
469
475
if (is_array ($ result )) {
470
476
if ((bool )$ result [1 ]) {
471
477
$ errors ['_translations ' ] = $ result [1 ];
472
478
}
473
479
$ result = $ result [0 ];
474
480
}
475
481
476
- return $ result ;
482
+ return [ $ result, $ errors ] ;
477
483
}
478
484
479
485
/**
480
486
* Return if table contains translate behavior or we specificate to use via `translations` options.
481
487
*
482
- * In case that $options has `fieldList` option and `_translations` field is not present inside it, it will include
483
- *
484
488
* ### Options:
485
489
*
486
490
* - translations: Set to false to disable translations
487
491
*
488
492
* @param array $options List of options
489
493
* @return bool
490
494
*/
491
- protected function _hasTranslations (array &$ options = [])
495
+ protected function _hasTranslations (array $ options = [])
496
+ {
497
+ return ($ this ->_table ->behaviors ()->hasMethod ('mergeTranslations ' ) && (bool )$ options ['translations ' ]);
498
+ }
499
+
500
+ /**
501
+ * Add `_translations` field to `fieldList` $options if it's not present inside
502
+ *
503
+ * @param array $options List of options
504
+ * @return array
505
+ */
506
+ protected function _addTranslationsToFieldList (array $ options = [])
492
507
{
493
- $ hasTranslations = ($ this ->_table ->behaviors ()->hasMethod ('mergeTranslations ' ) && (bool )$ options ['translations ' ]);
494
- if ($ hasTranslations && !empty ($ options ['fieldList ' ]) && !in_array ('_translations ' , $ options ['fieldList ' ])) {
508
+ if (!empty ($ options ['fieldList ' ]) && !in_array ('_translations ' , $ options ['fieldList ' ])) {
495
509
array_push ($ options ['fieldList ' ], '_translations ' );
496
510
}
497
511
498
- return $ hasTranslations ;
512
+ return $ options ;
499
513
}
500
514
501
515
/**
@@ -553,6 +567,9 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
553
567
}
554
568
555
569
$ hasTranslations = $ this ->_hasTranslations ($ options );
570
+ if ($ hasTranslations ) {
571
+ $ options = $ this ->_addTranslationsToFieldList ($ options );
572
+ }
556
573
557
574
$ errors = $ this ->_validate ($ data + $ keys , $ options , $ isNew );
558
575
$ schema = $ this ->_table ->schema ();
@@ -582,7 +599,10 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
582
599
continue ;
583
600
}
584
601
} elseif ($ key === '_translations ' && $ hasTranslations ) {
585
- $ value = $ this ->_mergeTranslations ($ original , $ value , $ errors , $ options );
602
+ list ($ value , $ translationsErrors ) = $ this ->_mergeTranslations ($ original , $ value , $ options );
603
+ if (!empty ($ translationsErrors )) {
604
+ $ errors += $ translationsErrors ;
605
+ }
586
606
}
587
607
588
608
$ properties [$ key ] = $ value ;
0 commit comments