1

Been pulling my hair out over this for a day and exhausted my google foo. I have inherited a Silverstripe 3.4 site that we have upgraded to 4.4. But something odd has been going on with certain images after running MigrateFilesTask.

I think this is something to do with a file being attached to an unversioned objects that are accessed via ModelAdmin. But I have not been able to find a definitive solution.

Code for this object below. Problems experienced are under it.

<?php

use SilverStripe\Assets\Image;
use gorriecoe\Link\Models\Link;
use SilverStripe\Security\Member;
use SilverStripe\Control\Controller;
use SilverStripe\View\Parsers\URLSegmentFilter;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\FieldGroup;
use gorriecoe\LinkField\LinkField;
use SilverStripe\TagField\TagField;
use SilverStripe\ORM\DataObject;
use SilverStripe\SelectUpload\SelectUploadField;

class Person extends DataObject
{

  private static $db = array(
    'FirstName'        => 'Varchar(128)',
    'LastName'         => 'Varchar(128)',
    'Role'             => 'Varchar(128)',
    'DirectDialNumber' => 'Varchar(128)',
    'Email'            => 'Varchar(128)',
    'CellphoneNumber'  => 'Varchar(30)',
    'DirectDial'       => 'Varchar(30)',
    'UrlSegment'       => 'Varchar(255)',
    'Blurb'            => 'HTMLText',
    'SortOrder'        => 'Int'
  );

  private static $has_one = array(
    'Image' => Image::class,
    'Office' => 'Office',
    'LinkedIn' => Link::class,
    'Member' => Member::class
  );

  private static $many_many = array(
    'Interests' => 'Section'
  );

  private static $belongs_many_many = array(
    'ElementCollection' => 'ElementCollection'
  );

  static $sort_fields = array(
    'FirstName' => 'First name',
    'LastName' => 'Last name',
    'Role' => 'Role'
  );

  private static $summary_fields = array(
    'Name' => 'Name',
    'Role' => 'Role',
    'Office.Name' => 'Office'
  );

  private static $searchable_fields = array(
    'FirstName',
    'LastName',
    'Role'
  );

  // For use with the ElementCollection
  public static $templates = array(
    'ElementPeople' => 'Default',
    'ElementPeopleAlternative' => 'Alternative'
  );


  public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->removeByName( ['SortOrder', 'ElementCollection', 'FirstName', 'LastName', 'Interests'] );

    $firstname = TextField::create('FirstName', 'First name');
    $lastname = TextField::create('LastName', 'Last name');

    $fields->addFieldsToTab('Root.Main', FieldGroup::create($firstname, $lastname)->setTitle('Name')->setName('Name'), 'Role');

    $image = UploadField::create('Image', 'Photo');
    $image->setFolderName('Uploads/People');
    $image->setCanSelectFolder(false);
    $fields->addFieldToTab('Root.Main', $image);

    $linkedin = LinkField::create('LinkedIn', 'LinkedIn', $this);
    $fields->addFieldToTab('Root.Main', $linkedin);

    $interests = TagField::create(
      'Interests',
      'Interests Tags',
      Section::get(),
      $this->Interests()
    )->setShouldLazyLoad(true)
     ->setCanCreate(false);

    $fields->addFieldToTab('Root.Main', $interests);

    return $fields;
  }

  public function onBeforeWrite()
  {

    $count = 1;
    $this->UrlSegment = $this->generateURLSegment();
    while (!$this->validURLSegment()) {
      $this->UrlSegment = preg_replace('/-[0-9]+$/', null, $this->UrlSegment) . '-' . $count;
      $count++;
    }
    parent::onBeforeWrite();
  }
}

Problem #1 is after running MigrateFileTask, ALL existing images attached to instances of this class get moved from /assets/Uploads/People to /assets/.protected/Uploads/People. The confusing part here is that there is one other class called Company that is structurally near identical, yet images for that remain in /assets/Uploads/Companies as expected.

Problem #2 is if I create a new Person object and attach an image, that image is in Draft, sitting in /assets/.protected/Uploads/People with no method of actually publishing it. Meanwhile, if I do the same with a Company object, the image is still in Draft, but I can see it in the CMS.

