Example #1
0
 /**
  * @param BlockInterface $block
  */
 public function load(BlockInterface $block)
 {
     $gallery = $this->mediaManager->getRepository()->findByIds(json_decode($block->getValue()));
     if ($gallery) {
         $block->setGallery($gallery);
     }
 }
Example #2
0
 /**
  * @param BlockInterface $block
  */
 public function load(BlockInterface $block)
 {
     $ids = json_decode($block->getValue());
     if (empty($ids) || !count($ids)) {
         return;
     }
     $gallery = $this->mediaManager->getRepository()->findByIds($ids);
     uasort($gallery, function ($a, $b) use($ids) {
         return array_search($a->getId(), $ids) > array_search($b->getId(), $ids);
     });
     if ($gallery) {
         $block->setGallery(new ArrayCollection($gallery));
     }
 }
Example #3
0
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     if ($options['to_json']) {
         $builder->addModelTransformer(new CallbackTransformer(function ($original) {
             $ids = json_decode($original, true);
             return $this->mediaManager->getRepository()->findByIds($ids);
         }, function ($submitted) {
             $ids = [];
             foreach ($submitted as $media) {
                 $ids[] = $media->getId();
             }
             return json_encode($ids);
         }));
     }
 }
Example #4
0
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     if ($options['to_json']) {
         $builder->addModelTransformer(new CallbackTransformer(function ($original) {
             $ids = json_decode($original, true);
             $items = $this->mediaManager->getRepository()->findByIds($ids);
             if (is_array($items)) {
                 uasort($items, function ($a, $b) use($ids) {
                     return array_search($a->getId(), $ids) > array_search($b->getId(), $ids);
                 });
             }
             return array_values($items);
         }, function ($submitted) {
             $ids = [];
             foreach ($submitted as $media) {
                 $ids[] = $media->getId();
             }
             return json_encode($ids);
         }));
     }
     $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
         if ($event->getData() && count($event->getData())) {
             $this->sortedIds = $event->getData();
         }
     });
     $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
         $data = $event->getData();
         if (count($this->sortedIds) && is_array($data) && count($data)) {
             $ids = $this->sortedIds;
             uasort($data, function ($a, $b) use($ids) {
                 return array_search($a->getId(), $ids) > array_search($b->getId(), $ids);
             });
             $event->setData(array_values($data));
         }
     });
 }