public function testGetConfig() { $this->assertEquals('Test\\Class', $this->fieldId->getClassName()); $this->assertEquals('testScope', $this->fieldId->getScope()); $this->assertEquals('testField', $this->fieldId->getFieldName()); $this->assertEquals('string', $this->fieldId->getFieldType()); $this->assertEquals('field_testScope_Test-Class_testField', $this->fieldId->toString()); $this->fieldId->setFieldType('integer'); $this->assertEquals('integer', $this->fieldId->getFieldType()); }
/** * Process field * * @param array $mapConfig * @param ConfigInterface $searchConfig * @param FieldConfigId $fieldId * @param string $className */ protected function processFields(&$mapConfig, ConfigInterface $searchConfig, FieldConfigId $fieldId, $className) { $extendConfigProvider = $this->configManager->getProvider('extend'); $fieldName = $fieldId->getFieldName(); if ($searchConfig->is('searchable')) { $fieldType = $this->transformCustomType($fieldId->getFieldType()); if (in_array($fieldType, [Indexer::RELATION_ONE_TO_ONE, Indexer::RELATION_MANY_TO_ONE])) { $config = $extendConfigProvider->getConfig($className, $fieldName); $targetEntity = $config->get('target_entity'); $targetField = $config->get('target_field'); $targetType = $this->transformCustomType($this->configManager->getId('extend', $targetEntity, $targetField)->getFieldType()); $field = ['name' => $fieldName, 'relation_type' => $fieldType, 'relation_fields' => [['name' => $targetField, 'target_type' => $targetType, 'target_fields' => [strtolower($fieldName . '_' . $targetField)]]]]; } elseif (in_array($fieldType, [Indexer::RELATION_MANY_TO_MANY, Indexer::RELATION_ONE_TO_MANY])) { $config = $extendConfigProvider->getConfig($className, $fieldName); $targetEntity = $config->get('target_entity'); $targetFields = array_unique(array_merge($config->get('target_grid'), $config->get('target_title'), $config->get('target_detailed'))); $fields = []; foreach ($targetFields as $targetField) { $targetType = $this->transformCustomType($this->configManager->getId('extend', $targetEntity, $targetField)->getFieldType()); $fields[] = ['name' => $targetField, 'target_type' => $targetType, 'target_fields' => [strtolower($fieldName . '_' . $targetField)]]; } $field = ['name' => $fieldName, 'relation_type' => $fieldType, 'relation_fields' => $fields]; } else { $field = ['name' => $fieldName, 'target_type' => $fieldType, 'target_fields' => [strtolower($fieldName)]]; } $mapConfig[$className][self::FIELDS_PATH][] = $field; } }
/** * Guess formatters for given fieldConfigId * Returns array with data: * - formatters: array with supported formatters for given field * - default_formatter: default formatter for given field * * @param FieldConfigId $configId * * @return array|null */ public function guessFormatters(FieldConfigId $configId) { $fieldType = $configId->getFieldType(); $formatters = []; $defaultFormatter = null; $found = false; foreach ($this->formatters as $formatterName => $formatter) { $isSupport = in_array($fieldType, $formatter->getSupportedTypes()); if ($isSupport) { $found = true; $formatters[] = $formatterName; if ($formatter->isDefaultFormatter()) { $defaultFormatter = $formatterName; } } } if ($found) { return ['formatters' => $formatters, 'default_formatter' => $defaultFormatter]; } return null; }
protected function renameExtendField(Schema $schema, QueryBag $queries, Table $table, FieldConfigId $fieldConfigId, ConfigManager $configManager, EntityMetadataHelper $entityMetadataHelper) { switch ($fieldConfigId->getFieldType()) { case RelationType::MANY_TO_ONE: $this->renameManyToOneExtendField($schema, $queries, $table, $fieldConfigId->getFieldName()); break; case RelationType::ONE_TO_MANY: $config = $configManager->getConfig($fieldConfigId); $targetEntityClassName = $config->get('target_entity'); $this->renameOneToManyExtendField($schema, $queries, $table, $fieldConfigId->getFieldName(), $targetEntityClassName, $entityMetadataHelper); break; case RelationType::MANY_TO_MANY: break; default: $oldColumnName = 'field_' . $fieldConfigId->getFieldName(); if ($table->hasColumn($oldColumnName)) { $this->renameExtension->renameColumn($schema, $queries, $table, $oldColumnName, $fieldConfigId->getFieldName()); } break; } }
public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array('attr' => array('class' => 'extend-rel-target-name'), 'label' => 'oro.entity_extend.form.target_entity', 'empty_value' => $this->targetEntity ? null : '', 'read_only' => (bool) $this->targetEntity, 'choices' => $this->getEntityChoiceList($this->configId->getClassName(), $this->configId->getFieldType()), 'choice_attr' => function ($choice) { return $this->getChoiceAttributes($choice); }, 'configs' => array('allowClear' => true, 'placeholder' => 'oro.entity.form.choose_entity', 'result_template_twig' => 'OroEntityBundle:Choice:entity/result.html.twig', 'selection_template_twig' => 'OroEntityBundle:Choice:entity/selection.html.twig'))); }
/** * @param ConfigInterface $fieldConfig */ protected function createSelfRelation(ConfigInterface $fieldConfig) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); $targetEntityClass = $fieldConfig->get('target_entity'); $selfFieldId = new FieldConfigId('extend', $fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), $this->fieldTypeHelper->getUnderlyingType($fieldConfigId->getFieldType())); $selfConfig = $this->extendConfigProvider->getConfig($selfFieldId->getClassName()); $relationKey = ExtendHelper::buildRelationKey($selfFieldId->getClassName(), $selfFieldId->getFieldName(), $selfFieldId->getFieldType(), $targetEntityClass); /** * in case of oneToMany relation * automatically create target field (type: manyToOne) */ $targetFieldId = false; $owner = true; $targetOwner = false; if (in_array($selfFieldId->getFieldType(), RelationType::$toManyRelations)) { $classNameArray = explode('\\', $selfFieldId->getClassName()); $relationFieldName = strtolower(array_pop($classNameArray)) . '_' . $selfFieldId->getFieldName(); if ($selfFieldId->getFieldType() === RelationType::ONE_TO_MANY) { $owner = false; $targetOwner = true; } $targetFieldId = new FieldConfigId('extend', $targetEntityClass, $relationFieldName, ExtendHelper::getReverseRelationType($selfFieldId->getFieldType())); } $selfRelationConfig = ['assign' => false, 'field_id' => $selfFieldId, 'owner' => $owner, 'target_entity' => $targetEntityClass, 'target_field_id' => $targetFieldId]; if ($fieldConfig->has('cascade')) { $selfRelationConfig['cascade'] = $fieldConfig->get('cascade'); } $selfRelations = $selfConfig->get('relation') ?: []; $selfRelations[$relationKey] = $selfRelationConfig; $selfConfig->set('relation', $selfRelations); $this->extendConfigProvider->persist($selfConfig); $targetConfig = $this->extendConfigProvider->getConfig($targetEntityClass); $targetRelationConfig = ['assign' => false, 'field_id' => $targetFieldId, 'owner' => $targetOwner, 'target_entity' => $selfFieldId->getClassName(), 'target_field_id' => $selfFieldId]; $targetRelations = $targetConfig->get('relation') ?: []; $targetRelations[$relationKey] = $targetRelationConfig; $targetConfig->set('relation', $targetRelations); $fieldConfig->set('relation_key', $relationKey); $this->extendConfigProvider->persist($targetConfig); }
/** * @param array $options * @param ConfigInterface $extendConfig * @param FieldConfigId $fieldConfigId * * @return array */ protected function addConstraintsToOptions(array $options, ConfigInterface $extendConfig, FieldConfigId $fieldConfigId) { switch ($fieldConfigId->getFieldType()) { case 'decimal': $options['constraints'] = [new Decimal(['precision' => $extendConfig->get('precision'), 'scale' => $extendConfig->get('scale')])]; break; case 'string': $length = $extendConfig->get('length') ?: 255; $options['constraints'] = [new Length(['max' => $length])]; break; } return $options; }
public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array('attr' => array('class' => 'extend-rel-target-name'), 'label' => 'Target entity', 'empty_value' => $this->targetEntity ? false : 'Please choice target entity...', 'read_only' => (bool) $this->targetEntity, 'choices' => $this->getEntityChoiceList($this->configId->getClassName(), $this->configId->getFieldType()))); }
/** * @param string $code * @param $label * @param FieldConfigId $fieldConfig * * @return array */ protected function createFieldArrayDefinition($code, $label, FieldConfigId $fieldConfig) { return [$code => ['type' => 'field', 'label' => $label, 'field_name' => $code, 'filter_type' => $this->filterMap[$fieldConfig->getFieldType()], 'required' => false, 'sortable' => true, 'filterable' => true, 'show_filter' => true]]; }