コード例 #1
0
ファイル: ContentService.php プロジェクト: anoopogt/BWCMS
 /**
  * @param ContentEntity $content
  * @param Form $form
  * @param ContentTypeInterface|BaseContentType $classInstance
  * @return Form|void
  */
 public final function loadFormData(ContentEntity $content = null, Form $form = null, ContentTypeInterface $classInstance)
 {
     if (null === $content) {
         return;
     }
     if (null === $form) {
         return;
     }
     $form->get('id')->setData($content->getId());
     $form->get('type')->setData($content->getType());
     $form->get('schema')->setData($content->getSchema());
     $form->get('scope')->setData($content->getScope());
     $form->get('template')->setData($content->getTemplate());
     $form->get('status')->setData($content->getStatus());
     $form->get('title')->setData($content->getTitle());
     if ($classInstance->isSummaryEnabled()) {
         $form->get('summary')->setData($content->getSummary());
     }
     if ($classInstance->isContentEnabled()) {
         $form->get('content')->setData($content->getContent());
     }
     $form->get('slug')->setData($content->getSlug());
     $form->get('sortBy')->setData($content->getSortBy());
     $form->get('sortOrder')->setData($content->getSortOrder());
     if ($classInstance->isPublishDateEnabled()) {
         $form->get('publishDate')->setData($content->getPublishDate());
     }
     if ($classInstance->isExpireDateEnabled()) {
         $form->get('expireDate')->setData($content->getExpireDate());
     }
     $taxonomyRelations = $classInstance->getTaxonomyRelations();
     if (!empty($taxonomyRelations)) {
         $existingRelation = $content->getRelation();
         $fieldValues = array();
         foreach ($existingRelation as $relation) {
             $formFieldName = $taxonomyRelations[$relation->getRelation()]['fieldName'];
             $fieldValues[$formFieldName][] = $relation->getRelatedContent()->getId();
         }
         foreach ($taxonomyRelations as $taxonomyRelation) {
             $fieldName = $taxonomyRelation['fieldName'];
             if (isset($fieldValues[$fieldName]) && !empty($fieldValues[$fieldName])) {
                 try {
                     $formField = $form->get($fieldName);
                 } catch (\OutOfBoundsException $e) {
                     continue;
                 }
                 if ($taxonomyRelation['multiple']) {
                     $formField->setData($fieldValues[$fieldName]);
                 } else {
                     $singleValue = array_pop($fieldValues[$fieldName]);
                     if (!is_null($singleValue)) {
                         $formField->setData($singleValue);
                     }
                 }
             }
         }
     }
     $existingMeta = $content->getMeta();
     if (!empty($existingMeta)) {
         /**
          * @var ContentMetaEntity $meta
          */
         foreach ($existingMeta as $meta) {
             $metaField = $meta->getField();
             $metaValue = $meta->getValue();
             $metaType = $meta->getFieldType();
             try {
                 $formField = $form->get($metaField);
             } catch (\OutOfBoundsException $e) {
                 continue;
             }
             $fieldValue = $this->decodeDataFromDB($metaType, $metaField, $metaValue, $classInstance);
             $formField->setData($fieldValue);
         }
     }
     $form = $classInstance->loadFormData($content, $form);
     return $form;
 }