addCategory() public method

Adds a category to the entity.
public addCategory ( Sulu\Bundle\CategoryBundle\Entity\CategoryInterface $category )
$category Sulu\Bundle\CategoryBundle\Entity\CategoryInterface
Esempio n. 1
0
 /**
  * Data can be set over by array.
  *
  * @param $media
  * @param $data
  * @param UserInterface $user
  *
  * @return Media
  */
 protected function setDataToMedia(Media $media, $data, $user)
 {
     foreach ($data as $attribute => $value) {
         if ($value || $attribute === 'tags' && $value !== null || $attribute === 'size' && $value !== null || $attribute === 'description' && $value !== null || $attribute === 'copyright' && $value !== null || $attribute === 'credits' && $value !== null || $attribute === 'categories' && $value !== null || $attribute === 'focusPointX' && $value !== null || $attribute === 'focusPointY' && $value !== null) {
             switch ($attribute) {
                 case 'size':
                     $media->setSize($value);
                     break;
                 case 'title':
                     $media->setTitle($value);
                     break;
                 case 'description':
                     $media->setDescription($value);
                     break;
                 case 'copyright':
                     $media->setCopyright($value);
                     break;
                 case 'credits':
                     $media->setCredits($value);
                     break;
                 case 'version':
                     $media->setVersion($value);
                     break;
                 case 'name':
                     $media->setName($value);
                     break;
                 case 'url':
                     $media->setUrl($value);
                     break;
                 case 'formats':
                     $media->setFormats($value);
                     break;
                 case 'storageOptions':
                     $media->setStorageOptions($value);
                     break;
                 case 'publishLanguages':
                     $media->setPublishLanguages($value);
                     break;
                 case 'contentLanguages':
                     $media->setContentLanguages($value);
                     break;
                 case 'tags':
                     $media->removeTags();
                     if (count($value)) {
                         foreach ($value as $tag) {
                             $tagEntity = $this->tagManager->findOrCreateByName($tag, $user->getId());
                             $media->addTag($tagEntity);
                         }
                     }
                     break;
                 case 'properties':
                     $media->setProperties($value);
                     break;
                 case 'changed':
                     $media->setChanged($value);
                     break;
                 case 'created':
                     break;
                 case 'changer':
                     if ($value instanceof UserInterface) {
                         $media->setChanger($value);
                     }
                     break;
                 case 'creator':
                     if ($value instanceof UserInterface) {
                         $media->setCreator($value);
                     }
                     break;
                 case 'mimeType':
                     $media->setMimeType($value);
                     break;
                 case 'collection':
                     $collectionEntity = $this->getCollectionById($value);
                     $media->setCollection($collectionEntity);
                     // set parent
                     break;
                 case 'type':
                     if (isset($value['id'])) {
                         $type = $this->typeManager->get($value['id']);
                         $media->setType($type);
                     }
                     break;
                 case 'categories':
                     $categoryIds = $value;
                     $media->removeCategories();
                     if (is_array($categoryIds) && !empty($categoryIds)) {
                         /** @var CategoryRepositoryInterface $repository */
                         $categories = $this->categoryRepository->findCategoriesByIds($categoryIds);
                         foreach ($categories as $category) {
                             $media->addCategory($category);
                         }
                     }
                     break;
                 case 'focusPointX':
                     $media->setFocusPointX($value);
                     break;
                 case 'focusPointY':
                     $media->setFocusPointY($value);
                     break;
             }
         }
     }
     return $media;
 }