Search code examples
sonata-adminsymfony-sonatasonatasonata-media-bundle

sonata_type_model_list how to delete image after select


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


Solution

    1. 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'),
                  ])
      

      enter image description here

      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.

    2. List view. By default you have delete button in list view. enter image description here 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;
      }
      
    3. Edit mode. By default you have delete button in edit mode. enter image description here 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.