I have added the following code in my Admin class,
class ProductPriceAdmin extends Admin
{
protected function configureRoutes(RouteCollection $collection)
{
parent::configureRoutes($collection);
$collection->add('price'); //I want to add a "id" as route parameter
}
}
Here price is my custom function which I have declared in the controller as below,
class ProductPriceController extends Controller
{
public function priceAction($id) //I want to use this variable
{
if (false === $this->admin->isGranted('LIST')) {
throw new AccessDeniedException();
}
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
return $this->render($this->admin->getTemplate('list'), array(
'action' => 'list',
'form' => $formView,
'datagrid' => $datagrid,
'csrf_token' => $this->getCsrfToken('sonata.batch'),
));
}
}
How to add route parameter when adding dynamic routing ?
Thanks,
Faisal Nasir
Try this:
$collection->add('edit_price', 'price/{id}');
also you can view in Symfony\Component\Routing\Route\RouteCollection method add().
Related
I have a REST API and have an Entity Userwith field called Avatar, in DB I save name XXXX.jpg but when I return I want to add a url in this field Avatar, for example www.mylink.com/XXXX.jpg.
I'm trying with a service implements SubscribingHandlerInterfacebut I don't know how I can use it.
I have this method in this service:
class UrlManager implements SubscribingHandlerInterface
{
public static function getSubscribingMethods()
{
return array(
array(
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => 'AppBundle/Entity/User',
'method' => 'serializeUrlAvatar',
),
);
}
public function serializeUrlAvatar(User $user)
{
$url = 'www.mylink.com';
return array(
"avatar" => $url . $user->getAvatar()
);
}
}
but how can I call this service to modify url when I serialize.
Now I do this:
$_format = 'json';
$json = $this->get('jms_serializer')->serialize($user, $_format);
return new Response($json, 200, ['Content-Type' => 'application/' . $_format]);
In service.yml:
app.url_converter_service:
class: AppBundle\Service\UrlManager
tags:
- { name: jms_serializer.subscribing_handler }
Update
In my controller I call this function like this:
$result = $this->get('app.url_converter_service')->serializeUrlAvatar($user);
$json = $this->get('jms_serializer')->serialize($result, $_format);
return new Response($json, 200, ['Content-Type' => 'application/' . $_format]);
So my question is, exists a way to remove the first line and serialize correctly (add the url) when I serialize?
Have you registered your service like this?
# app/config/services.yml
avatar_url_handler:
class: YourBundle\Serializer\Handler\AvatarUrlHandler
tags:
- { name: jms_serializer.subscribing_handler }
I found a solution. I create a service which implements EventSubscriberInterface like this:
class UserSerializeHandler implements EventSubscriberInterface
{
private $user_uploads;
public function __construct($user_uploads){
$this->user_uploads = $user_uploads;
}
public static function getSubscribedEvents()
{
return array(
array(
'event' => 'serializer.pre_serialize',
'class' => User::class,
'method' => 'onPreSerializeUser'
));
}
public function onPreSerializeUser(PreSerializeEvent $event)
{
/** #var User $user */
$user = $event->getObject();
$avatar = $user->getAvatar();
$user->setAvatar($this->user_uploads . "/" . $avatar);
}
}
In service.yml:
app.serializer_user_service:
class: AppBundle\Service\UserSerializeHandler
arguments: ['%user_uploads%']
tags:
- { name: jms_serializer.event_subscriber }
I have user_uploads in parameters.yml like this:
user_uploads: 'https://myUrl.com'
And in any Controller that I serialize a User, I add the url in the Avatar paramter.
$json = $this->get('jms_serializer')->serialize($user, $_format);
return new Response($json, 200, ['Content-Type' => 'application/' . $_format]);
I am trying to register user from index page but When validator failed then want to redirect to register page.
I am tired to solve this problem . can't customize Illuminate/Foundation/Validation/ValidatesRequests.php page.
Here is the code
protected function getRedirectUrl() {
return route('register');
}
protected function validator(array $data) {
$this->getRedirectUrl();
return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]);
}
add the below method which generate the previous url in your controller and override the default one add following methods in your controller
in your controller where you have defined $this->validate call define below method and use Request
use Illuminate\Http\Request; // add at the top
protected function getRedirectUrl() {
return route('register');
}
protected function validator(array $data) {
return $this->validate(request(), [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]);
}
public function register(Request $request)
{
$this->validator($request->all());
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
I'm using Media in User Entity(avatar).
At first I used sonata_media_type. It worked good.
The problem is I'm using ManyToOne - so the admin can select one from the list. To achieve this I have to use sonata_type_model_list - this has list,new,delete actions. I removed delete by 'btn_delete' => ''. Here the list action works good(up to now).
The real PROBLEM is at new action. The new action window load from ajax - and it has File/Reference, Category (Two fields).
Here I need to remove Category field entirely(list,new,delete). Why do we need this? Because it is useless!.
LIST - only display the 'context' => 'profile' from
'link_parameters'. So here the LIST action is useless.
NEW - New action can create new context, but it will not display in the
LIST(right now). So I don't need this. If I need I'll create from
ClassificationBundle.
DELETE - Delete action has no effect(right now - here).
MY-RESEARCH:
I tried to modify the TEMPLATE - but I can't find the correct twig file. It points to parent() - which is pointing to admin bundle!
To validation File/Reference - I created my own ImageProvider(as per doc) - It works(validate) good.
I tried to remove Category field(check image) - but failed.
My code is:
class ImageProvider extends BaseProvider{...}
public function buildCreateForm(FormMapper $formMapper) {
// This works - changed File/Reference to ok
$formMapper->add('binaryContent', 'file', array('label' => 'ok',
'constraints' => array(
new NotBlank(),
),
));
// This works - added a new text field
$formMapper->add('context', 'text', ['attr' => ['class' => 'fz_rocks']]);
// This not working - also ->add('category') - has no effect even when attr=hide and so on..
$formMapper->remove('category');
}
-
To remove category field from media popup
You need to override media admin's class by overriding class parameter sonata.media.admin.media.class
Create you own admin class and extend it with sonata media's base admin class.
Override configureFormFields() method by defining in your admin class
Remove category field from $formMapper
Override Sonata media class
parameters:
sonata.media.admin.media.class: Your\MediaBundle\Admin\ORM\MediaAdmin
Media Admin Class
namespace Your\MediaBundle\Admin\ORM;
use Sonata\MediaBundle\Admin\ORM\MediaAdmin as Admin;
// .. Other use statements
class MediaAdmin extends Admin {
/**
* {#inheritdoc}
*/
protected function configureFormFields( FormMapper $formMapper ) {
$media = $this->getSubject();
if ( ! $media ) {
$media = $this->getNewInstance();
}
if ( ! $media || ! $media->getProviderName() ) {
return;
}
$formMapper->add( 'providerName', 'hidden' );
$formMapper->getFormBuilder()->addModelTransformer( new ProviderDataTransformer( $this->pool, $this->getClass() ), true );
$provider = $this->pool->getProvider( $media->getProviderName() );
if ( $media->getId() ) {
$provider->buildEditForm( $formMapper );
} else {
$provider->buildCreateForm( $formMapper );
}
}
}
I solved by hiding the categoy field. If i removed completely it cause problem sometimes. Safe is to hide.
To achieve this i use custom providers, as per sonata-media doc creating_a_provider_class.rst
namespace Application\Sonata\MediaBundle\Provider;
class ImageProvider extends BaseProvider {
public function buildCreateForm(FormMapper $formMapper) {
$formMapper->add('binaryContent', 'file', ['label' => 'Upload a new file', 'constraints' => [new NotBlank(), new NotNull()]])->end();
$formMapper->with('General', ['class' => 'hidden'])->add('category');
}
public function buildEditForm(FormMapper $formMapper) {
parent::buildEditForm($formMapper);
$formMapper->add('binaryContent', 'file', ['label' => 'Upload a new file', 'required' => FALSE])->end();
$formMapper->with('General', ['class' => 'hidden'])->add('category');
}
}
How to apply some code to each entity being displayed in Admin's list view?
For example, if I have a TagManager and need to load tags for each entity being displayed, how do I do that? Is there a method to override in entity's Admin or can I bind to some list form event? I could not find a place to do that.
I don't wan't to bind to entity's onLoad event.
EDIT: In your entityAdminController :
public function listAction()
{
if (false === $this->admin->isGranted('LIST')) {
throw new AccessDeniedException();
}
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
foreach($datagrid->getResults() as $object)
{
//do what you want with $object
}
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
return $this->render($this->admin->getTemplate('list'), array(
'action' => 'list',
'form' => $formView,
'datagrid' => $datagrid
));
}
How to limit the number of embedded form with the type "sonata_type_collection" ?
$formMapper->add('phones', 'sonata_type_collection',
array(
'required' => true,
'by_reference' => false,
'label' => 'Phones',
),
array(
'edit' => 'inline',
'inline' => 'table'
)
I would like limit to last five phones, I found only this solution for now, limit the display in the template twig "edit_orm_one_to_many", but i don't like that.
I found a solution by rewriting the edit action in the controller,
such in the documentation sonataAdminBundle I created my admin controller class:
class ContactAdminController extends Controller
{
public function editAction($id = null)
{
// the key used to lookup the template
$templateKey = 'edit';
$em = $this->getDoctrine()->getEntityManager();
$id = $this->get('request')->get($this->admin->getIdParameter());
// $object = $this->admin->getObject($id);
// My custom method to reduce the queries number
$object = $em->getRepository('GestionBundle:Contact')->findOneAllJoin($id);
if (!$object)
{
throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
}
if (false === $this->admin->isGranted('EDIT', $object))
{
throw new AccessDeniedException();
}
$this->admin->setSubject($object);
/** #var $form \Symfony\Component\Form\Form */
$form = $this->admin->getForm();
$form->setData($object);
// Trick is here ###############################################
// Method to find the X last phones for this Contact (x = limit)
// And set the data in form
$phones = $em->getRepository('GestionBundle:Phone')->findLastByContact($object, 5);
$form['phones']->setData($phones);
// #############################################################
if ($this->get('request')->getMethod() == 'POST')
{
$form->bindRequest($this->get('request'));
$isFormValid = $form->isValid();
// persist if the form was valid and if in preview mode the preview was approved
if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved()))
{
$this->admin->update($object);
$this->get('session')->setFlash('sonata_flash_success', 'flash_edit_success');
if ($this->isXmlHttpRequest())
{
return $this->renderJson(array(
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object)
));
}
// redirect to edit mode
return $this->redirectTo($object);
}
// show an error message if the form failed validation
if (!$isFormValid)
{
$this->get('session')->setFlash('sonata_flash_error', 'flash_edit_error');
}
elseif ($this->isPreviewRequested())
{
// enable the preview template if the form was valid and preview was requested
$templateKey = 'preview';
}
}
$view = $form->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
return $this->render($this->admin->getTemplate($templateKey), array(
'action' => 'edit',
'form' => $view,
'object' => $object,
));
}
}