Download the PHP package tetranz/select2entity-bundle without Composer

On this page you can find all versions of the php package tetranz/select2entity-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package select2entity-bundle

select2entity-bundle

Introduction

This is a Symfony bundle which enables the popular Select2 component to be used as a drop-in replacement for a standard entity field on a Symfony form.

It works with Symfony 4 and 5. For Symfony 2 and 3, please use version or 2.x of the bundle. For Select2 4.0 and above. For older versions, use version 1.x of the bundle (not compatible with Symfony 5).

The main feature that this bundle provides compared with the standard Symfony entity field (rendered with a html select) is that the list is retrieved via a remote ajax call. This means that the list can be of almost unlimited size. The only limitation is the performance of the database query or whatever that retrieves the data in the remote web service.

It works with both single and multiple selections. If the form is editing a Symfony entity then these modes correspond with many to one and many to many relationships. In multiple mode, most people find the Select2 user interface easier to use than a standard select tag with multiple=true with involves awkward use of the ctrl key etc.

The project was inspired by lifo/typeahead-bundle which uses the Typeahead component in Bootstrap 2 to provide similar functionality. Select2Entity can be used anywhere Select2 can be installed, including Bootstrap 3.

Thanks to @ismailbaskin we now have Select2 version 4 compatibility.

Screenshots

This is a form with a single selection field list expanded.

This is a form with a multiple selection field list expanded.

Installation

Select2 must be installed and working first. I hope to setup a demo site but my setup is basically BraincraftedBootstrapBundle with Select2 installed for Bootstrap 3. Once the Braincrafted bundle is working, the only files I've needed to install are:

select2.js, select2.css from https://github.com/select2/select2/tree/4.0.0

select2-bootstrap.css from https://github.com/t0m/select2-bootstrap-css/tree/bootstrap3. That gets it working for Bootstrap 3.

These files live in the Resources/public/js and Resources/public/css folders of one of my bundles and then included in my main layout.html.twig file.

Alternatively, minified versions of select2.js and select2.css can be loaded from the CloudFlare CDN using the two lines of code given here: https://select2.github.io. Make sure the script tag comes after where jQuery is loaded. That might be in the page footer.

Note that this only works with Select2 version 4. If you are using Select2 version 3.X please use "tetranz/select2entity-bundle": "1.*" in composer.json

How to use

The following is for Symfony 4. See https://github.com/tetranz/select2entity-bundle/tree/v2.1 for Symfony 2/3 configuration and use.

Select2Entity is simple to use. In the buildForm method of a form type class, specify Select2EntityType::class as the type where you would otherwise use entity:class.

Here's an example:

Put this at the top of the file with the form type class:

Options

Defaults will be used for some if not set.

The url of the remote query can be given by either of two ways: remote_route is the Symfony route. remote_params can be optionally specified to provide parameters. Alternatively, remote_path can be used to specify the url directly.

You may use query_parameters for when those remote_params have to be changeable dynamically. You may change them using $('#elem').data('query-parameters', { / new params / });

The defaults can be changed in your config/packages/tetranzselect2entity.yaml file with the following format.

AJAX Response

The controller should return a JSON array in the following format. The properties must be id and text.

Infinite Scrolling

If your results are being paged via the Select2 "infinite scrolling" feature then you can either continue to return the same array as shown above (for Backwards Compatibility this bundle will automatically try to determine if more results are needed), or you can return an object shown below to have finer control over the paged results.

The more field should be true if there are more results to be loaded.

Your controller action that fetches the results will receive a parameter page indicating what page of results should be loaded. If you set scroll to true then you must handle the page parameter in the query. Weird things will happen if you don't.

Custom option text

If you need more flexibility in what you display as the text for each option, such as displaying the values of several fields from your entity or showing an image inside, you may define your own custom transformer. Your transformer must implement DataTransformerInterface. The easiest way is probably to extend EntityToPropertyTransformer or EntitiesToPropertyTransformer and redefine the transform() method. This way you can return as text anything you want, not just one entity property.

Here's an example that returns the country name and continent (two different properties in the Country entity):

In transform sets data array like this:

Your custom transformer and respectively your Ajax controller should return an array in the following format:

If you are using the allow_add option and your entity requires other fields besides the text_property field to be valid, you will either need to extend the EntitiesToPropertyTransformer to add the missing field, create a doctrine prePersist listener, or add the missing data in the form view after submit before saving.

Add New Tags

If you want to be able to create new entities through Select2 tags, you can enable it using the allow_add set of options.

For example:

A few things to keep in mind when adding tags:

Including other field values in request

If you need to include other field values because the selection depends on it you can add the req_params option. The key is the name of the parameter in the query string, the value is the path in the FormView (if you don't know the path you can do something like {{ dump(form) }} in your template.)

The property option refers to your entity field which is used for the label as well as for the search term.

In the callback you get the QueryBuilder to modify the result query and the data object as parameter (data can be a simple Request object or a plain array. See AutocompleteService.php for more details).

Because the handling of requests is usually very similar you can use a service which helps you with your results. To use it just add a route in one of your controllers:

Templating

General templating has now been added to the bundle. If you need to render html code inside your selection results, set the render_html option to true and in your controller return data like this:

If you need further templating, you'll need to override the .select2entity() method as follows. If you need [Templating](https://select2.org/dropdown#templating) in Select2, you could consider the following example that shows the country flag next to each option. Your custom transformer should return data like this: You need to define your own JavaScript function `select2entityAjax` which extends the original one `select2entity` and display custom template with image: This script will add the functionality globally for all elements with class `select2entity`, but if the `img` is not passed it will work as the original `select2entity`. You should add a `'autostart' => false` to form to run properly JS code. `` You also will need to override the following block in your template: This block adds all additional data needed to the JavaScript function `select2entityAjax`, like data attribute. In this case we are passing `data-img`.

Themes

Select2 supports custom themes using the theme option so you can style Select2 to match the rest of your application. For Bootstrap4 theme look at https://github.com/ttskch/select2-bootstrap4-theme

Embed Collection Forms

If you use Embedded Collection Forms and data-prototype to add new elements in your form, you will need the following JavaScript that will listen for adding an element .select2entity:


All versions of select2entity-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.3
doctrine/orm Version >=2.4
twig/twig Version >=2.9
symfony/property-access Version >=4.0
symfony/dependency-injection Version >=4.0
symfony/http-kernel Version >=4.0
symfony/form Version >=4.0
symfony/config Version >=4.0
symfony/routing Version >=4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package tetranz/select2entity-bundle contains the following files

Loading the files please wait ....