backports murph-skeleton

This commit is contained in:
Simon Vieille 2021-06-02 19:16:40 +02:00
parent 761d697363
commit 6f2a9e9c7b
2 changed files with 18 additions and 5 deletions

View file

@ -40,8 +40,8 @@ abstract class Field
$resolver->setAllowedTypes('property', ['null', 'string']);
$resolver->setAllowedTypes('view', 'string');
$resolver->setAllowedTypes('attr', 'array');
$resolver->setAllowedTypes('href', ['null', 'string']);
$resolver->setAllowedTypes('href_attr', 'array');
$resolver->setAllowedTypes('href', ['null', 'string', 'callable']);
$resolver->setAllowedTypes('href_attr', 'array', 'callable');
$resolver->setAllowedTypes('raw', 'boolean');
$resolver->setAllowedTypes('property_builder', ['null', 'callable']);
$resolver->setAllowedValues('sort', function($value) {

View file

@ -42,14 +42,27 @@ class CrudExtension extends AbstractExtension
$render = $instance->buildView($this->twig, $entity, $resolver->resolve($config['options']), $locale);
if (isset($config['options']['href'])) {
$config['options']['href_attr'] = $config['options']['href_attr'] ?? [];
$hrefAttrConfig = $config['options']['href_attr'] ?? [];
$hrefConfig = $config['options']['href'] ?? null;
$attributes = '';
foreach ($config['options']['href_attr'] as $k => $v) {
if (is_callable($hrefAttrConfig)) {
$attrs = (array) call_user_func($hrefAttrConfig, $entity, $config['options']);
} else {
$attrs = $hrefAttrConfig;
}
if (is_callable($hrefConfig)) {
$href = call_user_func($hrefConfig, $entity, $config['options']);
} else {
$href = $hrefConfig;
}
foreach ($attrs as $k => $v) {
$attributes .= sprintf('%s="%s" ', htmlspecialchars($k), htmlspecialchars($v));
}
$render = sprintf('<a href="%s" %s>%s</a>', htmlspecialchars($config['options']['href']), $attributes, $render);
$render = sprintf('<a href="%s" %s>%s</a>', htmlspecialchars($href), $attributes, $render);
}
return $render;