-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] [RFC] Embedded crud in forms #6384
Draft
psihius
wants to merge
4
commits into
EasyCorp:4.x
Choose a base branch
from
psihius:embedded-crud
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−2
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5af6acd
Embedded crud
psihius 18a1c90
Fixed condition that determines if embedded template needs to be used
psihius ae1234f
Removed custom CSS for spinner - use standard bootstrap spinner
psihius 5bdcc5d
EmbedField - show list also on detail page (#2)
eminjk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Field; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface; | ||
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\EmbedType; | ||
|
||
final class EmbedField implements FieldInterface | ||
{ | ||
use FieldTrait; | ||
|
||
public static function new(string $propertyName, ?string $label = null): self | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
->setTemplateName('crud/field/embed') | ||
->setFormType(EmbedType::class) | ||
->setFormTypeOption('mapped', false) | ||
->hideWhenCreating() | ||
; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Form\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | ||
|
||
class EmbedType extends AbstractType | ||
{ | ||
public function getBlockPrefix(): string | ||
{ | ||
return 'ea_embedded_collection'; | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return CollectionType::class; | ||
} | ||
} |
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,10 @@ | ||
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #} | ||
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #} | ||
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #} | ||
{% set has_footer = entities|length != 0 %} | ||
{% set has_batch_actions = false %} | ||
{% set some_results_are_hidden = false %} | ||
{% set sort_field_name = app.request.get('sort')|keys|first %} | ||
{% set sort_order = app.request.get('sort')|first %} | ||
|
||
{{ block("content", "@EasyAdmin/crud/index.html.twig") }} |
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,107 @@ | ||
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #} | ||
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #} | ||
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #} | ||
{% set current_url = ea_url() %} | ||
{% if form is defined %} | ||
{% set field = form.vars.ea_vars.field %} | ||
{% set entity = form.vars.ea_vars.entity %} | ||
{% endif %} | ||
{% set target_entity = field.doctrineMetadata.get('targetEntity') %} | ||
{% set crudControllers = ea.crudControllers %} | ||
{% set target_entity_crud_fqcn = crudControllers.findCrudFqcnByEntityFqcn(target_entity) %} | ||
|
||
{% set url = ea_url().unsetAll().setController(target_entity_crud_fqcn).setAction('index').set('embedContext', { | ||
mappedBy: field.doctrineMetadata.get('mappedBy'), | ||
embeddedIn: entity.primaryKeyValue | ||
}) %} | ||
|
||
{% set id_suffix = '-'~field.property %} | ||
|
||
<div id="embed{{ id_suffix }}" class="position-relative"> | ||
<div class="d-flex justify-content-center d-none spinner"> | ||
<div class="spinner-border m-5" role="status"> | ||
<span class="sr-only"></span> | ||
</div> | ||
</div> | ||
<div class="embed-content"></div> | ||
</div> | ||
|
||
<script> | ||
window.addEventListener('load', () => { | ||
const referrer = window.location.href; | ||
const initialUrl = '{{ url|raw }}' | ||
|
||
const embed = document.querySelector('#embed{{ id_suffix }}'); | ||
const embedContent = embed.querySelector('.embed-content'); | ||
const spinner = embed.querySelector('.spinner'); | ||
|
||
const load = (url) => { | ||
spinner.classList.remove('d-none'); | ||
fetch(url) | ||
.then(it => it.text()) | ||
.then(it => embedContent.innerHTML = it) | ||
.then(() => { | ||
// override referrer of actions, so we get back to the "main" view afterwards, not the embed | ||
embedContent.querySelectorAll('.actions a').forEach(action => { | ||
const target = new URL(action.href) | ||
target.searchParams.set('referrer', referrer); | ||
action.href = target.toString(); | ||
}); | ||
|
||
// override referrer of actions, so we get back to the "main" view afterwards, not the embed | ||
embedContent.querySelectorAll('.global-actions a').forEach(action => { | ||
const target = new URL(action.href) | ||
const current = new URL(referrer); | ||
target.searchParams.set('relatedEntityId', current.searchParams.get('entityId')); | ||
target.searchParams.set('referrer', referrer); | ||
action.href = target.toString(); | ||
}) | ||
|
||
embedContent.querySelectorAll('.action-delete').forEach((actionElement) => { | ||
actionElement.addEventListener('click', (event) => { | ||
event.preventDefault(); | ||
|
||
document.querySelector('#modal-delete-button').addEventListener('click', () => { | ||
const deleteFormAction = new URL(actionElement.getAttribute('formaction')); | ||
const deleteForm = document.querySelector('#delete-form'); | ||
deleteFormAction.searchParams.set('referrer', referrer); | ||
deleteForm.setAttribute('action', deleteFormAction.toString()); | ||
deleteForm.submit(); | ||
}); | ||
}); | ||
}); | ||
|
||
// intercept sort and pagination | ||
embedContent.querySelectorAll('thead a, .pagination a').forEach(link => { | ||
link.addEventListener('click', evt => { | ||
evt.preventDefault(); | ||
load(link.href) | ||
}) | ||
}) | ||
|
||
// intercept search | ||
// embedContent.querySelector('.form-action-search form').addEventListener('submit', evt => { | ||
// evt.preventDefault(); | ||
// const data = new FormData(evt.target); | ||
// const params = new URLSearchParams(data).toString() | ||
// const target = new URL(url); | ||
// target.search = params.toString(); | ||
// load(target.toString()) | ||
// }) | ||
|
||
// highlight results | ||
// const searchQuery = new URL(url).searchParams.get('query'); | ||
// if(searchQuery) { | ||
// $(embedContent).find('table tbody td:not(.actions)').highlight($.merge([searchQuery], searchQuery.split(' '))); | ||
// } | ||
|
||
// can be used to re-initialize dynamic content | ||
document.dispatchEvent(new Event('ea.embed.content-loaded')) | ||
}) | ||
.finally(() => spinner.classList.add('d-none')) | ||
; | ||
} | ||
|
||
load(initialUrl); | ||
}) | ||
</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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this into a separate method to keep the index action method simple to understand?
Not sur of what's the best between creating a new
createEmbededIndexQueryBuilder()
or something likefilterEmbededQueryBuilder()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving it is no problem, as I said - this is a rough draft that needs lots of work :)
As for the second point - maybe creating dedicated
embedded(index|detail|edit|new|delete)
actions or even rolling aEmbeddedAbstractController
might be a better way of doing it, but then you have to maintain similar code. So the question is - what would be a better approach to refactor here so it is better long term? Those action methods already are pretty sizeable and adding to them might be a bit overkill :)