settingsForm = $form; $iteratorRecursive = static function ($block) use (&$iteratorRecursive, $form) { /** @var Block $block */ $reflector = new ReflectionMethod($block, 'settingsForm'); if ($reflector->getDeclaringClass()->getName() !== Block::class) { $block->settingsFormTab($form); $block->settingsForm($form); } $block->rotationSettingsForm($form); $block->blocksIterator($iteratorRecursive); }; $this->allowSettingsFill = false; $iteratorRecursive($this); $this->allowSettingsFill = true; } /** * Is settings fill allowed * @return bool */ public function isSettingsFillAllowed() { return $this->allowSettingsFill; } /** * Get block settings form instance * @return Form|null */ public function getSettingsForm() { if (! is_null($this->settingsForm)) { return $this->settingsForm; } if ($this->settingsFormPage) { return $this->app->theme()->getPageSettingsForm($this->settingsFormPage); } $page = $this->getParentPage(); if (! $page) { return null; } $page = $page->getAliasFor() ?? $page; return $this->app->theme()->getPageSettingsForm($page); } /** * Get block settings by key * @param string $key * @return mixed|null */ public function getBlockSettings($key) { if (is_array($this->settingsData)) { return $this->settingsData[$key] ?? null; } return null; } /** * Load settings & cache * @param string|null $lang */ public function loadSettings(?string $lang = null) { if (! is_null($this->settingsData)) { return; } $cacheKey = $this->settingsCacheKey($lang); $this->settingsData = Cache::rememberForever($cacheKey, function () { $data = []; # Save blocks state to revert after building settings form & cache data $blocks = $this->blocks; $form = $this->getSettingsForm(); if ($form) { $iteratorRecursive = static function ($block) use (&$iteratorRecursive, $form, &$data) { /** @var Block $block */ $key = $block->getKey(); $reflector = new ReflectionMethod($block, 'settingsForm'); if ($reflector->getDeclaringClass()->getName() !== Block::class) { $block->settingsFormTab($form); $data[$key] = array_merge($data[$key] ?? [], $form->getActiveTabValues()); } $rotation = $block->rotationSettingsValues($form); if (! empty($rotation)) { $data[$key] = array_merge($data[$key] ?? [], $rotation); } $block->blocksIterator($iteratorRecursive); }; $this->allowSettingsFill = false; $iteratorRecursive($this); $this->allowSettingsFill = true; } $this->blocks = $blocks; return $data; }); # json to array if (is_string($this->settingsData)) { $this->settingsData = json_decode($this->settingsData, true); $toArray = true; } if (! is_array($this->settingsData)) { $this->settingsData = []; $toArray = true; } if (isset($toArray)) { Cache::forever($cacheKey, $this->settingsData); } } /** * Get settings cache key * @param string|null $lang * @return string */ protected function settingsCacheKey(?string $lang = null) { return 'view.settings.' . $this->getKey() . '.' . ($lang ?? $this->locale->current()); } /** * Reset settings cache */ public function settingsCacheReset() { foreach ($this->locale->getLanguages() as $lang) { Cache::forget($this->settingsCacheKey($lang)); } } }