2
votes

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

Trying to set values from another entity class on ManyToMany field. But it says the array must be type of Entity.

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

public function getNewInstance() {
    $instance = parent::getNewInstance();
    $mailadds = $this->getRequest()->query->get('mails');
    foreach ($mailadds as $mailadd) {
        $instance->addRecipient($mailadd);
    }

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

Note: recipients is ManyToMany field.

1
Add some code as well. Simply put - in the array where you're trying to get the 'translation_domain', that key does not exist. Check with isset prior to using it. - Janno
In what page is this error occurring? Try setting the error handling to E_ALL, maybe perhaps make a custom error handler with which you can get the specific file line where this is occurring. For example: error_reporting(E_ALL); set_error_handler("errorHandler", E_ALL); function errorHandler($errno, $errstr, $err_file, $err_line, $err_context) { echo "Error $errno, errstr - $errstr, err_file - $errfile, err_line - $err_line, context - $err_context); die(); } - Janno
That code I posted will print out all errors, it might bring you closer. - Janno
@Janno thanks for the answer :) Error occurs on 420th line of vendor/sonata-project/admin-bundle/Controller/CRUDController.php - Rozig
Change the line to: $translationDomain = isset($batchActions[$action]['translation_domain']) ? $batchActions[$action]['translation_domain'] : $this->admin->getTranslationDomain(); - Janno

1 Answers

2
votes

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.