propertyAccessor = PropertyAccess::createPropertyAccessorBuilder() ->getPropertyAccessor() ; $this->twig = $twig; } /** * {@inheritdoc} */ public function getFunctions() { return [ new TwigFunction('render_field', [$this, 'renderField'], ['is_safe' => ['html']]), ]; } public function renderField($entity, array $config, ?string $locale = null): string { $field = $config['field']; $instance = new $field(); $resolver = $instance->configureOptions(new OptionsResolver()); $render = $instance->buildView($this->twig, $entity, $resolver->resolve($config['options']), $locale); if (isset($config['options']['href'])) { $config['options']['href_attr'] = $config['options']['href_attr'] ?? []; $attributes = ''; foreach ($config['options']['href_attr'] as $k => $v) { $attributes .= sprintf('%s="%s" ', htmlspecialchars($k), htmlspecialchars($v)); } $render = sprintf('%s', htmlspecialchars($config['options']['href']), $attributes, $render); } return $render; } }