/** * @param ConfigInterface $fieldConfig * @param string $fieldName * @param object $entity * @return null|mixed */ protected function getEntityFieldData(ConfigInterface $fieldConfig, $fieldName, $entity) { if ($fieldConfig->getId()->getFieldType() != 'optionSet' || !FieldAccessor::hasGetter($entity, $fieldName) || !($options = FieldAccessor::getValue($entity, $fieldName))) { return null; } return $options; }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('added', 'oro_entity_identifier', ['class' => $options['class'], 'multiple' => true])->add('removed', 'oro_entity_identifier', ['class' => $options['class'], 'multiple' => true]); if ($options['extend']) { $em = $this->entityManager; $class = $options['class']; $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use($em, $class) { $data = $event->getData(); $repository = $em->getRepository($class); $targetData = $event->getForm()->getParent()->getData(); $fieldName = $event->getForm()->getName(); if (!empty($data['added'])) { foreach (explode(',', $data['added']) as $id) { $entity = $repository->find($id); if ($entity) { FieldAccessor::addValue($targetData, $fieldName, $entity); } } } if (!empty($data['removed'])) { foreach (explode(',', $data['removed']) as $id) { $entity = $repository->find($id); if ($entity) { FieldAccessor::removeValue($targetData, $fieldName, $entity); } } } }); } }
public function testHasGetter() { $entity = new FieldAccessorTestEntity(); $this->assertTrue(FieldAccessor::hasGetter($entity, 'name')); $this->assertTrue(FieldAccessor::hasGetter($entity, 'default_name')); $this->assertTrue(FieldAccessor::hasGetter($entity, 'anotherName')); $this->assertFalse(FieldAccessor::hasGetter($entity, 'unknown')); }
/** * @param string $id * @param string $entityName * @param string $fieldName * * @return array * * @Route( * "/detailed/{id}/{entityName}/{fieldName}", * name="oro_entity_detailed", * defaults={"id"=0, "fieldName"=""} * ) * @Template */ public function detailedAction($id, $entityName, $fieldName) { $entityClass = $this->get('oro_entity.routing_helper')->resolveEntityClass($entityName); if (!class_exists($entityClass)) { throw $this->createNotFoundException(); } $this->checkAccess('VIEW', $entityClass); $entityProvider = $this->get('oro_entity_config.provider.entity'); $extendProvider = $this->get('oro_entity_config.provider.extend'); $relationConfig = $extendProvider->getConfig($entityClass, $fieldName); $relationTargetEntity = $relationConfig->get('target_entity'); if (!class_exists($relationTargetEntity)) { throw $this->createNotFoundException(); } /** @var ConfigInterface[] $fields */ $fields = $extendProvider->filter(function (ConfigInterface $config) use($relationConfig) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $config->getId(); return !$config->is('state', ExtendScope::STATE_NEW) && !$config->is('is_deleted') && $fieldConfigId instanceof FieldConfigId && in_array($fieldConfigId->getFieldName(), (array) $relationConfig->get('target_detailed')); }, $relationConfig->get('target_entity')); $entity = $this->getDoctrine()->getRepository($relationTargetEntity)->find($id); if (!$entity) { return $this->createNotFoundException(); } $dynamicRow = array(); foreach ($fields as $field) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $field->getId(); $fieldName = $fieldConfigId->getFieldName(); $label = $entityProvider->getConfigById($fieldConfigId)->get('label') ?: $fieldName; $dynamicRow[$label] = FieldAccessor::getValue($entity, $fieldName); } return array('dynamic' => $dynamicRow, 'entity' => $entity); }
/** * @param object[] $entities * @param object|null $defaultEntity * @param ConfigInterface $extendConfig * * @return array */ protected function getInitialElements($entities, $defaultEntity, ConfigInterface $extendConfig) { $result = []; foreach ($entities as $entity) { $extraData = []; foreach ($extendConfig->get('target_grid') as $fieldName) { $label = $this->configManager->getProvider('entity')->getConfig($extendConfig->get('target_entity'), $fieldName)->get('label'); $extraData[] = ['label' => $this->translator->trans($label), 'value' => FieldAccessor::getValue($entity, $fieldName)]; } $title = []; foreach ($extendConfig->get('target_title') as $fieldName) { $title[] = FieldAccessor::getValue($entity, $fieldName); } /** * If using ExtendExtension with a form that only updates part of * of the entity, we need to make sure an ID is present. An ID * isn't present when a PHP-based Validation Constraint is fired. */ if (null !== $entity->getId()) { $result[] = ['id' => $entity->getId(), 'label' => implode(' ', $title), 'link' => $this->router->generate('oro_entity_detailed', ['id' => $entity->getId(), 'entityName' => str_replace('\\', '_', $extendConfig->getId()->getClassName()), 'fieldName' => $extendConfig->getId()->getFieldName()]), 'extraData' => $extraData, 'isDefault' => $defaultEntity != null && $defaultEntity->getId() == $entity->getId()]; } } return $result; }
/** * @param object[] $entities * @param object|null $defaultEntity * @param ConfigInterface $extendConfig * * @return array */ protected function getInitialElements($entities, $defaultEntity, ConfigInterface $extendConfig) { $result = []; foreach ($entities as $entity) { $extraData = []; foreach ($extendConfig->get('target_grid') as $fieldName) { $label = $this->configManager->getProvider('entity')->getConfig($extendConfig->get('target_entity'), $fieldName)->get('label'); $extraData[] = ['label' => $this->translator->trans($label), 'value' => FieldAccessor::getValue($entity, $fieldName)]; } $title = []; foreach ($extendConfig->get('target_title') as $fieldName) { $title[] = FieldAccessor::getValue($entity, $fieldName); } $result[] = ['id' => $entity->getId(), 'label' => implode(' ', $title), 'link' => $this->router->generate('oro_entity_detailed', ['id' => $entity->getId(), 'entityName' => str_replace('\\', '_', $extendConfig->getId()->getClassName()), 'fieldName' => $extendConfig->getId()->getFieldName()]), 'extraData' => $extraData, 'isDefault' => $defaultEntity != null && $defaultEntity->getId() == $entity->getId()]; } return $result; }
/** * @param object $entity * @param null|string $entityClass * @return array */ public function getFields($entity, $entityClass = null) { $dynamicRow = array(); if (null === $entityClass) { $entityClass = ClassUtils::getRealClass($entity); } $fields = $this->extendProvider->filter(array($this, 'filterFields'), $entityClass); foreach ($fields as $field) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $field->getId(); $fieldName = $fieldConfigId->getFieldName(); $fieldType = $fieldConfigId->getFieldType(); $underlyingFieldType = $this->fieldTypeHelper->getUnderlyingType($fieldType); $value = $this->propertyAccessor->getValue($entity, $fieldName); /** Prepare OptionSet field type */ if ($fieldType == 'optionSet') { $value = $this->getValueForOptionSet($entity, $fieldConfigId); } if ($value && $underlyingFieldType == 'manyToOne') { $value = $this->propertyAccessor->getValue(FieldAccessor::getValue($entity, $fieldName), $field->get('target_field')); } /** Prepare Relation field type */ if ($value && $value instanceof Collection) { $value = $this->getValueForCollection($value, $fieldConfigId); } $fieldConfig = $this->entityProvider->getConfigById($fieldConfigId); $label = $fieldConfig->get('label'); if (!$label) { $label = $fieldName; } $dynamicRow[$fieldName] = array('type' => $fieldType, 'label' => $label, 'value' => $value); } return $dynamicRow; }