Extending File: can't add field to Root.Main - php

I want to add a Tag field to all Files in a SilverStripe 3.3.1 site. I'm using Blog v2.4.0 and have upgraded tagfield to v1.2.1.
My FileExtension is configured to extend File:
class FileExtension extends DataExtension
{
private static $many_many = ['FileTags' => 'FileTag'];
public function updateCMSFields(FieldList $fields)
{
$tagField = TagField::create('FileTags', 'Tags', FileTag::get(), $this->owner->FileTags())
->setShouldLazyLoad(true)
->setCanCreate(true);
$fields->push($tagField);
}
}
The FileTag class is:
class FileTag extends DataObject
{
private static $db = ['Title' => 'Varchar(255)'];
private static $belongs_many_many = ['Files' => 'File'];
}
The extension hook I'm attaching to is here, and it provides a standard FieldList as described in the extension documentation.
The problem!
The field shows up correctly, but it's outside of the Root.Main tab and looks like this:
I've tried:
Using addFieldToTab('Root.Main', $field) - it results in [User Error] FieldList::addFieldToTab() Tried to add a tab to object 'FieldList' - 'Root' didn't exist.
As above, using Root and Main
Why doesn't this work the way it is supposed to?

The problem is Folder is a type of file, but Folder does not have a Root.Main tab in its CMS FieldList.
When adding fields to File we need to check that the File type is not Folder:
class FileExtension extends DataExtension {
public function updateCMSFields(FieldList $fields) {
if (!$this->owner instanceof Folder) {
$tagField = TagField::create('FileTags', 'Tags', FileTag::get(), $this->owner->FileTags())
->setShouldLazyLoad(true)
->setCanCreate(true);
$fields->addFieldToTab('Root.Main', $tagField);
}
}
}

Related

Extending Blog Module on Silverstripe

I'm about to extend Silverstripe Module. But I can't figure out what is missing on my simple code.
I need some suggestion to solve what is missing.
namespace {
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
class BlogPostExtension extends DataExtension {
private static $db = [
'Title' => 'Varchar'
];
public function updateCMSFields(FieldList $fields) {
// Add fields here
$fields->addFieldToTab("Root.Gallery", new TextField("Title","Title"));
}
}
}
This what I've added on app.yml. I also doing /dev/build?flush=all. But still nothing works.
SilverStripe\Blog\BlogPost:
extensions:
- Project\Extensions\BlogPostExtension
as wmk pointed out - Title is already part of the fields in the FieldList. If you rename the field to a different name. For example "GalleryTitle" and run dev/build you should have more success.
-- Peter

How to automaticly publish images in silverstripe dataextension

I was trying to add an uploadfield to a Custom DataExtension and got the Image field working. However the image i uploaded stays in concept mode, and i have to go to the File tab to publish it. I tried to use the code provided in the Silverstripe documentation but this only seems to work on regular pages. I found a question similar to mine:How to automaticaly publish files uploaded to a dataobject in Silverstripe model admin however this only seems to work on DataObjects.
This is my current code:
<?php
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\Storage\AssetStore;
use SilverStripe\Versioned\Versioned;
use SilverStripe\ORM\DataExtension;
class CustomSiteConfig extends DataExtension
{
private static $db = [
];
private static $has_one = [
'Logo' => Image::class
];
private static $owns = [
'Logo'
];
private static $extensions = [
Versioned::class,
];
private static $versioned_gridfield_extensions = true;
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldToTab("Root.Header", LiteralField::create("","<h1>Header</h1>"));
$fields->addFieldToTab("Root.Header", UploadField::create('Logo', 'Logo'));
}
}
Does anyone know a solution?
There's currently a bug that prevents "owned" records to be published if the owning dataobject is not versioned.
I think you're experience this bug, since SiteConfig is not versioned and thus won't publish owned files/images when it's being saved.
Until this bug has been resolved, you could use an onAfterWrite hook in your extension to publish the file:
public function onAfterWrite()
{
if ($this->owner->LogoID) {
$this->owner->Logo()->publishSingle();
}
}

Unable to upload files in SilverStripe 4

