Networking\InitCmsBundle\Controller\PageAdminController::linkAction PHP Method

linkAction() public method

Link pages as translations of each other
public linkAction ( Request $request, $id, $locale ) : RedirectResponse | Response
$request Symfony\Component\HttpFoundation\Request
$id
$locale
return Symfony\Component\HttpFoundation\RedirectResponse | Symfony\Component\HttpFoundation\Response
    public function linkAction(Request $request, $id, $locale)
    {
        /** @var PageInterface $page */
        $page = $this->admin->getObject($id);
        if (!$page) {
            throw new NotFoundHttpException(sprintf('unable to find the Page with id : %s', $id));
        }
        if ($request->getMethod() == 'POST') {
            $linkPageId = $request->get('page');
            if (!$linkPageId) {
                $this->get('session')->getFlashBag()->add('sonata_flash_error', 'flash_link_error');
            } else {
                /** @var PageInterface $linkPage */
                $linkPage = $this->admin->getObject($linkPageId);
                $page->addTranslation($linkPage);
                $this->admin->update($page);
                if ($this->isXmlHttpRequest()) {
                    $html = $this->renderView('NetworkingInitCmsBundle:PageAdmin:page_translation_settings.html.twig', array('object' => $page, 'admin' => $this->admin));
                    return $this->renderJson(array('result' => 'ok', 'html' => $html));
                }
                $this->get('session')->getFlashBag()->add('sonata_flash_success', 'flash_link_success');
                return new RedirectResponse($this->admin->generateUrl('edit', array('id' => $page->getId())));
            }
        }
        $pages = $this->admin->getModelManager()->findBy($this->admin->getClass(), array('locale' => $locale));
        if (count($pages)) {
            $pages = new ArrayCollection($pages);
            $originalLocale = $page->getLocale();
            $pages = $pages->filter(function (PageInterface $linkPage) use($originalLocale) {
                return !in_array($originalLocale, $linkPage->getTranslatedLocales());
            });
        }
        return $this->render('NetworkingInitCmsBundle:PageAdmin:page_translation_link_list.html.twig', array('page' => $page, 'pages' => $pages, 'locale' => $locale, 'original_language' => \Locale::getDisplayLanguage($page->getLocale()), 'language' => \Locale::getDisplayLanguage($locale), 'admin' => $this->admin));
    }