无线/doctrine-mongodb-translatable-form-bundle

1.1.0 2019-06-14 12:13 UTC

This package is auto-updated.

Last update: 2024-09-05 18:18:19 UTC


README

此 Symfony 包的目标是使用 Gedmo Doctrine 扩展和 StofDoctrineExtensionsBundle 简化可翻译表单的创建。

基于 https://github.com/Simettric/DoctrineTranslatableFormBundle 的分支,经过编辑以使其与 Symfony 4.x 兼容,并使用 Doctrine MongoDB ODM 代替 Doctrine ORM。

安装

步骤 1: 下载包

该包已上传到 https://packagist.org.cn/packages/wireless/doctrine-mongodb-translatable-form-bundle,这是 Composer 的主要包仓库,因此您可以使用 composer require 命令下载它

composer require wireless/doctrine-mongodb-translatable-form-bundle

此命令要求您全局安装 Composer,如 Composer 文档中的 安装章节 所述。

步骤 2: 启用包

然后,通过将其添加到项目中 app/AppKernel.php 文件中注册的包列表中来启用包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
            new Simettric\DoctrineTranslatableFormBundle\SimettricDoctrineTranslatableFormBundle(),
        );

        // ...
    }

    // ...
}

配置

您必须在 stof_doctrine_extensions 配置选项中激活 persist_default_translation 键

#app/config/config.yml
stof_doctrine_extensions:
    default_locale: %locale%
    translation_fallback: true
    persist_default_translation: true
    mongodb:
        default:
            translatable: true
            
            

配置视图

此包实现了 Bootstrap 标签组件 以显示每个字段的不同的区域设置输入。

如果您想使用它,只需将模板添加到您的 Twig 配置中的 form_themes 部分。显然,必须在您的布局中加载 bootstrap 资产。

# app/config/config.yml
twig:
    ...
    form_themes:
        - 'SimettricDoctrineTranslatableFormBundle:Form:fields.html.twig'
            

或者,如果您不希望它覆盖您所有的表单主题,您可以在特定的 Twig 模板中添加表单主题

{% form_theme form 'SimettricDoctrineTranslatableFormBundle:Form:fields.html.twig' %}

创建您的表单

这是一个简单示例,展示了您如何编码可翻译表单

<?php

namespace AppBundle\Form;


use Simettric\DoctrineTranslatableFormBundle\Form\AbstractTranslatableType;
use Simettric\DoctrineTranslatableFormBundle\Form\TranslatableTextType;

class YourFormType extends AbstractTranslatableType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        // you can add the translatable fields
        $this->createTranslatableMapper($builder, $options)
             ->add("name", TranslatableTextType::class)
             ->add("description", TranslatableTextareaType::class)
        ;

        // and then you can add the rest of the fields using the standard way
        $builder->add('enabled')
        ;

    }
    
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {

        $resolver->setDefaults(array(
            'data_class'   => 'AppBundle\Document\YourForm'
        ));

        // this is required
        $this->configureTranslationOptions($resolver);

    }
}

然后您需要将表单类型声明为服务

#app/config/services.yml

parameters:
    locale: es
    locales: [es, en, fr]

services:
    app.form.your_form_type:
        class: AppBundle\Form\YourFormType
        arguments: ["@sim_trans_form.mapper"]
        calls:
            - [ setRequiredLocale, [%locale%] ]
            - [ setLocales, [%locales%] ]
        tags:
            - { name: form.type }
            
            

现在您可以在控制器中像处理常规实体一样工作

<?php
    ...

    $yourForm = new YourForm();
    
    $form = $this->createForm(YourFormType::class, $yourForm);
    $form->handleRequest($request);
    
    if($form->isSubmitted() && $form->isValid()){
        
        $em = $this->getDoctrine()->getManager();
        
        $em->persist($category);
        $em->flush();
        
        }
    }
    ...

您可以通过定义一个新的自定义映射服务来设置自己的存储库。例如,当您使用特定的翻译存储库进行个人映射配置时,这很有用。

services:
    app.my_custom_mapper:
        class:     Simettric\DoctrineTranslatableFormBundle\Form\DataMapper
        arguments: ["@doctrine.orm.entity_manager", "AppBundle\Repository\PostTranslationRepository"]
        
    app.form.post_type:
        class: AppBundle\Form\PostType
        arguments: ["@app.my_custom_mapper"]
        calls:
            - [ setRequiredLocale, [%locale%] ]
            - [ setLocales, [%locales%] ]
        tags:
            - { name: form.type }

如果您已在您的 Twig 配置中配置了 Bootstrap 标签主题或直接添加了 form_theme,则可以在模板中使用 form_row 标签显示您的字段

<div class="form-group">
    {{ form_row(form.name, {label: "name"|trans}) }}
</div>

<div class="form-group">
    {{ form_row(form.description, {label: "description"|trans}) }}
</div>