Extending Blog Module on Silverstripe - php

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

Related

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();
}
}

laravel observers are not working

I am trying to listen to model events using laravel observers .The problem is when i submit my form (update or creating new records), nothing happened at all .Do i miss something ?
app.php
'providers' => [
...
App\Providers\CasesManagerServiceProvider::class,
]
CasesManagerServiceProvider.php
class CasesManagerServiceProvider extends ServiceProvider
{
public function boot( )
{
Cases::observe(CasesObserver::class);
}
public function register()
{
}
}
CasesObserver.php
class CasesObserver
{
private $cases;
public function __construct(Cases $cases){
$this->cases = $cases;
}
public function creating(Cases $case)
{
dd('creating');
}
public function saved(Cases $case)
{
dd('saved');
}
public function updating($case)
{
dd('updating');
}
public function updated($case)
{
dd('updated');
}
}
Cases.php
class Cases extends Model
{
const UPDATED_AT = 'modified_at';
protected $dispatchesEvents = [
'updating' => CasesObserver::class,
'updated' => CasesObserver::class,
'creating' => CasesObserver::class,
'saved' => CasesObserver::class,
];
}
for me, the problem was registering observer in the register() method!
so when I put it in the boot() method every thing worked well! the reason is the order of running methods in service providers which are mentioned hear
hope be useful
Ok i have found my answer . All the problem was when I added
use app\Observers\CasesObserver; in CasesManagerServiceProvider.php instead of use App\Observers\CasesObserver; .
Yes the Camel case of App was the problem, so i changed to App and all things are working fine now.
It seems to be a misuse of Composer and Laravel themselves.
You should inform them that you have added some files and configurations:
To autoload the files:
composer dump
To reconfigure the cache:
php artisan config:cache
Hope this help you too!
You do not need to use $dispatchesEvents in your case. You should try to remove $dispatchesEvents from model, and remove __constructor() from CasesObserver.
The reason is that you have to add a HasEvents trait to your model
<?php
use Illuminate\Database\Eloquent\Concerns\HasEvents;
class MyModel extends Model
{
use HasEvents;
//your code goes here
}
Not possible according to the documentation. When issuing a mass update or delete query via Eloquent.

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

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);
}
}
}

Create custom driver using Codeigniter

I'm trying to create my custom driver using Codeigniter
Files structure:
/libraries
/Test_driver
/drivers
Test_driver_first_driver.php
Test_driver.php
Driver super class:
class Test_driver extends CI_Driver_Library
{
function __construct()
{
$this->valid_drivers = array('test_driver_first_driver');
}
}
Driver Subclass :
class Test_driver_first_driver extends CI_Driver
{
function index()
{
echo "Hello world!";
}
}
Testing code in welcome.php Controller :
$this->load->driver('test_driver');
$this->test_driver->test_driver_first_driver->index();
but the output was : "Invalid driver requested Test_driver.test_driver_first_driver".
Does any one have any idea, Unfortunately Codeigniter user guide does not contains steps for creating custom driver.
its best practice or i should say my thinking that i always avoid underscores in parent class for the driver
so for me the file structure is some what like this
/libraries
/Testdriver
/drivers
Testdriver_first_driver.php
Testdriver.php
Testdriver.php
<?php
class Testdriver extends CI_Driver_Library
{
function __construct()
{
$this->valid_drivers = array('testdriver_first_driver');
}
}
Testdriver_first_driver.php
<?php
class Testdriver_first_driver extends CI_Driver
{
public function index()
{
echo "Hello world!";
}
}
In controller
$this->load->driver('testdriver');
$this->testdriver->first_driver->index();
Note : even if you don't use ucfirst() it will still work
i.e. Folder testdriver
Files -
testdriver.php (class testdriver extends CI_Driver_Library)
and
testdriver_first_driver.php (class testdriver_first_driver extends CI_Driver)
hope it is helpful. :)
I tried Karan's answer but I removed the parent's name in valid_drivers' value:
<?php
class Testdriver extends CI_Driver_Library{
function __construct(){
$this->valid_drivers = array('first_driver');
}
}
?>
This worked for me, you might want to give it a try. Credits to Karan.
I have just grappled with this in CodeIgniter v2.2.0 so thought I'd chip in. The scant documentation on custom drivers isn't too helpful as the example does not show the complete setup. The existing core CodeIgniter drivers are not organised in a consistent way either, with the driver parent class files being in different directory locations to where the docs say they should be etc. so you have little to go by but to consult the core Driver Library code.
In your given situation, the driver is seen as invalid is because you are effectively adding on the parent class name twice when calling it. This:
$this->test_driver->test_driver_first_driver->index();
Should be changed to:
$this->test_driver->first_driver->index();
Looking at the core code that the driver parent class extends:
class CI_Driver_Library {
protected $valid_drivers = array();
protected $lib_name;
// The first time a child is used it won't exist, so we instantiate it
// subsequents calls will go straight to the proper child.
function __get($child) {
if (!isset($this->lib_name)) {
$this->lib_name = get_class($this);
}
// The class will be prefixed with the parent lib
$child_class = $this->lib_name . '_' . $child;
Note the last line there. Basically, CI was trying to load a driver class named "Test_driver_test_driver_first_driver", which of course didn't exist.
For codeignaiter 3 problem in core system libraries driver.php
testdriver_first_driver.php
class Testdriver_first_driver extends CI_Driver {
public function index()
{
echo "Hello world!";
}
}
testdriver.php
class Testdriver extends CI_Driver_Library{
function __construct(){
$this->valid_drivers = array('first_driver');
}
}
CodeIgniter SPL Autoloader
/*
|--------------------------------------------------------------------------
| Autoloader function
|--------------------------------------------------------------------------
|
| Add to the bottom of your ./application/config/config.php file.
|
| #author Brendan Rehman
| #param $class_name
| #return void
*/
function __autoloader($class_name)
{
// class directories
$directories = array(
APPPATH . 'core/',
// add more autoloading folders here� and you�re done.
);
// for each directory
foreach ($directories as $directory)
{
// see if the file exsists
if (file_exists($directory.$class_name.'.php'))
{
require_once($directory.$class_name.'.php');
// only require the class once, so quit after to save effort (if
you got more, then name them something else
return;
}
}
}
spl_autoload_register('__autoloader');

Gas ORM - class not found

I was trying out Gas ORM, and have managed to auto-generate my models and now need to test them. However, I cannot seem to access the newly generated model.
I have the library autoloaded, and the config set up as:
config/gas.php
$config['models_path'] = array('GasModel' => APPPATH.'gas');
gas/useraccounts.php
<?php namespace GasModel;
/* This basic model has been auto-generated by the Gas ORM */
use \Gas\Core;
use \Gas\ORM;
class UserAccounts extends ORM {
public $primary_key = 'id';
function _init()
{
self::$fields = array(
'id' => ORM::field('auto[11]'),
...
);
}
}
controller/user.php
public function test() {
GasModel\UserAccounts::all()
}
Trying to access it however throws a fatal error:
PHP Fatal error: Class 'GasModel\UserAccounts' not found in applications/controllers/user.php on line 28
Can anyone help me in solving this issue?
Try add use of your model namespace or try add \ before GasModel
When I used back the same namespace as the example in http://gasorm-doc.taufanaditya.com/configuration.html, which is Model, it started working mysteriously. I would have preferred to use my custom namespace though..

Categories