简体   繁体   中英

Sonata Admin Form Tabs

I am using Sonata Admin on my application and would like to disable some tabs when creating or editing the entity depending on user rights. So far I've tried adding a css class on the tab but it seems to be ignored when rendered. Here's my code:

protected function configureFormFields(FormMapper $formMapper)
{
  $securityContext = $this->getConfigurationPool()->getContainer()->get('security.authorization_checker');
  $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
  $arr = $em->getRepository('AppBundle:Staff')->findByDesignation(9);
    $formMapper
      ->tab('1. Service Start Section')
        ->with('Brief Information')
          ->add('dateOfReceipt','sonata_type_datetime_picker', array('format'=>'dd/MM/yyyy HH:mm', 'dp_side_by_side' => false,'dp_use_current' => true))
          ->add('receiptMode','choice',['choices' => ['Email' => 'Email', 'Letter' => 'Letter', 'Orally / Meeting' => 'Orally / Meeting', 'Phone' => 'Phone']])
          ->add('client', 'sonata_type_model_list', array('btn_delete' => false))
          ->add('natureOfTheBrief',CKEditorType::class, array(
            'config' => array(
                'uiColor' => '#ffffff',
                //...
            )))
          ->add('forwardTo',StaffType::class,['label' => 'Forward To', 'choices' => $arr,'choice_value' => 'id'])
        ->end()
      ->end();
      $disabled = $securityContext->isGranted('ROLE_PROFESSIONAL_SERVICES_MANAGER') ? '' : 'tab-disabled';
      $formMapper
    ->tab('2. Service Commencement Section',array('class' => $disabled))
      ->with('Brief Analysis')
         .....
         .....
      ->end()
    -end();

Just don't add the tab at all when the user does not have the proper rights:

protected function configureFormFields(FormMapper $formMapper)
{
  $securityContext = $this->getConfigurationPool()->getContainer()->get('security.authorization_checker');
  $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
  $arr = $em->getRepository('AppBundle:Staff')->findByDesignation(9);
    $formMapper
      ->tab('1. Service Start Section')
        ->with('Brief Information')
          ->add('dateOfReceipt','sonata_type_datetime_picker', array('format'=>'dd/MM/yyyy HH:mm', 'dp_side_by_side' => false,'dp_use_current' => true))
          ->add('receiptMode','choice',['choices' => ['Email' => 'Email', 'Letter' => 'Letter', 'Orally / Meeting' => 'Orally / Meeting', 'Phone' => 'Phone']])
          ->add('client', 'sonata_type_model_list', array('btn_delete' => false))
          ->add('natureOfTheBrief',CKEditorType::class, array(
            'config' => array(
                'uiColor' => '#ffffff',
                //...
            )))
          ->add('forwardTo',StaffType::class,['label' => 'Forward To', 'choices' => $arr,'choice_value' => 'id'])
        ->end()
      ->end();

    if($securityContext->isGranted('ROLE_PROFESSIONAL_SERVICES_MANAGER')){
      $formMapper
          ->tab('2. Service Commencement Section',array('class' => $disabled))
             ->with('Brief Analysis')
           .....
           .....
          ->end()
       ->end();
}

Try this:

$tabs = $this->getFormTabs();

unset($tabs['2. Service Commencement Section']);

$this->setFormTabs($tabs);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM