private function validateLanguageMainForPage($pageId) { $page = PageModel::findWithDetails($pageId); // Moving a root page does not affect language assignments if (null === $page || !$page->languageMain || 'root' === $page->type) { return; } $duplicates = PageModel::countBy(['id IN (' . implode(',', Database::getInstance()->getChildRecords($page->rootId, 'tl_page')) . ')', 'languageMain=?', 'id!=?'], [$page->languageMain, $page->id]); // Reset languageMain if another page in the new page tree has the same languageMain if ($duplicates > 0) { $this->resetPageAndChildren($page->id); return; } $pageFinder = new PageFinder(); $masterRoot = $pageFinder->findMasterRootForPage($page); // Reset languageMain if current tree has no master or if it's the master tree if (null === $masterRoot || $masterRoot->id === $page->rootId) { $this->resetPageAndChildren($page->id); return; } // Reset languageMain if the current value is not a valid ID of the master tree if (!in_array($page->id, Database::getInstance()->getChildRecords($masterRoot->id, 'tl_page'), false)) { $this->resetPageAndChildren($page->id); } }
/** * Validate input value when saving tl_page.languageMain field. * * @param mixed $value * @param DataContainer $dc * * @return mixed * * @throws \RuntimeException */ public function onSaveLanguageMain($value, DataContainer $dc) { // Validate that there is no other page in the current page tree with the same languageMain assigned if ($value > 0) { $currentPage = PageModel::findWithDetails($dc->id); $childIds = \Database::getInstance()->getChildRecords($currentPage->rootId, 'tl_page'); $duplicates = PageModel::countBy(['tl_page.id IN (' . implode(',', $childIds) . ')', 'tl_page.languageMain=?', 'tl_page.id!=?'], [$value, $dc->id]); if ($duplicates > 0) { throw new \RuntimeException($GLOBALS['TL_LANG']['MSC']['duplicateMainLanguage']); } } return $value; }