【问题标题】:In EasyAdmin 3 configureActions method how do I get the current entity?在 EasyAdmin 3 configureActions 方法中如何获取当前实体?
【发布时间】:2021-03-10 16:23:58
【问题描述】:

我希望使用预定义的过滤器将一个动作添加到另一个索引动作中。

要构建过滤器,我需要在 configureActions 方法中获取当前实体。

    public function configureActions(Actions $actions): Actions
    {
        parent::configureActions($actions);

        $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
        $url = $adminUrlGenerator
            ->setController(SiteCrudController::class)
            ->setAction(Action::INDEX)
            ->set('filters', [
                'agent' => [
                    'comparison' => '=',
                    'value' => 2194, // How to get current entity here??
                ]
            ])
            ->generateUrl()
        ;

        $viewRelatesSites = Action::new('viewRelatedSites', 'Sites', 'fa fa-file-invoice')
            ->linkToUrl($url)
        ;

        $actions->add(Action::DETAIL, $viewRelatesSites);
        $actions->add(Action::EDIT, $viewRelatesSites);

        return $actions;
    }
}

我怎样才能在这里获得实体?

【问题讨论】:

    标签: easyadmin


    【解决方案1】:

    要获取当前实体,需要 AdminContext。

    使用实体集获取此信息的最佳位置是在 BeforeCrudActionEvent 中。

    final class AgentCrudActionEventListen implements EventSubscriberInterface
    {
        private AdminUrlGenerator $adminUrlGenerator;
    
        private AdminContextProvider $adminContextProvider;
    
        public function __construct(AdminUrlGenerator $adminUrlGenerator, AdminContextProvider $adminContextProvider)
        {
            $this->adminUrlGenerator = $adminUrlGenerator;
            $this->adminContextProvider = $adminContextProvider;
        }
    
        public static function getSubscribedEvents(): array
        {
            return [
                BeforeCrudActionEvent::class => 'onBeforeCrudActionEvent',
            ];
        }
    
        public function onBeforeCrudActionEvent(BeforeCrudActionEvent $event): void
        {
            $crud = $event->getAdminContext()->getCrud();
    
            if ($crud->getControllerFqcn() !== AgentCrudController::class) {
                return;
            }
    
            $entity = $this->adminContextProvider->getContext()->getEntity();
            if (!$entity) {
                return;
            }
    
            $url = $this->adminUrlGenerator
                ->setController(SiteCrudController::class)
                ->setAction(Action::INDEX)
                ->set('filters', [
                    'agent' => [
                        'comparison' => '=',
                        'value' => $entity->getPrimaryKeyValue(),
                    ]
                ])
                ->generateUrl()
            ;
    
            $viewRelatedSites = Action::new('viewRelatedSites', 'Sites', 'fa fa-file-invoice')
                ->linkToUrl($url)
            ;
    
            $actions = $crud->getActionsConfig();
    
            $actions->appendAction(Action::DETAIL, $viewRelatedSites->getAsDto());
            $actions->appendAction(Action::EDIT, $viewRelatedSites->getAsDto());
        }
    }
    
    

    【讨论】:

      【解决方案2】:

      由于不鼓励使用 EasyAdmin 3 事件per the docs

      从 EasyAdmin 3.0 开始,一切都是用 PHP 定义的。这就是为什么自定义后端行为重载 PHP 类和方法以及调用您自己的服务更容易的原因。但是,如果您想使用它们,这些事件仍然存在。

      public function configureActions(Actions $actions): Actions
      {
          // Create your action
          $viewRelatedSites = Action::new('viewRelatedSites', 'Sites', 'fa fa-file-invoice');
      
          //set the link using a string or a callable (function like its being used here)
          $viewRelatedSites->linkToUrl(function($entity) {
              $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
              $url = $adminUrlGenerator
                  ->setController(SiteCrudController::class)
                  ->setAction(Action::INDEX)
                  ->set('filters', [
                      'agent' => [
                          'comparison' => '=',
                          'value' => $entity->getId()
                      ]
                  ])
                  ->generateUrl();
              return $url;
          });
          
          $actions->add(Crud::PAGE_INDEX, $viewRelatedSites);
          
          return $actions;
      }
      

      【讨论】:

        【解决方案3】:

        在我的 AbstractCrudController 中寻找当前实体一天后,我发现了这个:

        $currentEntity = $this->getContext()->getEntity()->getInstance();
        

        【讨论】:

        • 奇怪的是我发现这种方法在symfony documentation 中只做一次。因此,如果这是一种不好的做法,请随时提供更多信息。
        猜你喜欢
        • 2016-08-21
        • 2011-09-19
        • 2022-01-06
        • 1970-01-01
        • 2011-12-02
        • 2021-08-26
        • 1970-01-01
        • 1970-01-01
        • 2018-11-27
        相关资源
        最近更新 更多