Developed with love by KnpLabs Hire us for your project!
15

visualize-entity-bundle

by adrenalinkin

Provides possibility for visualize entity data by YAML configuration

Visualize Entity Bundle На Русском

Introduction

Bundle provides possibility for visualize entity data by yaml configuration.

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable
version of this bundle:
text
composer require adrenalinkin/visualize-entity-bundle

This command requires you to have Composer install globally.

Step 2: Enable the Bundle

Then, enable the bundle by updating your app/AppKernel.php file to enable the bundle:

<?php
// app/AppKernel.php

class AppKernel extends Kernel
{
    // ...

    public function registerBundles()
    {
        $bundles = [
            // ...

            new Linkin\Bundle\EntityHelperBundle\LinkinEntityHelperBundle(),
            new Linkin\Bundle\VisualizeEntityBundle\LinkinVisualizeEntityBundle(),
        ];

        return $bundles;
    }

    // ...
}

Configuration

To start using bundle you need to create configuration file in the needed bundle of the your project.
Simple configuration for the user entity for displaying 3 fields:

linkin_visualize_entity:
    acme_user_default:
        className:    AcmeBundle:User
        fields:
            username:   ~
            firstName:  ~
            email:      ~

More information about configuration file in the part
Configuration file visualize_entity.yml.

Usage

Usage example expect using simple configuration from the previous part.

Controller

To apply simple user configuration, which has been created early, you just need to call
linkin_visualize_entity.builder service and call method buildViewEntity to create special object.
You need put name of the configuration as first parameter and entity object or entity identity as second.

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
 * @Route("acme_user")
 */
class AcmeController extends Controller
{
    /**
     * @Route("/user_view/{id}", name="acme_user_view")
     *
     * @Method("GET")
     *
     * @Template()
     *
     * @param int $id
     *
     * @return array
     */
    public function viewAction($id)
    {
        $viewEntity = $this->get('linkin_visualize_entity.builder')->buildViewEntity('acme_user_default', $id);

        return [
            'viewEntity' => $viewEntity,
        ];
    }
}

TWIG Template

ViewEntity object allow several opportunities for work in twig-template.
Most simple and most popular method notDisplayed - will display all fields which was not displayed yet.

    {# @var viewEntity \Linkin\Bundle\VisualizeEntityBundle\Entity\ViewEntity #}

    <table class="striped">
        <tbody>
        {% for fieldData in viewEntity.notDisplayed %}
            <tr>
                <td><b>{{ fieldData.label }}:</b></td>
                <td>{{ fieldData.value|raw }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>

When you need display some fields in the specific place or just take specific field you should use fieldData method.
Next call of the notDisplayed method returns all data except already displayed by fieldData method call.

    {# @var viewEntity \Linkin\Bundle\VisualizeEntityBundle\Entity\ViewEntity #}

    <table class="striped">
        <thead>
        <th>
            <td>{{ viewEntity.fieldData('username').label }}</td>
            <td>{{ viewEntity.fieldData('username').value|raw }}</td>
        </th>
        </thead>
        <tbody>
        {% for fieldData in viewEntity.notDisplayed %}
            <tr>
                <td><b>{{ fieldData.label }}:</b></td>
                <td>{{ fieldData.value|raw }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>

Also you can use other advanced methods for more specific situations:
* entity - retrieve original entity object.
* entityId - retrieve original entity identity.
* all - returns all configuration-based fields regardless is field has been already displayed or not. Also you can
set list of the fields which should be excluded from the result.
* alreadyDisplayed - returns list of the all fields, which already has been displayed.
* resetDisplayed - reset statistics of the already has been displayed fields.

Visualizers

Visualization of the different data types is handled by visualizers. You can get information about all predefined
visualizers in the part Predefined visualizers.
More information about registration own visualizer in the part
Add new visualizer.

Dependencies

License

license

Copyright (c) Viktor Linkin since 2014

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
linkin_visualize_entity:

# Prototype
configName:
className: ~ # Required
label_translation:
domain: ~
prefix: ~
fields:

# Prototype
fieldName:

# Getter for get field value
getter: ~

# Label for the field
label: ~

# Name of the visualizer for the field
visualizer: ~

# Name of the visualizer for the field
visualizer_options:

# Prototype
optionName: []