/** * @return BlockPropertyMetadata */ private function getMediaMetadata() { if (!$this->property->getMetadata()->offsetExists('media')) { $this->property->addMetadata(new BlockPropertyMetadata('media', $this->property)); } return $this->property->getMetadata()->get('media'); }
/** * @param mixed $value * @return null */ public function reverseTransform($value) { if (empty($value)) { $this->property->getMetadata()->clear(); return null; } if (!is_array($value)) { throw new TransformationFailedException(sprintf('Expected array only, got [%s]', gettype($value))); } if (!isset($value['url'])) { throw new TransformationFailedException('Media URL is missing.'); } $mediaEmbed = $this->container['cms.media_embed']; /* @var $mediaEmbed MediaEmbed */ $mediaObject = $mediaEmbed->parseUrl($value['url']); if ($mediaObject === null) { throw new TransformationFailedException(sprintf('Failed to parse media URL [%s].', $value['url'])); } $metadata = $this->property->getMetadata(); $mediaElement = new MediaReferencedElement(); $mediaElement->setUrl($value['url']); if (isset($value['width'])) { $width = (int) $value['width']; if ($width < 1) { throw new TransformationFailedException(sprintf('Invalid width value: [%s]', $value['width'])); } $mediaElement->setWidth($width); } if (isset($value['height'])) { $height = (int) $value['height']; if ($height < 1) { throw new TransformationFailedException(sprintf('Invalid height value: [%s]', $value['height'])); } $mediaElement->setHeight($height); } if (!isset($metadata['media'])) { $this->property->addMetadata(new BlockPropertyMetadata('media', $this->property)); } $metadata['media']->setReferencedElement($mediaElement); return $value['url']; }