Can someone offer some guidance on the above? At this point I'd be happy to just be able for images to be published when the DO is and I'll manually go through every single Person record and hit save myself just to get this upgrade over the line.

Aaryn
  • 1,601
  • 2
  • 18
  • 31

3 Answers3

1

You should be able to fix this issue by adding the image to your DataObejct's owns property. Basically add this:

    private static $owns = [
        'Image'
    ];

Basically owns tells a DataObject which objects to publish when it is saved:

More info in the docs: https://docs.silverstripe.org/en/4/developer_guides/model/versioning/#defining-ownership-between-related-versioned-dataobjects

PsychoMo
  • 699
  • 5
  • 17
  • That was one of the first things I tried. After digging around online, it looks like it doesn't actually do anything for unversioned objects. Certainly it doesn't do anything with this class or others like it. Images are still being saved as draft. – Aaryn Sep 03 '19 at 19:27
  • There's an extension you can add - `RecursivePublishable` or something like that – scrowler Sep 04 '19 at 00:39
  • 1
    @RobbieAverill, According to SS4 docs, it's already included on DataObject. Discussion here: https://github.com/silverstripe/silverstripe-framework/issues/7359. Someone from a bit under two years ago with the same issue on this question, and the answer suggests it was in place then. https://stackoverflow.com/questions/47392211/silverstripe-file-relation-in-modeladmin-doesnt-publish – Aaryn Sep 06 '19 at 00:35
0

The cause of issue #1 was found. Leaving this here in case it helps someone in future:

The database table File has a row for every File and Folder in the system. This table has a column called "CanViewType". It exists in both Silverstripe 3 and 4.

For the particular Folder that was causing trouble during the Migration process, I found it was the only one with that column set to "OnlyTheseUsers". The rest were set to "Inherit". This was the state of the table before the upgrade.

I'm unsure how or by what mechanism that row is ever changed, but the solution to problem #1 was to manually change that field to "Inherit" before running FileMigrationTask.

Issue #2 persists, but it looks like there are two very different issues here.

Aaryn
  • 1,601
  • 2
  • 18
  • 31
0

OK. So sorted problem #2 finally (see other answer for solution to #1), but it's put a massive dent in our confidence in Silverstripe and sparked a meeting here.

Code for future readers:

In your unversioned DataObject, add this. In this case, my file object is called "Image". If you had more than one file to publish on save, you would have to add one of these IF blocks for each.

public function onAfterWrite()
{
  if ($this->Image()->exists() && !$this->Image()->isPublished()) {
    $this->Image()->doPublish();
  }
  parent::onAfterWrite();
}

SIDENOTE:

This Object/File relationship really is a strange design choice. Are there actually any situations where you would want to attach a file or image to a data object and NOT publish that file at the same time as you save/publish the object/page? Developers even need to explicitly define that on Versioned objects using $owns - which I'm happy to bet that most developers have to add more times than NOT. Which should really tell a us something is around wrong way.

Adding an image to a CMS system shouldn't be hard. It should take reading basic docs at the most. Not Googling, deep API doc dives (which don't answer much) or posting on StackOverlfow (where no one really knows the answer) over three days. It's an image. A core function of the product.

I've been working with SS since v2.4 and seen all the hard lessons learned to get to v4. But this appears to be a textbook case of the simple being over-engineered.

Aaryn
  • 1,601
  • 2
  • 18
  • 31
  • I think this is an example of the push of "Everything is versioned" in SS4, I think in some cases un-versioned objects have pushed through the cracks. – PsychoMo Sep 06 '19 at 09:07
  • I would advise caution with the code above, I have used similar in the past and it has caused lots of issues with infinite loops, due to the fact `isPublished()` doesn't always seem to report accurately. I have found that just using `$object->publishRecursive();` works better and you also don't need to check if objects are already published. – PsychoMo Sep 06 '19 at 09:10
  • Thanks @PsychoMo. Where would `$object->publishRecursive()` go exactly? – Aaryn Sep 07 '19 at 02:20
  • Try swapping `$this->Image()->doPublish();` with `$this->Image()->publishRecursive()`. You should also then be able to remove the If statement. – PsychoMo Sep 08 '19 at 07:46