View more context

 

brasileric

found this 🙄 https://forum.silverstripe.org/t/extension-for-file-class-updatecmsfields-not-working/187

Show 1 attachment(s)
Silverstripe Forum  
Extension for File class: updateCMSFields not working

Silverstripe Version: 4.1 Question: I’m trying to add an extension to the File class as follows: class FileExtension extends DataExtension { private static $db = array( 'Copyright' => 'Varchar(255)' ); public function updateCMSFields(FieldList $fields) { $fields->insertAfter( 'Title', TextField::create('Copyright', 'Copyright') ); } } registered the extension in mysite.yml: Silverstripe\Assets\File: extensions: - FileExte...

Hide attachment content
andante

In order to add a field to something in asset admin, you need to do it in multiple places: see this example from my own site: https://github.com/andrewandante/locket/blob/master/mysite/code/model/GalleryImageFormFactoryExtension.php

Show 1 attachment(s)
mysite/code/model/GalleryImageFormFactoryExtension.php

<?php

namespace AndrewAndante\Locket\Model;

use SilverStripe\Core\Extension;
use SilverStripe\Forms\DateField;
use SilverStripe\Forms\FieldList;
//use SilverStripe\TagField\TagField;

class GalleryImageFormFactoryExtension extends Extension
{

    public function updateFormFields(FieldList $fields, $controller, $formName, $context)
    {
        $image = $context['Record'];
//        $tagField = TagField::create(
//            'GalleryImageTags',
//            'Tags',
//            GalleryImageTag::get(),
//            $image->GalleryImageTags()
//        );
        $fields->addFieldsToTab('Editor.Details', [
            DateField::create('OriginalDate', 'Date this was taken'),
//            $tagField
        ]);
    }
}
Hide attachment content
andante

and also https://github.com/andrewandante/locket/blob/master/mysite/code/model/GalleryImageExtension.php

Show 1 attachment(s)
mysite/code/model/GalleryImageExtension.php

<?php

namespace AndrewAndante\Locket\Model;

use DateTime;
use SilverStripe\Assets\Image;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Security\InheritedPermissions;

class GalleryImageExtension extends DataExtension
{
    private static $db = [
        'OriginalDate' => 'DBDatetime',
    ];

//    private static $many_many = [
//        'GalleryImageTags' => GalleryImageTag::class,
//    ];

    private static $defaults = [
        'CanViewType' => InheritedPermissions::LOGGED_IN_USERS,
        'CanEditType' => InheritedPermissions::LOGGED_IN_USERS,
    ];

    public function getDisplayDate()
    {
        return $this->getOwner()->OriginalDate ? $this->getOwner()->dbObject('OriginalDate') : $this->getOwner()->dbObject('Created');
    }

    public function onBeforeWrite()
    {
        /** @var Image $image */
        $image = $this->getOwner();
        if (!$image->OriginalDate) {
            $filePath = implode('/', [ASSETS_PATH, '.protected', $image->File->getMetaData()['path']]);
            $exifData = exif_read_data($filePath);
            if ($exifData && isset($exifData['DateTimeOriginal'])) {
                $dateTimeOriginal = DateTime::createFromFormat('Y:m:d H:i:s', $exifData['DateTimeOriginal']);
                $originalDate = DBDatetime::create()->setValue($dateTimeOriginal->format('Y-m-d H:i:s'));
                $image->setField('OriginalDate', $originalDate->format('y-MM-dd'));
            }
        }
    }
}
Hide attachment content
andante

@fbunduka you can look at these examples here - instead of adding a field like these though, you’ll have to find the relevant tab and remove it. these should get you started though

wmk

well, FileAdmin uses this react stuff, afaik you need to modify the fields somewhere else @brasileric

👍 (2)
Tamara

I'm having difficulty with adding a new filetype to the allowedExtensions. I've added it to the .htaccess, and also to the $allowedExtensions in UploadField.php, but this file extension (.ODT) is still not allowed. Any thoughts?

Mo

Am I correct in thinking Member uses the default can permissions?

Mo

Member doesn't implement permissionProvider and SecurityAdmin only has permissions related to using SecurityAdmin

robbieaverill

as in "can the current member create a member" ?