Skip to content

Commit

Permalink
Poor result with TemplateConfigPass
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Jul 7, 2018
1 parent 07ad5f2 commit 1e6c466
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/Configuration/TemplateConfigPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace AlterPHP\EasyAdminExtensionBundle\Configuration;

use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigPassInterface;

/**
* Adds templates :
* - Embedded lists in show view
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
*/
class TemplateConfigPass implements ConfigPassInterface
{
private $twigLoader;
private $defaultBackendTemplates = array(
'field_embedded_list' => '@EasyAdmin/default/field_embedded_list.html.twig',
);

public function __construct(\Twig_Loader_Filesystem $twigLoader)
{
$this->twigLoader = $twigLoader;
}

public function process(array $backendConfig)
{
$backendConfig = $this->processEntityTemplates($backendConfig);

return $backendConfig;
}

/**
* Determines the template used to render each backend element. This is not
* trivial because templates can depend on the entity displayed and they
* define an advanced override mechanism.
*
* @param array $backendConfig
*
* @return array
*/
private function processEntityTemplates(array $backendConfig)
{
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
foreach (array('list', 'show') as $view) {
foreach ($entityConfig[$view]['fields'] as $fieldName => $fieldMetadata) {
// if the field defines its own template, resolve its location
if (isset($fieldMetadata['template'])) {
$templatePath = $fieldMetadata['template'];

if (isset($this->defaultBackendTemplates[$templatePath])) {
$templatePath = $this->defaultBackendTemplates[$templatePath];
}

// template path should contain the .html.twig extension
// however, for usability reasons, we silently fix this issue if needed
if ('.html.twig' !== substr($templatePath, -10)) {
$templatePath .= '.html.twig';
@trigger_error(sprintf('Passing a template path without the ".html.twig" extension is deprecated since version 1.11.7 and will be removed in 2.0. Use "%s" as the value of the "template" option for the "%s" field in the "%s" view of the "%s" entity.', $templatePath, $fieldName, $view, $entityName), E_USER_DEPRECATED);
}

// before considering $templatePath a regular Symfony template
// path, check if the given template exists in any of these directories:
// * app/Resources/views/easy_admin/<entityName>/<templatePath>
// * app/Resources/views/easy_admin/<templatePath>
$templatePath = $this->findFirstExistingTemplate(array(
'easy_admin/'.$entityName.'/'.$templatePath,
'easy_admin/'.$templatePath,
$templatePath,
));
} else {
// At this point, we don't know the exact data type associated with each field.
// The template is initialized to null and it will be resolved at runtime in the Configurator class
$templatePath = null;
}

$entityConfig[$view]['fields'][$fieldName]['template'] = $templatePath;
}
}

$backendConfig['entities'][$entityName] = $entityConfig;
}

return $backendConfig;
}

private function findFirstExistingTemplate(array $templatePaths)
{
foreach ($templatePaths as $templatePath) {
if (null !== $templatePath && $this->twigLoader->exists($templatePath)) {
return $templatePath;
}
}
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<argument type="service" id="alterphp.easyadmin_extension.helper.embedded_list"/>
<tag name="form.type"/>
</service>
<service id="alterphp.easyadmin_extension.config_pass.template" class="AlterPHP\EasyAdminExtensionBundle\Configuration\TemplateConfigPass">
<argument type="service" id="twig.loader.filesystem" />
<!-- Makes it process just after EasyAdmin TemplateConfigPass -->
<tag name="easyadmin.config_pass" priority="21"/>
</service>

<!-- Admin security roles -->
<service id="alterphp.easyadmin_extension.helper.editable_roles" class="AlterPHP\EasyAdminExtensionBundle\Helper\EditableRolesHelper">
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/views/default/field_embedded_list.html.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{# TODO: keep or not ? #}
{% set filters = field_options.template_options.filters|default({})|merge({(field_options.template_options.association_alias): item.id }) %}

{% 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}) %}
Expand Down

0 comments on commit 1e6c466

Please sign in to comment.