Skip to content

Commit

Permalink
Merge c466e55 into 4f616df
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Jul 19, 2018
2 parents 4f616df + c466e55 commit 4091b18
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 52 deletions.
121 changes: 121 additions & 0 deletions src/Configuration/ShowViewConfigPass.php
@@ -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;
}
}
2 changes: 2 additions & 0 deletions src/Form/Type/EasyAdminEmbeddedListType.php
Expand Up @@ -51,11 +51,13 @@ public function buildView(FormView $view, FormInterface $form, array $options)
);
}

// TODO: Move the default filters guess to the template (as for SHOW view)
// Let default filters be overriden by defined filters
$embeddedListFilters = array_merge($defaultFilters, $embeddedListFilters);

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

// XXX : Only for backward compatibility (when there were no guesser)
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$filters = array_map(function ($filter) use ($propertyAccessor, $form) {
if (0 === strpos($filter, 'form:')) {
Expand Down
14 changes: 7 additions & 7 deletions src/Helper/EmbeddedListHelper.php
Expand Up @@ -98,12 +98,12 @@ function ($entityConfig) use ($entityFqcn) {
* Returns default filter for embeddedList.
*
* @param string $entityFqcn
* @param string $embeddedListFieldName
* @param object $targetEntity
* @param string $parentEntityProperty
* @param object $parentEntity
*
* @return array
*/
public function guessDefaultFilter(string $entityFqcn, string $embeddedListFieldName, $targetEntity)
public function guessDefaultFilter(string $entityFqcn, string $parentEntityProperty, $parentEntity)
{
$entityClassMetadata = $this->doctrine->getManagerForClass($entityFqcn)->getClassMetadata($entityFqcn);

Expand All @@ -113,16 +113,16 @@ public function guessDefaultFilter(string $entityFqcn, string $embeddedListField
}

$entityAssociations = $entityClassMetadata->getAssociationMappings();
$targetEntityFqcn = get_class($targetEntity);
$parentEntityFqcn = get_class($parentEntity);
foreach ($entityAssociations as $assoc) {
if (
$targetEntityFqcn === $assoc['targetEntity']
&& $embeddedListFieldName === $assoc['inversedBy']
$parentEntityFqcn === $assoc['targetEntity']
&& $parentEntityProperty === $assoc['inversedBy']
&& 1 === count($assoc['joinColumns'])
) {
$assocFieldPart = 'entity.'.$assoc['fieldName'];
$assocIdentifierValue = PropertyAccess::createPropertyAccessor()->getValue(
$targetEntity, $assoc['joinColumns'][0]['referencedColumnName']
$parentEntity, $assoc['joinColumns'][0]['referencedColumnName']
);

return [$assocFieldPart => $assocIdentifierValue];
Expand Down
11 changes: 9 additions & 2 deletions src/Resources/config/services.xml
Expand Up @@ -25,6 +25,12 @@
<argument type="service" id="alterphp.easyadmin_extension.helper.embedded_list"/>
<tag name="form.type"/>
</service>
<service id="alterphp.easyadmin_extension.config_pass.show_view" class="AlterPHP\EasyAdminExtensionBundle\Configuration\ShowViewConfigPass">
<argument type="service" id="twig.loader.filesystem" />
<argument type="service" id="alterphp.easyadmin_extension.helper.embedded_list"/>
<!-- Makes it process just after EasyAdmin TemplateConfigPass -->
<tag name="easyadmin.config_pass" priority="19"/>
</service>

<!-- Admin security roles -->
<service id="alterphp.easyadmin_extension.helper.editable_roles" class="AlterPHP\EasyAdminExtensionBundle\Helper\EditableRolesHelper">
Expand All @@ -45,12 +51,13 @@
<service id="alterphp.easyadmin_extension.helper.menu" class="AlterPHP\EasyAdminExtensionBundle\Helper\MenuHelper">
<argument type="service" id="alterphp.easyadmin_extension.admin_authorization_checker"/>
</service>
<service id="alterphp.easyadmin_extension.twig.extension.menu" class="AlterPHP\EasyAdminExtensionBundle\Twig\MenuExtension">
<argument type="service" id="alterphp.easyadmin_extension.helper.menu"/>
<service id="alterphp.easyadmin_extension.twig.extension.embedded_list" class="AlterPHP\EasyAdminExtensionBundle\Twig\EmbeddedListExtension">
<argument type="service" id="alterphp.easyadmin_extension.helper.embedded_list"/>
<tag name="twig.extension"/>
</service>
<service id="alterphp.easyadmin_extension.twig.extension.admin_authorization" class="AlterPHP\EasyAdminExtensionBundle\Twig\AdminAuthorizationExtension">
<argument type="service" id="alterphp.easyadmin_extension.admin_authorization_checker"/>
<argument type="service" id="alterphp.easyadmin_extension.helper.menu"/>
<tag name="twig.extension"/>
</service>
</services>
Expand Down
14 changes: 9 additions & 5 deletions src/Resources/views/default/field_embedded_list.html.twig
@@ -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 %}
6 changes: 1 addition & 5 deletions src/Resources/views/form/bootstrap_3_layout.html.twig
@@ -1,11 +1,7 @@
{% use '@BaseEasyAdmin/form/bootstrap_3_layout.html.twig' %}

{% block easyadmin_embedded_list_row %}
{% 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 %}
{{ render(path('easyadmin', action_params)) }}
{% include '@EasyAdmin/includes/_include_embedded_list.html.twig' with {entity: entity, filters: filters, sort: sort|default(null)} only %}
{% endblock %}

{% block easyadmin_admin_roles_widget %}
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/views/includes/_include_embedded_list.html.twig
@@ -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)) }}
18 changes: 13 additions & 5 deletions src/Twig/AdminAuthorizationExtension.php
Expand Up @@ -9,23 +9,26 @@
class AdminAuthorizationExtension extends AbstractExtension
{
protected $adminAuthorizationChecker;
protected $menuHelper;

public function __construct($adminAuthorizationChecker)
public function __construct($adminAuthorizationChecker, $menuHelper)
{
$this->adminAuthorizationChecker = $adminAuthorizationChecker;
$this->menuHelper = $menuHelper;
}

public function getFunctions()
public function getFilters()
{
return array(
new TwigFunction('is_easyadmin_granted', array($this, 'isEasyAdminGranted')),
new TwigFilter('prune_item_actions', array($this, 'pruneItemsActions')),
new TwigFilter('prune_menu_items', array($this, 'pruneMenuItems')),
);
}

public function getFilters()
public function getFunctions()
{
return array(
new TwigFilter('prune_item_actions', array($this, 'pruneItemsActions')),
new TwigFunction('is_easyadmin_granted', array($this, 'isEasyAdminGranted')),
);
}

Expand All @@ -40,4 +43,9 @@ public function pruneItemsActions(array $itemActions, array $entity, array $forb
return !in_array($action, $forbiddenActions) && $this->isEasyAdminGranted($entity, $action);
}, ARRAY_FILTER_USE_KEY);
}

public function pruneMenuItems(array $menuConfig, array $entitiesConfig)
{
return $this->menuHelper->pruneMenuItems($menuConfig, $entitiesConfig);
}
}
28 changes: 28 additions & 0 deletions src/Twig/EmbeddedListExtension.php
@@ -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);
}
}
28 changes: 0 additions & 28 deletions src/Twig/MenuExtension.php

This file was deleted.

0 comments on commit 4091b18

Please sign in to comment.