| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\CommonExtensions; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use Embed\Embed; |
||
| 7 | use SilverStripe\ORM\DataObject; |
||
| 8 | use SilverStripe\Forms\FieldList; |
||
| 9 | use SilverStripe\ORM\DataExtension; |
||
| 10 | use SilverStripe\ORM\ValidationResult; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Embed external url on this data object |
||
| 14 | * |
||
| 15 | * @property DataObject|\LeKoala\CommonExtensions\EmbeddableExtension $owner |
||
| 16 | * @property string $EmbedURL |
||
| 17 | */ |
||
| 18 | class EmbeddableExtension extends DataExtension |
||
| 19 | { |
||
| 20 | private static $db = [ |
||
| 21 | "EmbedURL" => "Varchar(255)" |
||
| 22 | ]; |
||
| 23 | private static $casting = array( |
||
| 24 | "EmbedURL" => "HTMLFragment", |
||
| 25 | ); |
||
| 26 | |||
| 27 | public function updateCMSFields(FieldList $fields) |
||
| 28 | { |
||
| 29 | $EmbedURL = $fields->dataFieldByName('EmbedURL'); |
||
| 30 | if ($EmbedURL) { |
||
| 31 | $EmbedURL->setTitle(_t('EmbeddableExtension.EMBEDURL', 'Embed from URL')); |
||
| 32 | $EmbedURL->setDescription(_t('EmbeddableExtension.EMBEDURLDESC', 'Copy the url of a compatible ressource (eg: a Youtube video)')); |
||
| 33 | |||
| 34 | // Position properly |
||
| 35 | $Content = $fields->dataFieldByName('Content'); |
||
| 36 | if ($Content) { |
||
| 37 | $fields->insertAfter('Content', $EmbedURL); |
||
| 38 | } |
||
| 39 | $Description = $fields->dataFieldByName('Description'); |
||
| 40 | if ($Description) { |
||
| 41 | $fields->insertAfter('Description', $EmbedURL); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | public function validate(ValidationResult $validationResult) |
||
| 47 | { |
||
| 48 | if ($this->owner->EmbedURL) { |
||
| 49 | try { |
||
| 50 | $embed = Embed::create($this->owner->EmbedURL); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 51 | } catch (Exception $ex) { |
||
| 52 | $validationResult->addError($ex->getMessage()); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | public function EmbeddedContent() |
||
| 58 | { |
||
| 59 | $embed = Embed::create($this->owner->EmbedURL); |
||
| 60 | $html = $embed->code; |
||
| 61 | return $html; |
||
| 62 | } |
||
| 63 | } |
||
| 64 |