Example #1
0
 /**
  * @param IControlConfig $controlConfig
  * @return string
  */
 public function getLabelFor(IControlConfig $controlConfig)
 {
     $label = $this->format;
     $fieldName = $controlConfig->getFieldName();
     $className = $controlConfig->getParentClassName();
     if (Nette\Utils\Strings::contains($fieldName, DotNotation\ClassMetadataWrapper::FIELD_SEPARATOR)) {
         $classMetadata = $this->classMetadataFactory->getMetadataFor($className);
         $parts = explode(DotNotation\ClassMetadataWrapper::FIELD_SEPARATOR, $fieldName);
         $fieldName = array_pop($parts);
         $association = implode(DotNotation\ClassMetadataWrapper::FIELD_SEPARATOR, $parts);
         $className = $classMetadata->getAssociationTargetClass($association);
     }
     $classParts = explode('\\', $className);
     $classParts = array_map(function ($value) {
         return lcfirst($value);
     }, $classParts);
     // Convert \NameSpace1\NameSpace2\ClassName to nameSpace1.nameSpace2.className
     $fullQualifiedClassName = trim(implode('.', $classParts), '.');
     // Convert \NameSpace1\NameSpace2\ClassName to className
     $shortClassName = end($classParts);
     $label = str_replace(self::FIELD_NAME, $fieldName, $label);
     $label = str_replace(self::CLASS_NAME, $shortClassName, $label);
     $label = str_replace(self::FULL_QUALIFIED_CLASS_NAME, $fullQualifiedClassName, $label);
     return $label;
 }
Example #2
0
 /**
  * @param IControlConfig $controlConfig
  * @throws \NForms\Exceptions\FieldNotFoundException
  */
 protected function setupControlConfig(IControlConfig $controlConfig)
 {
     $name = $controlConfig->getFieldName();
     $classMetadata = $this->getClassMetadata();
     if ($classMetadata->hasField($name)) {
         $controlConfig->setMapping($classMetadata->getFieldMapping($name));
         $controlConfig->setType($classMetadata->getFieldType($name));
     } elseif ($classMetadata->hasAssociation($name)) {
         $controlConfig->setMapping($classMetadata->getAssociationMapping($name));
         $controlConfig->setTargetClass($classMetadata->getAssociationTargetClass($name));
         $controlConfig->setType($classMetadata->isSingleValuedAssociation($name) ? IControlConfig::TO_ONE : IControlConfig::TO_MANY);
     } else {
         throw new FieldNotFoundException("Field/association '{$name}' was not found in '{$this->targetClass}' class.");
     }
     $controlConfig->setParentClassName($classMetadata->getClass());
     $controlConfig->setLabel($this->getLabelFor($controlConfig));
     $constraintMetadata = $this->getConstraintMetadata();
     if ($constraintMetadata) {
         $controlConfig->setConstraints($constraintMetadata->getConstraintsForField($name));
     }
 }