简体   繁体   中英

The show action of Symfony 2 Sonata Admin Bundle is not working

I'm integrating Sonata Admin Bundle into my Symfony 2.6 application by following Symfony 2 jobeet tutorial . Everything is fine except the Show action . I have an entiry "Job" and so I have src/Ibw/JobeetBundle/Admin/JobAdmin.php which have a function configurShowField(ShowMapper $showMapper) like below

<?php
namespace Ibw\JobeetBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;

use Ibw\JobeetBundle\Entity\Job;

class JobAdmin extends Admin
{
    // ....

    protected function configureShowField(ShowMapper $showMapper)
    {
        $showMapper
            ->add('category')
            ->add('type')
            ->add('company')
            ->add('webPath', 'string', array('template' => 'IbwJobeetBundle:JobAdmin:list_image.html.twig'))
            ->add('url')
            ->add('position')
            ->add('location')
            ->add('description')
            ->add('how_to_apply')
            ->add('is_public')
            ->add('is_activated')
            ->add('token')
            ->add('email')
            ->add('expires_at')
        ;
    }

    // ....
}

When I click the Show button and go to the view page (admin/ibw/jobeet/job/xxx/show), it shows none of the fields. The original template processed is /vendor/sonata-project/admin-bundle/Resources/views/CRUD/base_show.html.twig :

{% extends base_template %}

{% block actions %}
    <li>{% include 'SonataAdminBundle:Button:edit_button.html.twig' %}</li>
    <li>{% include 'SonataAdminBundle:Button:history_button.html.twig' %}</li>
    <li>{% include 'SonataAdminBundle:Button:list_button.html.twig' %}</li>
    <li>{% include 'SonataAdminBundle:Button:create_button.html.twig' %}</li>
{% endblock %}

{% block tab_menu %}{{ knp_menu_render(admin.sidemenu(action), {'currentClass' : 'active', 'template': admin_pool.getTemplate('tab_menu_template')}, 'twig') }}{% endblock %}

{% block show %}
    <div class="sonata-ba-view">
        ***
        {{ sonata_block_render_event('sonata.admin.show.top', { 'admin': admin, 'object': object }) }}

        {% for name, view_group in admin.showgroups %}
            <table class="table table-bordered">
                {% if name %}
                    <thead>
                        {% block show_title %}
                            <tr class="sonata-ba-view-title">
                                <th colspan="2">
                                    {{ admin.trans(name) }}
                                </th>
                            </tr>
                        {% endblock %}
                    </thead>
                {% endif %}

                <tbody>
                    {% for field_name in view_group.fields %}
                        {% block show_field %}
                            <tr class="sonata-ba-view-container">
                                {% if elements[field_name] is defined %}
                                    {{ elements[field_name]|render_view_element(object) }}
                                {% endif %}
                            </tr>
                        {% endblock %}
                    {% endfor %}
                </tbody>
            </table>
        {% endfor %}

        {{ sonata_block_render_event('sonata.admin.show.bottom', { 'admin': admin, 'object': object }) }}

    </div>
{% endblock %}

The inner content of <div class="sonata-ba-view"></div> is not shown except three asterisks I printed. Is there any configuration I'm missing?

You're missing as in your function's name.

it's

configureShowFields(FormMapper $formMapper)

not

configureShowField(FormMapper $formMapper)

Try to add call in your admin.yml

ibw_jobbeet.admin.jobadmin:
    class: Ibw\JobeetBundle\Admin\JobAdmin
    ...
    calls:
        - [ setTemplate, ['show', path_to_your_view.html.twig]]

Read more about templates in SonataAdmin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM