cubetools/cube-custom-fields-bundle

表单和实体中的自定义字段

安装次数: 5,477

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 7

分支: 0

开放问题: 5

类型:symfony-bundle

2.1.8 2022-01-25 14:18 UTC

README

将自定义字段添加到表单和实体中(特定于安装)。

安装

步骤 1:下载 Bundle

打开命令行,进入您的项目目录,并执行以下命令以下载此 Bundle 的最新稳定版本

$ composer require cubetools/cube-custom-fields-bundle

此命令需要您全局安装 Composer,具体请参阅 Composer 文档中的安装章节

步骤 2:启用 Bundle

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

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new CubeTools\CubeCustomFieldsBundle\CubeToolsCubeCustomFieldsBundle(),
        );

        // ...
    }

    // ...
}

步骤 3:配置 Bundle

在您的项目 app/config/routing.yml 中添加路由。

    ...
cube_custom_fields:
    resource: "@CubeCustomFieldsBundle/Resources/config/routing/all.yml"

然后在您的项目 app/config/config.yml 中配置 Bundle。

imports:
    ...
    { resource: custom_fields.yml }
    # or to ignore when not existing or invalid { resource: custom_fields.yml, ignore_errors: true }

app/config/custom_fields.yml

cube_custom_fields:
    entities:
        XxBundle\Entities\Entity1:
            field_id1:
                field_type: SomeFormType
                     # like Symfony\Component\Form\Extension\Core\Type\[TextType|SelectType|DateTimeType]|Symfony\Bridge\Doctrine\Form\Type\EntityType]
                label: 'label.for.a_field'
                form_options:
                     # any form option
                     label: label for in form # overwrites label from above
            field_idB:
                type: Symfony\Component\Form\Extension\Core\Type\SelectType
                label: label.select.something
                form_options:
                    choices:
                        label1: value1
                        ...
        SomeTool\YyBundle\Entity\Entity2:
            responsibles: # this links as m:n to an existing entity type
                type: Symfony\Bridge\Doctrine\Form\Type\EntityType
                label: 'Responsible Persons'
                field_options:
                    required: false
                    multiple: true
                    class: 'AppBundle:User'
                    attr:
                        class: select2
                        placeholder: Select responsible persons
            selections: # this links to the custom fields table itself, giving access to all TextCustomField entities with fieldId = predef_1
                type: Symfony\Bridge\Doctrine\Form\Type\EntityType
                label: 'Predefined select options'
                filter: predef_1 # this is the name of the referred custom field
                field_options:
                    required: false
                    multiple: false
                    class: 'CubeTools\CubeCustomFieldsBundle\Entity\TextCustomField'
                    attr:
                        class: select2
                        placeholder: Select from set of options
            owner: # special case for ajax retrievable select2 boxes (Using Tetranz\Select2EntityBundle)
                type: Tetranz\Select2EntityBundle\Form\Type\Select2EntityType # contains the class of the form type used
                label: 'Owned by'
                filters: # this can be used to filter for specific fields on the target entity (e.g. only activated users etc.). Note that this is not the same field as for "normal" entity type custom fields (it is "filter" there).
                    enabled: 1
                field_options:
                    required: false
                    multiple: false
                    class: 'AppBundle:User' # contains the class of the objects visible to the user (the REAL entities)
                    minimum_input_length: 0
                    page_limit: 10
                    scroll: true
                    allow_clear: false
                    delay: 250
                    cache: true
                    cache_timeout: 500
                    placeholder: Please select
                    language: de
                    attr:
                        style: width:100%
                        data-role: none
                        any_none: any:{not empty},none:{empty} # strings used to identify empty or non-empty custom fields in filters
    # access_rights_table: 'XxBundle:AccessEntity'

步骤 X:将自定义字段链接到实体

允许将自定义字段链接到您的实体。

class Xxx
{
    use \CubeTools\CubeCustomFieldsBundle\CustomFieldsEntityHook;
}

步骤 X:在表单中显示字段

将自定义字段添加到您的表单中。

class XxxType extends FormType
{
    ...
    public function buildForm(...)
    {
        ...
        $customFieldsService = $options['customFieldsService']; // or configure your form as a service
        $customFieldsService->addCustomFields($form);
    }
    ...
}

class XzyController extends Controller
{
    ...
    public function zxyAction(CubeTools\CubeCustomFieldsBundle\Form\CustomFieldsFormService $customFieldsService)
    {
        // or $customFieldsService = $this->get('cube_custom_fields.form_fields');
        $form = $this->createForm(XxxType::class, null, array('customFieldsService' => $customFieldsService;
        ...
    }

步骤 X:显示字段

import dynamicFields.macro.twig as dynFld

...
{{ dynFld.title_column(entities) }}
...
{{ dynFld.filter_column(entities) }}
...
{% for(entity in entites) %}
    ...
    {{ dynFld.value_column(entity) }}