Skip to content

Commit

Permalink
Fix embeddedList in NEW views
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed May 14, 2018
1 parent fba7a00 commit c76cdc2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
27 changes: 12 additions & 15 deletions src/Form/Type/EasyAdminEmbeddedListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace AlterPHP\EasyAdminExtensionBundle\Form\Type;

use Doctrine\ORM\PersistentCollection as OrmPersistentCollection;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
Expand Down Expand Up @@ -32,27 +31,25 @@ public function getBlockPrefix()
public function buildView(FormView $view, FormInterface $form, array $options)
{
$data = $form->getData();
$parentData = $form->getParent()->getData();

$embeddedListEntity = $options['entity'];
$embeddedListFilters = $options['filters'];

if ($data instanceof OrmPersistentCollection) {
$entityFqcn = $data->getTypeClass()->getName();
// Guess entity FQCN from parent metadata
$entityFqcn = $this->embeddedListHelper->getEntityFqcnFromParent(get_class($parentData), $form->getName());

// Guess embeddedList entity if not set
if (!isset($embeddedListEntity)) {
$embeddedListEntity = $this->embeddedListHelper->guessEntityEntry($entityFqcn);
}

// Guess default filter and let it be overriden by defined filters
$embeddedListFilters = array_merge(
$this->embeddedListHelper->guessDefaultFilter(
$entityFqcn, $form->getConfig()->getName(), $data->getOwner()
),
$embeddedListFilters
);
// Guess embeddedList entity if not set
if (!isset($embeddedListEntity)) {
$embeddedListEntity = $this->embeddedListHelper->guessEntityEntry($entityFqcn);
}

// Guess default filter and let it be overriden by defined filters
$embeddedListFilters = array_merge(
$this->embeddedListHelper->guessDefaultFilter($entityFqcn, $form->getConfig()->getName(), $parentData),
$embeddedListFilters
);

$view->vars['entity'] = $embeddedListEntity;

$propertyAccessor = PropertyAccess::createPropertyAccessor();
Expand Down
28 changes: 28 additions & 0 deletions src/Helper/EmbeddedListHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ public function __construct(ManagerRegistry $doctrine, array $easyAdminConfig)
$this->easyAdminConfig = $easyAdminConfig;
}

/**
* Returns EasyAdmin entity entry name for a parent FQCN and property for embedded list.
*
* @param string $parentFqcn
* @param string $parentProperty
*
* @return mixed
*
* @throws \RuntimeException
*/
public function getEntityFqcnFromParent(string $parentFqcn, string $parentProperty)
{
$parentClassMetadata = $this->doctrine->getManagerForClass($parentFqcn)->getClassMetadata($parentFqcn);

// Required to use getAssociationMappings method
if (!$parentClassMetadata instanceof ClassMetadataInfo) {
return;
}

try {
$entityFqcn = $parentClassMetadata->getAssociationTargetClass($parentProperty);
} catch (\Exception $e) {
return;
}

return $entityFqcn;
}

/**
* Returns EasyAdmin entity entry name for a FQCN.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Configuration/EmbeddedListViewConfigPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public function testOpenNewTabOption()
'NotSetEntity' => array(
),
'SetTrueEntity' => array(
'embeddedList' => array('open_new_tab' => true)
'embeddedList' => array('open_new_tab' => true),
),
'SetFalseEntity' => array(
'embeddedList' => array('open_new_tab' => false)
'embeddedList' => array('open_new_tab' => false),
),
),
);
Expand Down

0 comments on commit c76cdc2

Please sign in to comment.