sonata_type_model_list how to delete image after select - sonata-admin

Is it possible to delete a media after you have selected it (also in database)?

Many-to-One or One-to-One relations with sonata_type_model_list
->add('client', 'sonata_type_model_list', [
'btn_add' => $this->trans('admin.button.add_client'),
'btn_list' => $this->trans('admin.button.list'),
'btn_delete' => 'Delete button',
'btn_catalogue' => $this->translationDomain,
'label' => $this->trans('admin.label.client'),
'required' => true,
], [
'placeholder' => $this->trans('admin.placeholder.no_client'),
])
The line 'btn_delete' => 'delete button' gives you delete button
right of you one-to-one entity.
In this example I use an entity ClientCard which has one-to-one relation to Client entity.
List view.
By default you have delete button in list view.
Delete - is one of the batch actions. If you want to disable this action (and all other as well) you have to overwrite getBatchActions method
/**
* #return null
*/
public function getBatchActions()
{
return null;
}
Edit mode.
By default you have delete button in edit mode.
If you want completely disable the delete action, then you can overwrite configureRoutes method:
/**
* #param RouteCollection $collection
*/
protected function configureRoutes(RouteCollection $collection)
{
$collection->remove('delete');
}
Hope it will help.

Related

Form elements not being submitted

So I've got a weird problem that I'm having a hard time figuring out. I've got a simple form with a few elements that are not being submitted, all of these elements have only one thing in common, they're select elements:
echo $this->Form->control("spirit_type_id", [
"label" => false,
"type" => "select",
"options" => $spirit_types,
"empty" => "Spirit Type"
]);
echo $this->Form->control("country_id", [
"label" => false,
"type" => "select",
"options" => $countries,
"empty" => "Country"
]);
echo $this->Form->control("region_id", [
"label" => false,
"type" => "select",
"options" => $regions,
"empty" => "Region"
]);
And in my controller I have:
public function add() {
$spirit = $this->Spirits->newEntity();
$spirit_types = $this->Spirits->SpiritTypes->find("list");
$countries = $this->Spirits->Countries->find("list");
$regions = $this->Spirits->Regions->find("list");
if ($this->request->is("post")) {
debug($this->request->getData());
die();
$spirit = $this->Spirits->patchEntity($spirit, $this->request->getData());
$spirit->user_id = $this->Auth->user("id");
if ($this->Spirits->save($spirit)) {
$this->Flash->success("Your spirit was successfully saved.");
$this->redirect(["action" => "index"]);
} else {
$this->Flash->error("Your spirit could not be saved.");
}
}
$this->set(compact("spirit", "spirit_types", "countries", "regions"));
}
The important part is that debug statement. It shows this when I insert data using the form.
[
'name' => 'Longrow Peated',
'image' => 'imageLocation',
'brand' => 'Springbank',
'age' => '',
'cost' => '55'
]
Those are all text and/or number elements in my form, and they all come out just fine. It gets a little weirder though. I have validation in my table to require those id fields:
public function validationDefault(Validator $validator) {
$validator->requirePresence(
"name", "brand", "spirit_type_id", "country_id", "region_id", "age", "cost", "image"
)
->notEmpty("name", "We require a name")
->notEmpty("brand", "We require a brand or distillery")
->notEmpty("spirit_type_id", "We require a type of alchohol")
->notEmpty("country_id", "We require a country of origin")
But this doesn't ever seem to get triggered when I insert the data using patchEntity, it's only caught when I actually call the save function and I try inserting into the database.
If $this->request->getData() is not showing all of your fields, the most likely cause would be some sort of problem with your form; there are not a lot of ways for CakePHP to discard your data from here. You can narrow it down by using browser tools (built into most of them now) to inspect the data actually being sent from your browser in the page request.
If it turns out that the fields really aren't being sent across at all, the problem is almost certainly in your form. For example, you might be closing it early, or there might be HTML errors that confuse the browser. Make sure that all of your input tags are between the <form> and </form>, and if they are then try an HTML validator to check your code. There are lots of options online, and even the inspectors built into browsers can often help you spot these sorts of issues.
This is the most common problem:
If you check debug($this->request->getData()); before $spirit = $this->Spirits->newEntity(); you then see all submitted data!
Next go to Spirit Entity and double check if your fields "spirit_type_id,.." accessible!
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* #var array
*/
protected $_accessible = [
'*' => true, // quick fix
'id' => false,
];
or better way:
protected $_accessible = [
'spirit_type_id' => true,
'country_id' => true,
// etc ...
];
Edit
debug
$spirit = $this->Spirits->patchEntity($spirit, $this->request->getData());
debug($spirit); exit();
see if any errors.

CakePHP 3 hasMany update strange behaviour

I have JobsTable:
This is relation definition:
$this->hasMany( 'JobContracts', [
'foreignKey' => 'job_id'
] );
Saving code:
$entity = $this->patchEntity( $entity, $toSave, [
'fieldList' => ['notes],
'associated' => [
'JobContracts' => ['fieldList' => ['id', 'checked']]
]
] );
And now:
if I put this notes in fieldList then JobContracts are NOT saved properly.
If I remove fieldList, then I am able to save it properly.
Question is Why? I need to control base model fields also. Any suggestions?
Ive already checked: http://book.cakephp.org/3.0/en/orm/saving-data.html#avoiding-property-mass-assignment-attacks
You need to allow assigning the association property too, not only notes. If you don't, then the associated data is never going to be set on the resulting entity, and consequently is not going to be saved.
Check the docs that you've linked again, the tags example shows exactly that:
// Only allow changing the title and tags
// and the tag name is the only column that can be set
$entity = $this->patchEntity($entity, $data, [
'fieldList' => ['title', 'tags'],
'associated' => ['Tags' => ['fieldList' => ['name']]]
]);
$this->save($entity);
http://book.cakephp.org/3.0/en/orm/saving-data.html#avoiding-property-mass-assignment-attacks
So, add job_contracts to the field list, and you should be good.

hook_action_info does not create its VBO list item in view

I am trying to code my custom action using views and VBO.
The my view shows the user a list of commerce line items.
Here the code:
function nlmcode_action_info() {
return array(
'vbo_download_pdf' => array(
'type' => 'entity',
'label' => t('Download PDF'),
'configurable' => FALSE,
'triggers' => array('any')
),
);
}
function vbo_download_pdf($entity, $context) {
dpm("Do the magic here.");
}
I then can see the bulk operation available in the select field but once I have selected my custom 'Download PDF' option and added to the view, the item is missing in the VBO drop down list.
Solved.
Permissions to 'Download PDF' have to be set.

cakephp 3 bootstrap-ui change prev/next text

I have FriendsofCake Bootstrap-ui plugin. I see in the source that it accepts text for the pagination prev and next labels.
I am not sure how to exactly set the config option though.
PaginatorHelper.php
if (isset($options['next'])) {
if ($options['next'] === true) {
$options['next'] = $this->config('labels.next');
}
$options['after'] = $this->next($options['next'], ['escape' => false]) . $options['after'];
}
I was trying this below in the bootstrap.php but no effect
Configure::write('friendsofcake.PaginatorHelper.labels.prev', 'previous');
But I see they are also set in the __construct
Answer
With the help from drmonkeyninja here is the exact code needed to configure the labels in the AppView.php
$this->loadHelper(
'Paginator',
[
'className' => 'BootstrapUI.Paginator',
'labels' => [
'prev' => 'previous',
'next' => 'next',
]
]
);
This appears to be badly documented, but to configure any of the settings for a helper you need to pass them as an array when you load it. So for example, if you are loading the Paginator helper inside your AppView you would pass prevlike this:-
$this->loadHelper(
'Paginator',
[
'className' => 'BootstrapUI.Paginator',
'prev' => 'previous'
]
);

Checkbox filtering in Magento admin grid

I have a Slider model having some associated Images (models). When editing a Slider, there's a tab for its images. Now this can be modified, to select and deselect containing images. This, it seems, is done through an admin grid, having a checkbox column (the "values" key is for testing):
$this->addColumn('in_slider', array(
'header_css_class' => 'a-center',
'type' => 'checkbox',
'name' => 'in_slider',
'values' => array(1,2),
'align' => 'center',
'index' => 'slider_image_id'
));
Let's say the Slider in the above image has one image attached, and that works fine. But, when user selects "Any" and clicks "Search", all images must show, but those that are not associated should be deselected. I think this is standard behavior for such a feature.
What I don't get is the mechanism behind those two buttons: Reset Filter and Search. From what I've managed to dig up, a grid class has a getGridUrl() method which is used to refresh the grid data (via AJAX). That URL mapping to a controller.
// in grid class
public function getGridUrl()
{
return $this->getUrl('*/*/editGrid', array('_current' => true));
}
// Image controller
public function editGridAction()
{
$blockMarkup = $this->getLayout()->createBlock('module/someblock')->toHtml();
$this->getResponse()->setBody($blockMarkup);
}
But what about the "Search" button? How does that work?
How do other modules know when to show ALL entities, or searching for "Yes" or "No"?
When saving the slider, how can one grab the selected values? Working in Slider controller's saveAction I presume, but how do you get the checkbox values?
What you're looking for is a mass action. Add this function to your grid file (whatever extends Mage_Adminhtml_Block_Weidget_Grid):
protected function _prepareMassaction()
{
$this->setMassactionIdField('some_ID');
$this->getMassactionBlock()->setFormFieldName('element_name'); //html name of checkbox
$this->getMassactionBlock()->addItem('some_ID', array(
'label'=> __('Some Label'),
'url' => $this->getUrl('*/*/doSomething'), //an action defined in the controller
'selected' => 'selected',
'confirm' => __('Are you sure?')
));
return $this;
}
Here's a good blog post that helps to explain the concept:
http://inchoo.net/ecommerce/magento/how-to-add-massactions-to-magentos-grid/

Resources