2

Feeling a little stupid here, I'm hoping this is just something new in SS4 that I'm unaware of and not just my advanced age getting the better of me...

I have an UploadField in my DataObject. But when I loop through it in my template I can't get the Image to display.

MyPage.php:

<?php
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
class MyPage extends Page {
    private static $has_many = array(
        'MyObjects' => MyObject::class
    );
...

MyObject.php:

<?php
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextField;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
use SilverStripe\AssetAdmin\Forms\PreviewImageField;
class MyObject extends DataObject {
    private static $db = array(
        'Title'         => 'Varchar(255)'
    );
    private static $has_one = array(
        'MyImage'   => Image::class,
        'MyPage'    => MyPage::class
    );
    public function getGeneratedCMSFields() {
        $fields = FieldList::create(TabSet::create('Root'));
        $fields->addFieldToTab('Root.Main', TextField::create('Title', 'Title'));
        $fields->addFieldToTab('Root.Main', $img = UploadField::create('MyImage', 'My Image'));
        $img->getValidator()->setAllowedExtensions = array('jpg', 'jpeg', 'gif', 'png');
        $fields->removeFieldFromTab('Root.Main', 'MyPageID');
        return $fields;
    }
    public function getCMSFields() {
        $fields = $this->getGeneratedCMSFields();
        return $fields;
    }
}

MyPage.ss:

<% loop $MyObjects %>
    <h1>$Title</h1>
    <% with $MyImage %><img src="{$URL}" title="{$Up.Title}"><% end_with %>
<% end_loop %>

This prints out the $Title and alt img tag as expected, but no image. I've even tried to reduce the complexity to simply <% loop $MyObjects %> $MyImage <% end_loop %> and still no image. What am I missing?

scrowler
  • 24,273
  • 9
  • 60
  • 92
C.Brown
  • 33
  • 1
  • 8
  • What kind of output are you getting? Is the src attribute empty? Is `$Up.Title` populating? – UncleCheese Sep 29 '17 at 01:02
  • Isn't it `{$Filename}` instead of `{$URL}` – Gavin Bruce Sep 29 '17 at 03:09
  • @UncleCheese src is empty, but $Up.Title is populating as expected. – C.Brown Sep 29 '17 at 05:35
  • This is what it returns: `` – C.Brown Sep 29 '17 at 05:51
  • Image doesnt have URL according to this https://docs.silverstripe.org/en/4/developer_guides/files/images/ . Its either Link, FileName or AbsoluteUrl – Olli Tyynelä Sep 29 '17 at 10:46
  • I tried all of these and they produce absolutely nothing. Maybe I'm not using a class that I need to use? `$MyImage.AbsoluteLink` `$MyImage.FileName` `$MyImage.Link` `$MyImageID.AbsoluteLink` `$MyImageID.FileName` `$MyImageID.Link` – C.Brown Sep 29 '17 at 15:33
  • $URL should work. It gets `getURL()` from the `ImageManipulation` trait. What about `$ID` and `$ClassName`? Do those return their expected values? – UncleCheese Sep 30 '17 at 07:57
  • I've just tried your code and it's working fine for me, images are displaying. What filesystem are you using? Maybe it's something related to the way that the assets are being stored? What are the permissions on your assets directory? – scrowler Oct 01 '17 at 09:51
  • Ubuntu16.04 both desktop and server. Assets were set as: `chown -R myuser:www-data assets` `chmod -R 774 assets` These are the same permissions I've always used. – C.Brown Oct 01 '17 at 23:01
  • Some other things regarding my installation...This site is running alongside a handful of 3.6 installations that are reading and writing fine. In regards to the SS4 site, I couldn't use the installer.php. It was _hell-bent_ on insisting on using .htaccess and that's turned off in favor of controlling access via apache.conf files. I had to manually install the instance by hand creating the .env, config.yml, config.php, etc... So if there's something SS does in the install.php script that affects the assets, then that very well could be the place to look. – C.Brown Oct 01 '17 at 23:22
  • Also...I'm able to hit my image directly in my browser like: `myproject.localhost/assets/.protected/Uploads/78215515e1/myimage__Resampled.gif` So read permissions shouldn't be an issue. – C.Brown Oct 01 '17 at 23:44
  • Today, via composer, I installed a new instance of SS4 beta3. I created a special Apache conf file just for it and enabled .htaccess. I ran through the installer, made www-data the owner/group for the .env, _config.yml (which I had to create first), config.php and the entire assets folder. I was able to upload an image to the CMS. And I'm still not able to see an image on the front end...I'm simply stumped at this point. Going back to your question, Robbie, officially it's **Ubuntu** 16.04.3 LTS, **PHP** Version 7.1.9-1+ubuntu16.04.1+deb.sury.org+1, **Apache** 2.4.18 and **MariaDB** 10.2.8 – C.Brown Oct 02 '17 at 18:17
  • I think you should log an issue on the framework explaining your problem and the way you set up your server e.g. "I use apache.conf files instead of htaccess" – scrowler Oct 02 '17 at 20:51
  • 1
    For what it's worth...I neglected to read the part about publishing images in the release notes. Problem solved. – C.Brown Oct 21 '17 at 22:45
  • 1
    Can you elaborate on "part about publishing images in the release notes". I just have s similiar problem: https://stackoverflow.com/questions/47392211/silverstripe-file-relation-in-modeladmin-doesnt-publish – ivoba Nov 20 '17 at 12:50

0 Answers0