silverstripe /
silverstripe-elemental-blocks
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace SilverStripe\ElementalBlocks\Block; |
||||
| 4 | |||||
| 5 | use DNADesign\Elemental\Models\BaseElement; |
||||
| 6 | use SilverStripe\Assets\File; |
||||
| 7 | use SilverStripe\Assets\Image_Backend; |
||||
| 8 | use SilverStripe\Core\Manifest\ModuleResourceLoader; |
||||
| 9 | use SilverStripe\ORM\FieldType\DBHTMLText; |
||||
| 10 | |||||
| 11 | class FileBlock extends BaseElement |
||||
| 12 | { |
||||
| 13 | private static $has_one = [ |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 14 | 'File' => File::class, |
||||
| 15 | ]; |
||||
| 16 | |||||
| 17 | private static $owns = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 18 | 'File', |
||||
| 19 | ]; |
||||
| 20 | |||||
| 21 | private static $singular_name = 'file'; |
||||
|
0 ignored issues
–
show
|
|||||
| 22 | |||||
| 23 | private static $plural_name = 'files'; |
||||
|
0 ignored issues
–
show
|
|||||
| 24 | |||||
| 25 | private static $icon = 'font-icon-block-file'; |
||||
|
0 ignored issues
–
show
|
|||||
| 26 | |||||
| 27 | private static $table_name = 'S_EB_FileBlock'; |
||||
|
0 ignored issues
–
show
|
|||||
| 28 | |||||
| 29 | public function getType() |
||||
| 30 | { |
||||
| 31 | return _t(__CLASS__ . '.BlockType', 'File'); |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | public function getSummary() |
||||
| 35 | { |
||||
| 36 | if ($this->File() && $this->File()->exists()) { |
||||
|
0 ignored issues
–
show
The method
File() does not exist on SilverStripe\ElementalBlocks\Block\FileBlock. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 37 | return $this->getSummaryThumbnail() . $this->File()->Title; |
||||
| 38 | } |
||||
| 39 | return ''; |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * Return a thumbnail of the file, if it's an image. Used in GridField preview summaries. |
||||
| 44 | * |
||||
| 45 | * @return DBHTMLText |
||||
| 46 | */ |
||||
| 47 | public function getSummaryThumbnail() |
||||
| 48 | { |
||||
| 49 | $data = []; |
||||
| 50 | |||||
| 51 | if ($this->File() && $this->File()->exists()) { |
||||
| 52 | if ($this->File()->getIsImage()) { |
||||
| 53 | // Stretch to maximum of 36px either way then trim the extra off |
||||
| 54 | if ($this->File()->getOrientation() === Image_Backend::ORIENTATION_PORTRAIT) { |
||||
| 55 | $data['Image'] = $this->File()->ScaleWidth(36)->CropHeight(36); |
||||
| 56 | } else { |
||||
| 57 | $data['Image'] = $this->File()->ScaleHeight(36)->CropWidth(36); |
||||
| 58 | } |
||||
| 59 | } else { |
||||
| 60 | $data = [ |
||||
| 61 | 'Image' => ModuleResourceLoader::resourceURL( |
||||
| 62 | 'silverstripe/framework:client/images/app_icons/document_92.png' |
||||
| 63 | ), |
||||
| 64 | 'IsPlaceholder' => true |
||||
| 65 | ]; |
||||
| 66 | } |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | return $this->customise($data)->renderWith(__CLASS__ . '/SummaryThumbnail'); |
||||
| 70 | } |
||||
| 71 | } |
||||
| 72 |