Silverstripe 3 -Multiple Gridfields on one Page - silverstripe

i want to add multiple gridfields to one pagetype.
At the moment I'm doing it like this
$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator(10),
new GridFieldEditButton(),
new GridFieldDeleteAction(),
new GridFieldDetailForm()
);
$sliderField = new GridField('Slides', 'Slider', $this->Slides(), $gridFieldConfig);
$fields->addFieldToTab('Root.Slider', $sliderField);
$categoryField = new GridField('ShopCategories', 'Kategorien', $this->ShopCategories(), $gridFieldConfig);
$fields->addFieldToTab('Root.Shop Kategorien', $categoryField);
It works but the problem is that i got the same "add blablabla object" title for both.
How can i fix this without using multiple gridFieldConfigs?
Thx in Advance

It seems that if you use the same config, the details are shared by both. However, there are a few default configs set-up you could use instead.
GridFieldConfig_RelationEditor
GridFieldConfig_RecordEditor
GridFieldConfig_RecordViewer
GridFieldConfig_Base
All of which extend the gridfield config.
So you could do this, for instance:
$sliderField = new GridField(
'Slides',
'Slider',
$this->Slides(),
GridFieldConfig_RelationEditor::create()
);
If you wish to create your own custom config you can write a class that extends GridFieldConfig in the same manner:
class GridFieldConfig_Custom extends GridFieldConfig {
/**
*
* #param int $itemsPerPage - How many items per page should show up
*/
public function __construct($itemsPerPage=null) {
$this->addComponent(new GridFieldToolbarHeader());
$this->addComponent(new GridFieldAddNewButton('toolbar-header-right'));
$this->addComponent(new GridFieldSortableHeader());
$this->addComponent(new GridFieldDataColumns());
$this->addComponent(new GridFieldPaginator(10));
$this->addComponent(new GridFieldEditButton());
$this->addComponent(new GridFieldDeleteAction());
$this->addComponent(new GridFieldDetailForm());
}
}
And then:
$sliderField = new GridField(
'Slides',
'Slider',
$this->Slides(),
GridFieldConfig_Custom::create());
$categoryField = new GridField(
'ShopCategories',
'Kategorien',
$this->ShopCategories(),
GridFieldConfig_Custom::create());

the problem is that the config object is tied to the gridfield as soon as you add it.
that means that you need 2 config objects, currently you only have one.
you can either create a second one, or clone the first one:
$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator(10),
new GridFieldEditButton(),
new GridFieldDeleteAction(),
new GridFieldDetailForm()
);
$gridFieldConfig2 = clone $gridFieldConfig;
$sliderField = new GridField('Slides', 'Slider', $this->Slides(), $gridFieldConfig);
$fields->addFieldToTab('Root.Slider', $sliderField);
$categoryField = new GridField('ShopCategories', 'Kategorien', $this->ShopCategories(), $gridFieldConfig2);
$fields->addFieldToTab('Root.Shop Kategorien', $categoryField);

Related

Upload photo without form (API)

I'm making an app mobile and I need to upload a photo from my camera via my API. But when doctrine try to INSERT INTO, he isn't happy because my name field is null. So the name generate by my entity is missing.
I don't understand where the code block. My web app uses already the same entity photo and everything works fine.
My Api controller:
<?php
/**
* #throws AccessDeniedException
* #return array
* #FOSRest\View()
*/
public function apiUploadAction()
{
$photo = new Photo;
$photo->setUser($this->getUser());
$photo->setFile = new UploadedFile(
$_FILES["file"]["tmp_name"],
$_FILES["file"]["name"],
$_FILES["file"]["type"],
$_FILES["file"]["size"],
$_FILES["file"]["error"],
$test = false
);
$em = $this->getDoctrine()->getManager();
$em->persist($photo);
$em->flush();
$apiResponse = array(
"code" => true,
"style" => "success",
/*********** SCREENSHOT COMES FROM HERE ***********/
"message" => $_FILES["file"]["tmp_name"]." ".$_FILES["file"]["name"]." ".$_FILES["file"]["type"]." ".$_FILES["file"]["size"]." ".$_FILES["file"]["error"],
);
$view = View::create();
$view->setFormat('json');
$view->setData($apiResponse);
return $this->get('fos_rest.view_handler')->handle($view);
}
The $apiResponse.message (screenshot):
As you can see symfony has the image so the problem don't come from my app mobile. new UploadedFile(); is the right way ? To upload without form.
Here is the solution :
Replace :
$photo->setFile = new UploadedFile(
$_FILES["file"]["tmp_name"],
$_FILES["file"]["name"],
$_FILES["file"]["type"],
$_FILES["file"]["size"],
$_FILES["file"]["error"],
$test = false
);
by
$photo->setFile($this->getRequest()->files->get('file'));

