pool = $pool; } /** * {@inheritdoc} */ public function initRuntime(\Twig_Environment $environment) { $this->environment = $environment; } /** * {@inheritDoc} */ public function getFilters() { return array( 'render_list_element' => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))), 'render_view_element' => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))), 'render_relation_element' => new \Twig_Filter_Method($this, 'renderRelationElement'), 'sonata_urlsafeid' => new \Twig_Filter_Method($this, 'getUrlsafeIdentifier'), 'sonata_slugify' => new \Twig_Filter_Method($this, 'slugify'), ); } /** * {@inheritDoc} */ public function getTokenParsers() { return array(); } /** * {@inheritDoc} */ public function getName() { return 'sonata_admin'; } /** * @param FieldDescriptionInterface $fieldDescription * @param string $defaultTemplate * * @return \Twig_TemplateInterface */ protected function getTemplate(FieldDescriptionInterface $fieldDescription, $defaultTemplate) { $templateName = $fieldDescription->getTemplate() ? : $defaultTemplate; try { $template = $this->environment->loadTemplate($templateName); } catch (\Twig_Error_Loader $e) { $template = $this->environment->loadTemplate($defaultTemplate); } return $template; } /** * render a list element from the FieldDescription * * @param mixed $object * @param FieldDescriptionInterface $fieldDescription * @param array $params * * @return string */ public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array()) { $template = $this->getTemplate($fieldDescription, $fieldDescription->getAdmin()->getTemplate('base_list_field')); return $this->output($fieldDescription, $template, array_merge($params, array( 'admin' => $fieldDescription->getAdmin(), 'object' => $object, 'value' => $this->getValueFromFieldDescription($object, $fieldDescription), 'field_description' => $fieldDescription ))); } /** * @param FieldDescriptionInterface $fieldDescription * @param \Twig_TemplateInterface $template * @param array $parameters * * @return string */ public function output(FieldDescriptionInterface $fieldDescription, \Twig_TemplateInterface $template, array $parameters = array()) { $content = $template->render($parameters); if ($this->environment->isDebug()) { return sprintf("\n\n%s\n", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $template->getTemplateName(), $content, $fieldDescription->getFieldName() ); } return $content; } /** * return the value related to FieldDescription, if the associated object does no * exists => a temporary one is created * * @param object $object * @param FieldDescriptionInterface $fieldDescription * @param array $params * * @throws \RuntimeException * * @return mixed */ public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array()) { if (isset($params['loop']) && $object instanceof \ArrayAccess) { throw new \RuntimeException('remove the loop requirement'); } $value = null; try { $value = $fieldDescription->getValue($object); } catch (NoValueException $e) { if ($fieldDescription->getAssociationAdmin()) { $value = $fieldDescription->getAssociationAdmin()->getNewInstance(); } } return $value; } /** * render a view element * * @param FieldDescriptionInterface $fieldDescription * @param mixed $object * * @return string */ public function renderViewElement(FieldDescriptionInterface $fieldDescription, $object) { $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig'); try { $value = $fieldDescription->getValue($object); } catch (NoValueException $e) { $value = null; } return $this->output($fieldDescription, $template, array( 'field_description' => $fieldDescription, 'object' => $object, 'value' => $value, 'admin' => $fieldDescription->getAdmin() )); } /** * @throws \RunTimeException * * @param mixed $element * @param FieldDescriptionInterface $fieldDescription * * @return mixed */ public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription) { if (!is_object($element)) { return $element; } $method = $fieldDescription->getOption('associated_tostring', '__toString'); if (!method_exists($element, $method)) { throw new \RunTimeException(sprintf('You must define an `associated_tostring` option or create a `%s::__toString` method to the field option "%s" from service "%s".', get_class($element), $fieldDescription->getName(), $fieldDescription->getAdmin()->getCode())); } return call_user_func(array($element, $method)); } /** * Get the identifiers as a string that is save to use in an url. * * @param object $model * * @return string string representation of the id that is save to use in an url */ public function getUrlsafeIdentifier($model) { $admin = $this->pool->getAdminByClass( ClassUtils::getClass($model) ); return $admin->getUrlsafeIdentifier($model); } /** * Slugify a text * * @param $text * * @return string */ public function slugify($text) { // replace non letter or digits by - $text = preg_replace('~[^\\pL\d]+~u', '-', $text); // trim $text = trim($text, '-'); // transliterate if (function_exists('iconv')) { $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); } // lowercase $text = strtolower($text); // remove unwanted characters $text = preg_replace('~[^-\w]+~', '', $text); return $text; } }