コード例 #1
0
 public function renderIncludedBlock($key, BlockManager $parent = null, $type = "Text", $addWhenEmpty = false, $defaultContent = "", $editorExtraAttributes = "", $blockExtraOptions = array())
 {
     $blocksRepository = $this->container->get('red_kite_cms.factory_repository');
     $repository = $blocksRepository->createRepository('Block');
     $blocks = $repository->retrieveContents(null, null, $key);
     $blockManagerFactory = $this->container->get('red_kite_cms.block_manager_factory');
     $extraOptions = array('parent_slot_name' => $key);
     if (null !== $parent && preg_match('/' . $parent->get()->getId() . '\\-([0-9]+)/', $key, $matches)) {
         $extraOptions['parent_block_id'] = $parent->get()->getId();
         $extraOptions['key'] = $matches[1];
     }
     if (count($blocks) > 0) {
         $alBlock = $blocks[0];
         $type = $alBlock->getType();
         $blockManager = $blockManagerFactory->createBlockManager($type);
         if (null !== $blockManager) {
             $blockManager->set($alBlock);
             $blockManager->setBlockExtraOptions($blockExtraOptions);
             if (null !== $parent) {
                 $blockManager->setEditorDisabled($parent->getEditorDisabled());
             }
             return $this->renderBlock($blockManager, '_included_block.html.twig', true, $editorExtraAttributes, $extraOptions);
         }
         // @codeCoverageIgnoreStart
     }
     // @codeCoverageIgnoreEnd
     if (true === $addWhenEmpty) {
         if (null === $parent) {
             throw new RuntimeException($this->translator->translate('twig_extension_valid_block_manager_required', array(), 'RedKiteCmsBundle'));
         }
         $blockManager = $blockManagerFactory->createBlockManager($type);
         if (null !== $blockManager) {
             $blockManager->setEditorDisabled($parent->getEditorDisabled());
             $blockManager->setBlockExtraOptions($blockExtraOptions);
             $parentBlock = $parent->get();
             $values = array("PageId" => $parentBlock->getPageId(), "LanguageId" => $parentBlock->getLanguageId(), "SlotName" => $key, "Type" => $type, "ContentPosition" => 1);
             if (!empty($defaultContent)) {
                 $values["Content"] = $defaultContent;
             }
             $blockManager->save($values);
             return $this->renderBlock($blockManager, '_included_block.html.twig', true, $editorExtraAttributes, $extraOptions);
         }
         // @codeCoverageIgnoreStart
     }
     // @codeCoverageIgnoreEnd
     return sprintf('<div data-editor="enabled" data-block-id="0" data-slot-name="%s" data-included="1">%s</div>', $key, $this->translator->translate('twig_extension_empty_slot', array(), 'RedKiteCmsBundle'));
 }