I've created a simple extension to SiteConfig to add a logo uploadfield to the settings section. However, the server returns an error upon uploading a file.
SiteConfig.php
namespace mymodule\siteconfig;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
use SilverStripe\AssetAdmin\Forms\UploadField;
class SiteConfig extends DataExtension {
private static $has_one = array(
'Logo' => 'SilverStripe\\Assets\\File'
);
public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root.Main', $logo = UploadField::create('Logo', 'Logo upload'));
}
}
Server's error log does not show anything relevant to this error. Whats going on here?
UPDATE
I just tried to upload a file to the Files section in the CMS, got the exact same error.
private static $has_one = [
'LogoImage' => Image::class,
];
public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root.Main', UploadField::create('LogoImage', 'Logo'));
}

My SilverStripe module overrides other modules display

I'm writing a subscribe module to plugin to the SilverStripe Blog module. So far I have my yml as:
---
Name: subscription
After: 'framework/*','cms/*'
---
Blog:
extensions:
- Subscription
Page_Controller:
extensions:
- SubscriptionWidget
And my SubscriptionWidget.php:
<?php
class SubscriptionWidget extends DataExtension {
public function SubscriptionWidget() {
$controller = SubscriptionWidget_Controller::create();
$form = $controller->SubscriptionWidget();
return $form;
}
}
class SubscriptionWidget_Controller extends Controller {
private static $allowed_actions = array('SubscriptionWidget');
public function SubscriptionWidget () {
$form = Form::create(
$this,
__FUNCTION__,
FieldList::create(
TextField::create('Email', 'Email'),
TextField::create('Name', 'Name')
),
FieldList::create(
FormAction::create('submit', 'Subscribe')
)
);//->setTemplate('SubscriptionWidget');
return $form;
}
public function submit($data, $form) {
return $this->redirect('/subscribed');
}
}
At the moment this works as intended however another plugin I use called BetterNavigator disappears from the screen. If I take out
Page_Controller:
extensions:
- SubscriptionWidget
from my yml it reappears. I've looked through both code bases which are quite simple and there are no conflicting functions. I've also tried using ContentController instead of Page_Controller and my template disappears until I disable BetterNavigator and then it reappears. I do have one or two pretty empty classes but all are called some variation of Subscriber while there is only one function in BetterNavigator called BetterNavigator.
Why would this be happening?
I see only one clash in your code that results in incorrect runtime behaviour. Your method SubscriptionWidget::SubscriptionWidget() is treated as legacy class constructor. So I suggest you thinking about better class and method names.
class SubscriptionWidget extends Extension
{
// explicitly defined constructor
public function __construct() {
parent::__construct();
}
// now this one is normal function
public function SubscriptionWidget() {
$controller = SubscriptionWidget_Controller::create();
$form = $controller->SubscriptionWidget();
return $form;
}
}

SilverStripe MultiForm not working

I installed and configured SilverStripe on my server. I installed the MultiForm module and followed the instructions in the module documentation.
After following the instructions I still don't see any new page type in my CMS Portal.
I also tried db/build?flush=1 & dev/build?flush=1 but it doesn't make a difference.
I Created the Following files in mysite/code/ directory
SponsorSignupForms.php
class SponsorSignupForms extends MultiForm{
protected static $start_step = 'CompanyDetailsStep';
}
CompanyDetailsStep.php
class CompanyDetailsStep extends MultiFormStep{
public static $next_steps = 'ContactDetailsStep';
function getFields()
{
$fields = singleton('Member')->getFrontendFields();
return $fields;
}
function getValidator()
{
return new Member_Validator('FirstName', 'Surname', 'Email', 'Password');
}
}
ContactDetailsStep.php
class ContactDetailsStep extends MultiFormStep{
public static $is_final_step = true;
function getFields()
{
$fields = singleton('Reference')->getFrontendFields();
return $fields;
}
}
How do I get these custom MultiForms working and appearing as creatable pages?
Of course you don't see any new page type in the list of available pages, you will only see subclasses of SiteTree there, MultiFormStep is "just" a subclass of DataObject.
You can plug your form to every page you want manually, but you also can create a new page type for your form and include the Form in your Controller and Template, see readme of MultiForm:
class MyFormPage extends Page
{
}
class MyFormPageController extends Page_Controller
{
//
private static $allowed_actions = array(
'SponsorSignupForms',
'finished'
);
public function SponsorSignupForms() {
return new SponsorSignupForms($this, 'Form');
}
public function finished() {
return array(
'Title' => 'Thank you for your submission',
'Content' => '<p>You have successfully submitted the form!</p>'
);
}
}
In the template just include the form:
<% if $SponsorSignupForms %>
$SponsorSignupForms
<% end_if %>
and you should see the form now.

Categories