/** * @param BlockInterface $block */ public function load(BlockInterface $block) { $gallery = $this->mediaManager->getRepository()->findByIds(json_decode($block->getValue())); if ($gallery) { $block->setGallery($gallery); } }
/** * @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)); } }
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); })); } }
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)); } }); }