Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
349 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace AlterPHP\EasyAdminExtensionBundle\Configuration; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigPassInterface; | ||
|
||
/** | ||
* Adds custom types for SHOW view : | ||
* - Embedded lists | ||
* | ||
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com> | ||
*/ | ||
class ShowViewConfigPass implements ConfigPassInterface | ||
{ | ||
private $twigLoader; | ||
private $embeddedListHelper; | ||
|
||
private static $mapTypeToTemplates = array( | ||
// Use EasyAdminExtension namespace because EasyAdminTwigExtension checks namespaces | ||
// to detect custom templates. | ||
'embedded_list' => '@EasyAdminExtension/default/field_embedded_list.html.twig', | ||
); | ||
|
||
public function __construct(\Twig_Loader_Filesystem $twigLoader, $embeddedListHelper) | ||
{ | ||
$this->twigLoader = $twigLoader; | ||
$this->embeddedListHelper = $embeddedListHelper; | ||
} | ||
|
||
public function process(array $backendConfig) | ||
{ | ||
$backendConfig = $this->processCustomShowTypes($backendConfig); | ||
|
||
return $backendConfig; | ||
} | ||
|
||
/** | ||
* Process custom types for SHOW view. | ||
* | ||
* @param array $backendConfig | ||
* | ||
* @return array | ||
*/ | ||
private function processCustomShowTypes(array $backendConfig) | ||
{ | ||
foreach ($backendConfig['entities'] as $entityName => $entityConfig) { | ||
foreach ($entityConfig['show']['fields'] as $fieldName => $fieldMetadata) { | ||
if (in_array($fieldMetadata['type'], array_keys(static::$mapTypeToTemplates))) { | ||
$template = $this->isFieldTemplateDefined($fieldMetadata) | ||
? $fieldMetadata['template'] | ||
: static::$mapTypeToTemplates[$fieldMetadata['type']]; | ||
$entityConfig['show']['fields'][$fieldName]['template'] = $template; | ||
; | ||
$entityConfig['show']['fields'][$fieldName]['template_options'] = $this->processTemplateOptions( | ||
$fieldMetadata['type'], $fieldMetadata | ||
); | ||
} | ||
} | ||
|
||
$backendConfig['entities'][$entityName] = $entityConfig; | ||
} | ||
|
||
return $backendConfig; | ||
} | ||
|
||
private function isFieldTemplateDefined(array $fieldMetadata) | ||
{ | ||
return isset($fieldMetadata['template']) | ||
&& '@EasyAdmin/default/label_undefined.html.twig' != $fieldMetadata['template']; | ||
} | ||
|
||
private function findFirstExistingTemplate(array $templatePaths) | ||
{ | ||
foreach ($templatePaths as $templatePath) { | ||
if (null !== $templatePath && $this->twigLoader->exists($templatePath)) { | ||
return $templatePath; | ||
} | ||
} | ||
} | ||
|
||
private function processTemplateOptions(string $type, array $fieldMetadata) | ||
{ | ||
$templateOptions = $fieldMetadata['template_options'] ?? []; | ||
|
||
switch ($type) { | ||
case 'embedded_list': | ||
$parentEntityFqcn = $templateOptions['parent_entity_fqcn'] ?? $fieldMetadata['sourceEntity']; | ||
$parentEntityProperty = $templateOptions['parent_entity_property'] ?? $fieldMetadata['property']; | ||
$entityFqcn = $this->embeddedListHelper->getEntityFqcnFromParent( | ||
$parentEntityFqcn, $parentEntityProperty | ||
); | ||
if (!isset($templateOptions['entity_fqcn'])) { | ||
$templateOptions['entity_fqcn'] = $entityFqcn; | ||
} | ||
if (!isset($templateOptions['parent_entity_property'])) { | ||
$templateOptions['parent_entity_property'] = $parentEntityProperty; | ||
} | ||
if (!isset($templateOptions['entity'])) { | ||
$templateOptions['entity'] = $this->embeddedListHelper->guessEntityEntry($entityFqcn); | ||
} | ||
if (!isset($templateOptions['filters'])) { | ||
$templateOptions['filters'] = []; | ||
} | ||
if (isset($templateOptions['sort'])) { | ||
$sortOptions = $templateOptions['sort']; | ||
$templateOptions['sort'] = [ | ||
'field' => $sortOptions[0], | ||
'direction' => $sortOptions[1] ?? 'DESC', | ||
]; | ||
} else { | ||
$templateOptions['sort'] = null; | ||
} | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
return $templateOptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
{% set action_params = { entity: entity, action: 'embeddedList', filters: filters } %} | ||
{% if sort is defined %} | ||
{% set action_params = action_params|merge({ sortField: sort.field, sortDirection: sort.direction}) %} | ||
{% endif %} | ||
{% set default_filters = guess_default_filters( | ||
field_options.template_options.entity_fqcn, field_options.template_options.parent_entity_property, item | ||
) %} | ||
{% set filters = default_filters|merge(field_options.template_options.filters) %} | ||
|
||
{{ render(path('easyadmin', action_params)) }} | ||
{% include '@EasyAdmin/includes/_include_embedded_list.html.twig' with { | ||
entity: field_options.template_options.entity, | ||
filters: filters, | ||
sort: field_options.template_options.sort | ||
} only %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Resources/views/includes/_include_embedded_list.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{% set action_params = { entity: entity, action: 'embeddedList', filters: filters } %} | ||
{% if sort is not null %} | ||
{% set action_params = action_params|merge({ sortField: sort.field, sortDirection: sort.direction}) %} | ||
{% endif %} | ||
|
||
{{ render(path('easyadmin', action_params)) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace AlterPHP\EasyAdminExtensionBundle\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
class EmbeddedListExtension extends AbstractExtension | ||
{ | ||
protected $embeddedListHelper; | ||
|
||
public function __construct($embeddedListHelper) | ||
{ | ||
$this->embeddedListHelper = $embeddedListHelper; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return array( | ||
new TwigFunction('guess_default_filters', array($this, 'guessDefaultFilters')), | ||
); | ||
} | ||
|
||
public function guessDefaultFilters(string $entityFqcn, string $parentEntityProperty, $parentEntity) | ||
{ | ||
return $this->embeddedListHelper->guessDefaultFilter($entityFqcn, $parentEntityProperty, $parentEntity); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.