Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
330 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/Form/Type/Extension/EasyAdminAutocompleteTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace AlterPHP\EasyAdminExtensionBundle\Form\Type\Extension; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\EasyAdminAutocompleteType; | ||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
class EasyAdminAutocompleteTypeExtension extends AbstractTypeExtension | ||
{ | ||
private $router; | ||
private $translator; | ||
|
||
public function __construct(RouterInterface $router, TranslatorInterface $translator) | ||
{ | ||
$this->router = $router; | ||
$this->translator = $translator; | ||
} | ||
|
||
public function getExtendedType() | ||
{ | ||
return EasyAdminAutocompleteType::class; | ||
} | ||
|
||
public function buildView(FormView $view, FormInterface $form, array $options) | ||
{ | ||
if (isset($options['attr']['create']) && $options['attr']['create']) { | ||
$view->vars['attr']['data-easyadmin-autocomplete-create-action-url'] = $this->router->generate( | ||
'easyadmin', array('action' => 'newAjax', 'entity' => $view->vars['autocomplete_entity_name']) | ||
); | ||
$view->vars['attr']['data-easyadmin-autocomplete-create-field-name'] = $view->vars['name']; | ||
$view->vars['attr']['data-easyadmin-autocomplete-create-button-text'] = $this->translator->trans( | ||
'action.add_new_item', array(), 'EasyAdminBundle' | ||
); | ||
|
||
unset($view->vars['attr']['create']); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
function createAutoCompleteCreateFields() { | ||
var autocompleteCreateFields = $('[data-easyadmin-autocomplete-create-action-url]'); | ||
|
||
autocompleteCreateFields.each(function () { | ||
var $this = $(this), | ||
url = $this.data('easyadmin-autocomplete-url'), | ||
url_action = $this.data('easyadmin-autocomplete-create-action-url'), | ||
field_name = $this.data('easyadmin-autocomplete-create-field-name'), | ||
button_text = $this.data('easyadmin-autocomplete-create-button-text'), | ||
select_id = $this.attr('id'); | ||
|
||
$this.select2({ | ||
theme: 'bootstrap', | ||
ajax: { | ||
url: url, | ||
dataType: 'json', | ||
delay: 250, | ||
data: function (params) { | ||
return { 'query': params.term, 'page': params.page }; | ||
}, | ||
// to indicate that infinite scrolling can be used | ||
processResults: function (data, params) { | ||
return { | ||
results: data.results, | ||
pagination: { | ||
more: data.has_next_page | ||
} | ||
}; | ||
}, | ||
cache: true | ||
}, | ||
placeholder: '', | ||
allowClear: true, | ||
minimumInputLength: 1, | ||
language: { | ||
noResults: function () { | ||
return '<a href="#" class="btn btn-info" onclick="switchToEntityCreation(\''+url_action+'\', \''+select_id+'\', \''+field_name+'\');return false;">'+button_text+' '+field_name+'</a>'; | ||
} | ||
}, | ||
escapeMarkup: function (markup) { | ||
return markup; | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function switchToEntityCreation(url_action, select_id, field_name) { | ||
$('#'+select_id).select2('close'); | ||
$.ajax({ | ||
url : url_action, | ||
type: 'GET', | ||
success: function(data) { | ||
openCreateEntityModal(data, url_action, field_name, select_id); | ||
$('#create-entity-modal').modal({ backdrop: true, keyboard: true }); | ||
} | ||
}); | ||
} | ||
|
||
function openCreateEntityModal(data, url_action, field_name, select_id) { | ||
$('#create-entity-modal .modal-body').html(data.html); | ||
$('form[name="'+field_name+'"]').attr('action', url_action); | ||
initCreateEntityAjaxForm(field_name, select_id); | ||
} | ||
|
||
function initCreateEntityAjaxForm(field_name, select_id) { | ||
$('form[name="'+field_name+'"]').submit(function( event ) { | ||
event.preventDefault(); | ||
var url_action = $(this).attr('action'); | ||
$.ajax({ | ||
url: url_action, | ||
type: $(this).attr('method'), | ||
data: $(this).serialize(), | ||
cache: false, | ||
contentType: false, | ||
processData: false, | ||
success: function(data) { | ||
if (data.hasOwnProperty('option')) { | ||
$('#create-entity-modal').modal('hide'); | ||
var newOption = new Option(data.option.text, data.option.id, true, true); | ||
$('#'+select_id).append(newOption).trigger('change'); | ||
// manually trigger the `select2:select` event | ||
$('#'+select_id).trigger({ | ||
type: 'select2:select', | ||
params: { data: data.option } | ||
}); | ||
} | ||
if (data.hasOwnProperty('html')) { | ||
openCreateEntityModal(data, url_action, field_name, select_id); | ||
} | ||
}, | ||
error: function(error){ | ||
console.log(error); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
$(function () { | ||
createAutoCompleteCreateFields(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{% extends '@BaseEasyAdmin/default/new.html.twig' %} | ||
|
||
{% block head_stylesheets %} | ||
<style> | ||
#create-entity-modal .main-sidebar { | ||
display: none !important; | ||
} | ||
#create-entity-modal .content-wrapper, | ||
#create-entity-modal .main-footer { | ||
margin-left: 0 !important; | ||
} | ||
</style> | ||
{% endblock %} | ||
|
||
{% block head_favicon %}{% endblock %} | ||
{% block head_javascript %}{% endblock %} | ||
{% block header %}{% endblock %} | ||
{% block sidebar %}{% endblock %} | ||
{% block confirm_modal %}{% endblock %} | ||
{% block create_entity_modal %}{% endblock %} | ||
{% block body_javascript %} | ||
{% set _select2_locales = ['ar', 'az', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'gl', 'he', 'hi', 'hr', 'hu', 'id', 'is', 'it', 'ja', 'km', 'ko', 'lt', 'lv', 'mk', 'ms', 'nb', 'nl', 'pl', 'pt-BR', 'pt', 'ro', 'ru', 'sk', 'sr-Cyrl', 'sr', 'sv', 'th', 'tr', 'uk', 'vi', 'zh-CN', 'zh-TW'] %} | ||
{% set _app_language = app.request.locale|split('-')|first|split('_')|first %} | ||
{% set _select2_locale = app.request.locale in _select2_locales | ||
? app.request.locale | ||
: _app_language in _select2_locales ? _app_language : 'en' | ||
%} | ||
|
||
<script src="{{ asset('bundles/easyadmin/javascript/select2/i18n/' ~ _select2_locale ~ '.js') }}"></script> | ||
|
||
<script type="text/javascript"> | ||
$(function() { | ||
// Select2 widget is only enabled for the <select> elements which | ||
// explicitly ask for it | ||
$('#create-entity-modal').find('form select[data-widget="select2"]').select2({ | ||
theme: 'bootstrap', | ||
language: '{{ _select2_locale }}' | ||
}); | ||
}); | ||
</script> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div id="create-entity-modal" class="modal fade"> | ||
<div class="modal-dialog"> | ||
<div class="modal-content"> | ||
<div class="modal-body"></div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<script src="{{ asset('bundles/easyadminextension/js/autocomplete-create.js') }}"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace AlterPHP\EasyAdminExtensionBundle\Tests\Controller; | ||
|
||
use AlterPHP\EasyAdminExtensionBundle\Tests\Fixtures\AbstractTestCase; | ||
|
||
class AutocompleteAddTest extends AbstractTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->initClient(array('environment' => 'autocomplete_add')); | ||
} | ||
|
||
public function testNewEntityAutocompleteModal() | ||
{ | ||
$crawler = $this->requestNewView('Product'); | ||
$this->assertSame(1, $crawler->filter('select#product_category_autocomplete[data-easyadmin-autocomplete-create-action-url="/admin/?action=newAjax&entity=Category"]')->count()); | ||
|
||
$crawlerAjax = $this->requestNewAjaxView('Category'); | ||
$form = $crawlerAjax->filter('form[name=category]')->form( | ||
['category[name]' => 'New Ajax Category'] | ||
); | ||
$crawlerAjax = $this->client->submit($form); | ||
$this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
imports: | ||
- { resource: config.yml } | ||
|
||
easy_admin: | ||
entities: | ||
Product: | ||
class: AppTestBundle\Entity\FunctionalTests\Product | ||
form: | ||
fields: | ||
- { property: 'category', type: 'easyadmin_autocomplete', type_options: { attr: { create: true } } } | ||
Category: | ||
class: AppTestBundle\Entity\FunctionalTests\Category | ||
form: | ||
fields: | ||
- name |