Rozig Rozig - 4 months ago 132
PHP Question

Creating batch action on sonata admin

I'm trying to add new batch action on Sonata admin. But when i implement example on Sonata-Project to my project, it says:


Notice: Undefined index: translation_domain


How to solve it?

Some code pieces:

AdminClass.php

<?php
use Sonata\AdminBundle\Admin\Admin;

class AdminClass extends Admin {
//Other variables, functions
public function getBatchActions() {
// retrieve the default batch actions (currently only delete)
$actions = parent::getBatchActions();
if (
$this->hasRoute('edit') && $this->isGranted('EDIT') &&
$this->hasRoute('delete') && $this->isGranted('DELETE')
) {
$actions['merge'] = array(
'label' => 'Мэйл илгээх',
'ask_confirmation' => true
);

}

return $actions;
}

/*protected function configureRoutes(RouteCollection $collection) {
$collection->add('sendAction');
$collection->add('send-mail', $this->getRouterIdParameter().'/send-mail');
}*/
}


CRUDController.php

<?php

use Sonata\AdminBundle\Controller\CRUDController as BaseController;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class CRUDController extends BaseController
{
/**
* @param ProxyQueryInterface $selectedModelQuery
* @param Request $request
*
* @return RedirectResponse
*/
public function batchActionMerge(ProxyQueryInterface $selectedModelQuery)
{
if ($this->admin->isGranted('EDIT') === false || $this->admin->isGranted('DELETE') === false)
{
throw new AccessDeniedException();
}

$request = $this->get('request');
$modelManager = $this->admin->getModelManager();

$target = $modelManager->find($this->admin->getClass(), $request->get('targetId'));

if( $target === null){
$this->get('session')->setFlash('sonata_flash_info', 'flash_batch_merge_no_target');

return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
}

$selectedModels = $selectedModelQuery->execute();

// do the merge work here

try {
foreach ($selectedModels as $selectedModel) {
$modelManager->delete($selectedModel);
}

$modelManager->update($selectedModel);
} catch (\Exception $e) {
$this->get('session')->setFlash('sonata_flash_error', 'flash_batch_merge_error');

return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
}

$this->get('session')->setFlash('sonata_flash_success', 'flash_batch_merge_success');

return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
}
}


EDIT



CRUDController.php

public function batchActionSend(ProxyQueryInterface $selectedModelQuery) {
...
$request = $this->get('request');
$modelManager = $this->admin->getModelManager();
$selectedModels = $selectedModelQuery->execute();

return new RedirectResponse($this->container->get('router')->generate('route_name', array('mails' => $selectedModels)));
}


AdminClass.php

protected function configureFormFields(FormMapper $formMapper) {
$mailadds = $this->getRequest()->query->get('mails');
$formMapper
->add('recipients', 'sonata_type_model', array('label' => 'Хүлээн авагчид', 'multiple' => true, 'by_reference' => false, 'data' => $mailadds), array( ))
....
;
}

Answer

As per my comment, issue should be solved by the following code on line 420:

$translationDomain = isset($batchActions[$action]['translation_domain']) ? $batchActions[$action]['translation_domain'] : $this->admin->getTranslationDomain();

Issue happened because you weren't checking if the key 'translation_domain' existed in array $batchActions[$action]. As far as I know, the ternary operator '?' tries to apply the value to the variable, if the value didn't get set after the operation, it tries the second operator. Hence, why I think the isset should fix it.

Comments