Download the PHP package umanit/tree-bundle without Composer
On this page you can find all versions of the php package umanit/tree-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download umanit/tree-bundle
More information about umanit/tree-bundle
Files in umanit/tree-bundle
Package tree-bundle
Short Description Manages content tree in your database
License MIT
Homepage https://github.com/umanit/tree-bundle
Informations about the package tree-bundle
Purpose
The bundle allows you to easily manage content type routing, with the slug, breadcrumb and SEO
General configuration
As the routing for all content is managed by the bundle, you have to register the unique route at the end of your
config/routes.yaml
(in order to not override your custom routes):
Register the bundle to your config/bundles.php
if you don't use Symfony Flex.
Add Gedmo configuration in your services.yaml
or in a separate packages/gedmo.yaml
.
See documentation here
Update your database schema to add our model
or if you use DoctrineMigrationsBundle:
Now, you have to create the root node. The default object is RootEntity, but you can override it in configuration (param
umanit_tree.root_class
).
To create the root node, execute the following command
Create a new node type
You can now easily manage a new node type :
- Create an entity
- Implement
Umanit\TreeBundle\Model\TreeNodeInterface
andUmanit\TreeBundle\Model\SeoInterface
- You can now use the
Umanit\TreeBundle\Model\TreeNodeTrait
andUmanit\TreeBundle\Model\SeoTrait
to have a default implementation for most of the methods to implement
The 4 methods of TreeNodeInterface are :
public function getTreeNodeName(): string;
: returns the node name, used to build a slug of your routepublic function getParents(): array;
: returns the parent objects. For example, if you want a path /category/my-product for "My Product", must returns an array with an object "Category" that implements TreeNodeInterface toopublic function createRootNodeByDefault(): bool;
: if the node has or hasn't parent, should a node be created ? (e.g /my-product)public function getLocale(): ?string
: locale of the object (a locale recognize by$request->getLocale()
of Symfony)
You can manage some SEO options such as title, description and keywords with the Umanit\TreeBundle\Model\SeoInterface
and use the Umanit\TreeBundle\Model\SeoTrait
to automatically add an attribute "seoMetadata" to your entity
Bind a controller and get the entity
In order to bind a controller (or a template) to an entity, you have to configure the key node_types
The entity (or node) will be available in the request attribute.
Create a Parent Node selector
Usage example:
Create SEO Metadata Form
Usage example:
Create a link selector
It's possible to create links to one or more nodes (or external links).
In your entity that will have the link, add a relation with the entity Umanit\TreeBundle\Entity\Link
.
In your forms, you can materialize the relation with the Umanit\TreeBundle\Form\Type\LinkType
. By default, you'll have
two fields, "internal link" (a textfield) and "external link" (a select). By default, the select will be empty. You have
to populate it by giving the models allowed. You can keep only one field with the options : allow_internal: false
or allow_external: false
. Note : only one field can be filled at the same time.
You can define labels with label_internal
and label_external
Usage example :
Events
You can subscribe to some events in order to alter some behaviors
(in order)
umanit.node.before_update
: called before any node save for an entityumanit.node.parent_register
: allows to add/remove parents to an entityumanit.node.updated
: called once an entity saved its nodes and parents
Twig helpers
get_seo_title(default = "", override = false)
get_seo_description(default = "", override = false)
get_seo_keywords(default = "", override = false)
Returns the title, description and keywords of the current document if the route is managed by an entity that implements SeoInterface. Otherwise, the default value (from config) will be used, or the value from "default" parameter if it is set. If you set override to true, the value of the "default" parameter will be always used.
get_breadcrumb(elements = array())
Returns the breadcrumb (array of name/link). It will parse all parents of the current entity if the route is managed by an entity. You can add additional links with the "elements" parameter. An array of name/link.
get_path(object, parentObject = null, root = false, absolute = false, parameters = [)
Returns the route for the given entity (if the entity implements TreeNodeInterface)
get_path_from_node(node, absolute = false, parameters = [])
Returns the route for the given node (instance of Umanit\TreeBundle\Entity\Node
)
get_path_from_link(link)
Returns the path for the given link instance (instance of Umanit\TreeBundle\Entity\Link
).
is_external_link(link)
Returns true if the given link targets an external URL (instance of Umanit\TreeBundle\Entity\Link
).
Using the menu admin
/!\ For performance concerns, we chose to support PostgreSQL only.
Follow those two steps to get started:
1. Create your Menu entity (use annotations or attributes)
2. Configure TreeBundle to use your Menu entity
Usage
Front
TreeBundle doesn't come with a template for the menu. A global twig variable is injected to your site, use it to build your menu template(s).
Example:
Menu admin
A CRUD is provided in order to administrate your menus. It's available on the route tree_admin_menu_dashboard
,
/admin/menu.
/!\ You need to have the role ROLE_TREE_MENU_ADMIN
in order to be able to access the route.
Start by running php bin/console assets:install
to get the assets in your web/public directory.
Customizing the admin layout
The layout can be customized to your needs by setting the admin_layout
configuration value.
Example if you want to use Sonata Admin's layout:
The menu admin has 3 javascript dependencies, you ought to include them as well. Have a look in the default_layout.html.twig.
TreeBundle ships with those assets, you may use them or your own.
Again, if you want to use it with SonataAdmin, configure it as follows:
Customizing the admin form
Let's assume you added an image attribute on your Menu entity and want to use VichUploader to administrate it.
First, Create a form type:
Then add your form type to TreeBundle's configuration:
Configuration reference
All versions of tree-bundle with dependencies
gedmo/doctrine-extensions Version ~3.9
symfony/framework-bundle Version ^5.4|^6.0
doctrine/doctrine-bundle Version ^2.7
doctrine/orm Version ^2.13