2012-07-16 6 views
7

Có thể ghi đè lên mẫu cho loại biểu mẫu: "sonata_type_collection" không?Gói quản trị Sonata - Loại biểu mẫu: sonata_type_collection - mẫu tùy chỉnh?

Ive đã cố gắng cùng những dòng này:

$formMapper->add('slides', 'sonata_type_collection', array(), array(
       'edit' => 'inline', 
       'inline' => 'table', 
       'sortable' => 'priority', 
       'template' => 'MyBundle:Form:slides.admin.html.twig' 
      )); 

nhưng vô ích.

Tôi biết tôi có thể ghi đè lên toàn bộ mẫu, nhưng tôi chỉ muốn làm mẫu cho biểu mẫu này, không phải tất cả những nơi tôi sử dụng loại biểu mẫu này.

Có ai biết nếu điều này là có thể không?

Cảm ơn

Trả lời

18

Tôi tìm thấy một chút vĩ đại của mã trong /vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/Extension/Field/Type/FormTypeFieldExtension.php mà thực sự thiết lập một loạt các loại để đính kèm vào xem hình thức mà nó sử dụng để ưu tiên cho khối cành lá rendering: (dòng 99-105)

// add a new block types, so the Admin Form element can be tweaked based on the admin code 
     $types = $view->getVar('types'); 
     $baseName = str_replace('.', '_', $sonataAdmin['field_description']->getAdmin()->getCode()); 
     $baseType = $types[count($types) - 1]; 

     $types[] = sprintf('%s_%s', $baseName, $baseType); 
     $types[] = sprintf('%s_%s_%s', $baseName, $sonataAdmin['field_description']->getName(), $baseType); 

Do đó tất cả những gì phải làm là xác định một khối gọi là mycompany_admin_content_galleries_sonata_type_collection_widget hoặc mycompany_admin_content_galleries_slides_sonata_type_collection_widget và nó chỉ áp dụng cho hình thức quản trị này :)

để hoàn thành giải pháp này trong lớp học quản lý của tôi, tôi thêm functio này n:

public function getFormTheme() 
{ 
    return array_merge(
     parent::getFormTheme(), 
     array('MyBundle:Gallery:admin.slides.html.twig') 
    ); 
} 

và tôi đã tạo MyBundle/Resources/views/Gallery/admin.slides.html.twig, chứa những điều sau đây:

{% use 'SonataAdminBundle:Form:form_admin_fields.html.twig' %} // I think this 
      line is not really needed as the base admin's form theme uses this file 

{% block my_bundle_content_pages_slides_sonata_type_collection_widget %} 

    // copied and edited the contents of Sonata/DoctrineORMAdminBundle/Resources/views/CRUD/edit_orm_one_to_many.html.twig 

{% endblock %} 
+0

Chỉ cần hoàn thành câu trả lời của bạn, bạn có thể đăng ký các file mẫu của bạn với cành lá, do đó bạn không cần phải nhập cụm từ đó trong quản trị lớp học với "getFormTheme()", xem: http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html#creating-a-template-for-the-field – Cassiano

+0

Có nơi bạn đã đề cập "để xác định màu đen được gọi là ... "giống như bước cuối cùng nơi bạn tạo một khối trong admin.slides.html.twig ?? Hoặc bạn xác định khối đó ở đâu? – Mentos93