コード例 #1
0
 public function testGetFormDataByLevel1()
 {
     $formdata = ['opifer_content' => [], 'opifer_content_valueset_namedvalues_blocks' => '', 'opifer_content_valueset_namedvalues_carousel' => '', 'nested_content__blocks__nesting__0' => ['title' => 'title 1', '_token' => 'S0M3T0K3N'], 'nested_content__blocks__nesting__0_valueset_namedvalues_test' => '', 'nested_content__test__nesting__0__test__nesting__0' => ['title' => 'title 2', '_token' => 'S0M3T0K3N'], 'nested_content__carousel__slide__0' => ['title' => 'slide 1', '_token' => 'S0M3T0K3N']];
     $expected = ['nested_content__blocks__nesting__0' => ['title' => 'title 1', '_token' => 'S0M3T0K3N']];
     $manager = new EavManager($this->pool, 'Opifer\\EavBundle\\Tests\\TestData\\ValueSet');
     $actual = $manager->getFormDataByLevel($formdata, 'blocks', 1, 'opifer_content');
     $this->assertEquals($expected, $actual);
 }
コード例 #2
0
ファイル: ContentManager.php プロジェクト: leonverschuren/Cms
 /**
  * Maps the formdata to the related nestedcontent item resursively
  *
  * @param ContentInterface $content
  * @param $request
  * @param int $level
  * @param string $parentKey
  *
  * @return bool
  *
  * @throws \Exception
  */
 public function recursiveContentMapper(Request $request, ContentInterface $content, $level = 1, $parentKey = 'opifer_content')
 {
     $formdata = $request->request->all();
     foreach ($content->getNestedContentAttributes() as $attribute => $value) {
         $relatedformdata = $this->eavManager->getFormDataByLevel($formdata, $attribute, $level, $parentKey);
         // Store the original Ids, so we can check later what items were removed
         $ids = [];
         $oldIds = [];
         if ($formdata[$parentKey . '_valueset_namedvalues_' . $attribute] != '') {
             $oldIds = explode(',', $formdata[$parentKey . '_valueset_namedvalues_' . $attribute]);
         }
         $sort = 0;
         foreach ($relatedformdata as $key => $data) {
             $keys = $this->eavManager->parseNestedTypeName($key);
             $nestedContent = $this->getContentByReference($keys['reference']);
             $nestedContent->setSlug(md5(time() + rand()));
             if ($nestedContent->getTitle() == "") {
                 $nestedContent->setTitle('_');
             }
             $form = new NestedType($key);
             $form = $this->formFactory->create($form, $nestedContent);
             $form->handleRequest($request);
             $nestedContent->setNestedSort($sort);
             $sort++;
             // We do not check the standard isValid() method here, because our form
             // is not actually submitted.
             if (count($form->getErrors(true)) < 1) {
                 $this->em->persist($value);
                 $value->addNested($nestedContent);
                 $nestedContent->setNestedIn($value);
                 $this->save($nestedContent);
                 $ids[] = $nestedContent->getId();
             } else {
                 throw new NestedContentFormException('Something went wrong while saving nested content. Message: ' . $form->getErrors());
             }
             $this->recursiveContentMapper($request, $nestedContent, $level + 1, $key);
         }
         $this->remove(array_diff($oldIds, $ids));
     }
     return true;
 }