/** * {@inheritdoc} */ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) { $options = array(); $options['sonata_field_description'] = $fieldDescription; if ($type == 'sonata_type_model' || $type == 'sonata_type_model_list' || $type == 'sonata_type_model_hidden') { if ($fieldDescription->getOption('edit') == 'list') { throw new \LogicException('The ``sonata_type_model`` type does not accept an ``edit`` option anymore, please review the UPGRADE-2.1.md file from the SonataAdminBundle'); } $options['class'] = $fieldDescription->getTargetEntity(); $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); } elseif ($type == 'sonata_type_admin') { if (!$fieldDescription->getAssociationAdmin()) { throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity())); } if (!in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE))) { throw new \RuntimeException(sprintf('You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or Many-To-One. Maybe you want `sonata_model_list` instead?', $fieldDescription->getName())); } $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); } elseif ($type == 'sonata_type_collection') { if (!$fieldDescription->getAssociationAdmin()) { throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity())); } $options['type'] = 'sonata_type_admin'; $options['modifiable'] = true; $options['type_options'] = array('sonata_field_description' => $fieldDescription, 'data_class' => $fieldDescription->getAssociationAdmin()->getClass()); } return $options; }
/** * @param string $type * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription * * @return array */ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) { $options = array('sonata_field_description' => $fieldDescription); if ($type == 'sonata_type_model' || $type == 'sonata_type_model_list') { if ($fieldDescription->getOption('edit') == 'list') { throw new \LogicException('The ``sonata_type_model`` type does not accept an ``edit`` option anymore, please review the UPGRADE-2.1.md file from the SonataAdminBundle'); } $options['class'] = $fieldDescription->getTargetEntity(); $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); } elseif ($type == 'sonata_type_admin') { if (!$fieldDescription->getAssociationAdmin()) { throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity())); } $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); } elseif ($type == 'sonata_type_collection') { if (!$fieldDescription->getAssociationAdmin()) { throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity())); } $options['type'] = 'sonata_type_admin'; $options['modifiable'] = true; $options['type_options'] = array('sonata_field_description' => $fieldDescription, 'data_class' => $fieldDescription->getAssociationAdmin()->getClass()); } return $options; }
/** * @param $type * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription * @return array */ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) { $options = array(); $options['sonata_field_description'] = $fieldDescription; if ($type == 'doctrine_phpcr_type_tree_model') { $options['class'] = $fieldDescription->getTargetEntity(); $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); } if ($type == 'sonata_type_model') { $options['class'] = $fieldDescription->getTargetEntity(); $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); switch ($fieldDescription->getMappingType()) { case ClassMetadata::ONE_TO_MANY: case ClassMetadata::MANY_TO_MANY: $options['multiple'] = true; $options['parent'] = 'choice'; break; case ClassMetadata::MANY_TO_ONE: case ClassMetadata::ONE_TO_ONE: break; } if ($fieldDescription->getOption('edit') == 'list') { $options['parent'] = 'text'; if (!array_key_exists('required', $options)) { $options['required'] = false; } } } else { if ($type == 'sonata_type_admin') { // nothing here ... $options['edit'] = 'inline'; } else { if ($type == 'sonata_type_collection') { $options['type'] = 'sonata_type_admin'; $options['modifiable'] = true; $options['type_options'] = array('sonata_field_description' => $fieldDescription, 'data_class' => $fieldDescription->getAssociationAdmin()->getClass()); } } } return $options; }
/** * {@inheritdoc} */ public function attachAdminClass(FieldDescriptionInterface $fieldDescription) { $pool = $this->getConfigurationPool(); $adminCode = $fieldDescription->getOption('admin_code'); if ($adminCode !== null) { $admin = $pool->getAdminByAdminCode($adminCode); } else { $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity()); } if (!$admin) { return; } if ($this->hasRequest()) { $admin->setRequest($this->getRequest()); } $fieldDescription->setAssociationAdmin($admin); }
/** * attach an admin instance to the given FieldDescription * * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription */ public function attachAdminClass(FieldDescriptionInterface $fieldDescription) { $pool = $this->getConfigurationPool(); $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity()); if (!$admin) { return; } $fieldDescription->setAssociationAdmin($admin); }
/** * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription * @return array */ public function getChoices(FieldDescriptionInterface $fieldDescription) { $modelManager = $fieldDescription->getAdmin()->getModelManager(); $targets = $modelManager->getEntityManager() ->createQueryBuilder() ->select('t') ->from($fieldDescription->getTargetEntity(), 't') ->getQuery() ->execute(); $choices = array(); foreach ($targets as $target) { // todo : puts this into a configuration option and use reflection foreach (array('getTitle', 'getName', '__toString') as $getter) { if (method_exists($target, $getter)) { $choices[$modelManager->getNormalizedIdentifier($target)] = $target->$getter(); break; } } } return $choices; }
/** * attach an admin instance to the given FieldDescription * * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription */ public function attachAdminClass(FieldDescriptionInterface $fieldDescription) { $pool = $this->getConfigurationPool(); $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity()); if (!$admin) { throw new \RuntimeException(sprintf('You must define an Admin class for the `%s` field (targetEntity=%s)', $fieldDescription->getFieldName(), $fieldDescription->getTargetEntity())); } $fieldDescription->setAssociationAdmin($admin); }
/** * @param FieldDescriptionInterface $fieldDescription * * @return \LogicException */ protected function getAssociationAdminException(FieldDescriptionInterface $fieldDescription) { $msg = sprintf('The current field `%s` is not linked to an admin. Please create one', $fieldDescription->getName()); if (in_array($fieldDescription->getMappingType(), array(ClassMetadata::MANY_TO_ONE, ClassMetadata::MANY_TO_MANY, 'referrers'))) { if ($fieldDescription->getTargetEntity()) { $msg .= " for the target document: `{$fieldDescription->getTargetEntity()}`"; } $msg .= ', specify the `targetDocument` in the Reference, or the `referringDocument` in the Referrers or use the option `admin_code` to link it.'; } else { $msg .= ' and use the option `admin_code` to link it.'; } return new \LogicException($msg); }
/** * @param \Symfony\Component\Form\FormBuilder $formBuilder * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription * @return \Symfony\Component\Form\Type\FormTypeInterface */ protected function defineManyToManyField(FormBuilder $formBuilder, FieldDescriptionInterface $fieldDescription) { $type = $fieldDescription->getOption('form_field_type', 'sonata_type_model'); $options = $fieldDescription->getOption('form_field_options', array()); if ($type == 'sonata_type_model') { $options['class'] = $fieldDescription->getTargetEntity(); $options['multiple'] = true; $options['field_description'] = $fieldDescription; $options['parent'] = 'choice'; $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); } $formBuilder->add($fieldDescription->getName(), $type, $options); }
/** * @param $type * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription * @param array $options * @return array */ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) { $options = array(); $options['sonata_field_description'] = $fieldDescription; if ($type == 'sonata_type_model') { $options['class'] = $fieldDescription->getTargetEntity(); $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); switch ($fieldDescription->getMappingType()) { case ClassMetadataInfo::ONE_TO_MANY: case ClassMetadataInfo::MANY_TO_MANY: $options['multiple'] = true; $options['parent'] = 'choice'; break; case ClassMetadataInfo::MANY_TO_ONE: case ClassMetadataInfo::ONE_TO_ONE: break; } } else if ($type == 'sonata_type_admin') { // nothing here ... $options['edit'] = 'inline'; } else if ($type == 'sonata_type_collection') { $options['type'] = 'sonata_type_admin'; $options['modifiable'] = true; $options['type_options'] = array( 'sonata_field_description' => $fieldDescription, 'data_class' => $fieldDescription->getAssociationAdmin()->getClass() ); } return $options; }