Drupal domain access auto create content

I am using Drupal's domain access module to create micro sites in a white label system. Is there a way of automatically creating nodes with default content for each site?
Use hook_domain_insert() this will give you the id of the domain that has just been created in the $domain array. Then you can create a node as follows:
function YOUR_MODULE_domain_insert($domain, $form_values = array()) {
$domain_id = $domain['domain_id'];
$node = new stdClass();
$node->type = 'your_type';
$node->title = 'Your Title';
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->domain_site = 0;
$node->domains[$domain_id] = $domain_id;
$node = node_submit($node);
node_save($node);
}

SilverStripe GridField: too many versions of DataObject get created

TL;DR When creating/saving a versioned DataObject with relation to some page, two entries are created in the corresponding versions table (instead of one).
I'm trying to version some DataObject and have the Versioned extension applied as follows:
class Testobject extends DataObject {
static $has_one = array(
'Page' => 'Page'
);
static $extensions = array(
"Versioned('Stage', 'Live')",
);
Testobjects are being managed in a GridField on some page like so:
class PageContent extends Page {
public static $has_many = array(
"Testobjects" => "TestObject"
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$config = GridFieldConfig_RelationEditor::create();
$gridField = new GridField(
'Testobjects',
'Testobject',
$this->Testobjects(),
$config);
$fields->addFieldToTab('Root.Main', $gridField);
}
Now, whenever i add or save some Testobject in the GridField's EditForm, two new entries show up in the Testobject_versions table. For comparsion, when i save a page in the SiteTree, only one entry in the corresponding versions table is created.
As there will we thousands of these DataObjects on a page, i'm worried about this duplication filling up my database. Is there a way to get around this?
Further recognitions:
On creation of a new Testobject, the first entry in the versions table has it's PageID field set to 0, the second entry has set the actual PageID of the corresponding page.
If I replace $this->Testobjects() in the GridField construction by Testobject::get(), only one entry shows up in the versions table.
onBeforeWrite is called twice when using $this->Testobjects()
So it seems setting the relation to the page happens after a first 'write()', then another 'write()' is called. But where in the code does this happen?
If you're editing your page/testobject in the main section of the CMS (# '/admin/pages'), you can try this somewhat hackish trick in your TestObject class
public function getCMSFields(){
$fields = parent::getCMSFields();
$fields->push( new HiddenField('PageID','PageID', Controller::curr()->CurrentPageID());
return $fields;
}
This is not ideal for the following reasons:
hard to test with unit test controller
awareness of the controller in the model is bad
IMHO
But it can be a reasonable fix if it works for you

Elastica search add a second filter

I'm starting using Elastica with my Symfony app but I'm stuck to add a filter.
The following code works well, it search by name, slug, agglomeration and then add a geoloc filter. I want to add a "specialty" filter to remove all results which do not correspond to that filter/ have that specialty but I've no idea how to do that.
$nameQuery = new \Elastica_Query_Text();
$nameQuery->setFieldQuery('name', $name);
$slugQuery = new \Elastica_Query_Text();
$slugQuery->setFieldQuery('slug', $name);
$agglomerationQuery = new \Elastica_Query_Text();
$agglomerationQuery->setFieldQuery('agglomeration', $agglomeration);
$boolQuery = new \Elastica_Query_Bool();
$boolQuery->addShould($nameQuery);
$boolQuery->addShould($slugQuery);
$boolQuery->addShould($agglomerationQuery);
//todo add filter by speciality
if($latitude != null) {
$geoFilter = new \Elastica_Filter_GeoDistance('location', $latitude, $longitude, '3km');
$boolQuery = new \Elastica_Query_Filtered($boolQuery, $geoFilter);
}
return $this->find($boolQuery);

Silverstipe tutorial - addFieldToTab does not work

I am following silverstripe tutorial number 2: Extending a basic site http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site
I have downloaded ss3.0.3 and using Windows 7 WAMP 2.1
I have created the ArticlePage and ArticleHolderPage but when I go and create a page of type ArticleHolder there are no date and author fields in the content tab.
My code for the ArticlPage:
class ArticlePage extends Page
{
static $db = array(
'Date'=>'Date',
'Author'=>'Text'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$dateField = new DateField('Date');
$dateField = setConfig('showcalendar', true);
$fields->addFieldToTab('Root.Main', $dateField, 'Content');
$fields->addFieldToTab('Root.Main', new TextField('Author'), 'Content');
return $fields;
}
}
Am I doing something wrong?
Thank you
oh, i was too quick with my comment above, just found an error in your code:
$dateField = setConfig('showcalendar', true);
should read
$dateField->setConfig('showcalendar', true);

Resources