]> _ Git - cubist_cms-back.git/commitdiff
wip #5161 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 15 Mar 2022 11:52:52 +0000 (12:52 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 15 Mar 2022 11:52:52 +0000 (12:52 +0100)
src/app/Magic/Models/CubistMagicTranslatableModel.php

index e971209e63d699f4bf88cfd735227d943bdc1221..4fc8dc36cbf43b6b0988968259a2b8c8b082cb55 100644 (file)
@@ -19,6 +19,8 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
         getAvailableLocales as protected getAvailableLocalesTranslations;
     }
 
+    protected $_translations = [];
+
     /**
      * @var array
      */
@@ -56,6 +58,10 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
     public function getTranslations(string $key = null): array
     {
         if ($key !== null) {
+            if (isset($this->_translations[$key])) {
+                return $this->_translations[$key];
+            }
+            start_measure('Get translation of ' . $key);
             $this->guardAgainstNonTranslatableAttribute($key);
             $attrs = $this->getAttributes();
             if (!isset($attrs[$key])) {
@@ -71,18 +77,21 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
             }
 
             if ($key === 'slug') {
-                return array_filter($v, function ($value) {
+                $res = array_filter($v, function ($value) {
                     return $value !== null;
                 });
             } else {
-                return array_filter($v, function ($value) {
+                $res = array_filter($v, function ($value) {
                     return $value !== null && $value !== '';
                 });
             }
+            $this->_translations[$key] = $res;
+            stop_measure('Get translation of ' . $key);
+            return $res;
         }
 
         start_measure('Get all translations');
-        $res= array_reduce($this->getTranslatableAttributes(), function ($result, $item) {
+        $res = array_reduce($this->getTranslatableAttributes(), function ($result, $item) {
             $result[$item] = $this->getTranslations($item);
 
             return $result;