Skip to content

Commit

Permalink
feature #152 [WIP] Make config & entity handier (ogizanagi)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch (closes #152).

Discussion
----------

[WIP] Make config & entity handier

Some times ago, someone (I think it was @Pierstoval ?) talked about the fact it was kind of boring to pass `entity` & easyadmin `config` to the twig render method everywhere.
This is one of the feasible solutions, but not THE solution.

In fact, I wasn't able to establish what would be the best option. And as I do not want to give too many clues at first, but instead have your homemade opinions & first thoughts about it, I'll stay fairly general and will only list some of the other solutions, without more explanations:

- Declare **Twig_Globals** from the *Twig Extension* class
- Declare **Twig_Globals** through the *twig configuration* in `config.yml`
- Inject `entity` the same way `config` is currently injected in this PR (within a `kernel.request` listener)
- Do not use **Twig_Globals**, but **Twig_Functions** instead
- Simply inject both entity & config as **Twig_Globals** from `AdminController::initialize()` method

There are many underlying questions behind each of those "solutions". So, what do you think ? 😕

Commits
-------

2db112b [WIP] Make config & entity handier
  • Loading branch information
javiereguiluz committed Mar 10, 2015
2 parents 6d70504 + 2db112b commit 5b0f8b1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
5 changes: 5 additions & 0 deletions Configuration/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,9 @@ private function getFormTypeFromDoctrineType($doctrineType)
? $this->doctrineTypeToFormTypeMap[$doctrineType]
: $doctrineType;
}

public function getBackendConfig()
{
return $this->backendConfig;
}
}
10 changes: 0 additions & 10 deletions Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ protected function listAction()
$paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $this->config['list_max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'));

return $this->render('@EasyAdmin/list.html.twig', array(
'config' => $this->config,
'entity' => $this->entity,
'paginator' => $paginator,
'fields' => $fields,
));
Expand Down Expand Up @@ -160,8 +158,6 @@ protected function editAction()
}

return $this->render('@EasyAdmin/edit.html.twig', array(
'config' => $this->config,
'entity' => $this->entity,
'form' => $editForm->createView(),
'entity_fields' => $fields,
'item' => $item,
Expand All @@ -184,8 +180,6 @@ protected function showAction()
$deleteForm = $this->createDeleteForm($this->entity['name'], $this->request->query->get('id'));

return $this->render('@EasyAdmin/show.html.twig', array(
'config' => $this->config,
'entity' => $this->entity,
'item' => $item,
'fields' => $fields,
'delete_form' => $deleteForm->createView(),
Expand Down Expand Up @@ -215,8 +209,6 @@ protected function newAction()
}

return $this->render('@EasyAdmin/new.html.twig', array(
'config' => $this->config,
'entity' => $this->entity,
'form' => $newForm->createView(),
'entity_fields' => $fields,
'item' => $item,
Expand Down Expand Up @@ -261,8 +253,6 @@ protected function searchAction()
$fields = $this->entity['list']['fields'];

return $this->render('@EasyAdmin/list.html.twig', array(
'config' => $this->config,
'entity' => $this->entity,
'paginator' => $paginator,
'fields' => $fields,
));
Expand Down
9 changes: 5 additions & 4 deletions Resources/views/layout.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% trans_default_domain "EasyAdminBundle" %}
{% set entity = easyadmin_entity(app.request.query.get('entity')) %}
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -16,7 +17,7 @@
<link rel="stylesheet" href="{{ asset('bundles/easyadmin/stylesheet/admin.css') }}">
{% endblock %}

{% for css_asset in config.assets.css %}
{% for css_asset in easyadmin_config('asset.css') %}
<link rel="stylesheet" href="{{ asset(css_asset) }}">
{% endfor %}

Expand All @@ -30,7 +31,7 @@
<div id="header" class="col-lg-2">
<div id="header-contents" class="row">
<div id="header-logo" class="col-xs-6 col-md-2 col-lg-12">
<a href="{{ path('admin') }}">{{ config.site_name|raw }}</a>
<a href="{{ path('admin') }}">{{ easyadmin_config('site_name')|raw }}</a>
</div>

<div id="header-nav" class="col-xs-12 col-md-8 col-lg-12">
Expand All @@ -43,7 +44,7 @@
{% block navigation %}
<ul id="header-menu">
{% block navigation_items %}
{% for item in config.entities %}
{% for item in easyadmin_config('entities') %}
<li class="{{ item.name|lower == app.request.get('entity')|lower ? 'active' : '' }}">
<a href="{{ path('admin', { entity: item.name, action: 'list' }) }}">
{{- item.label|trans -}}
Expand Down Expand Up @@ -103,7 +104,7 @@
<script src="{{ asset('bundles/easyadmin/javascript/bootstrap.min.js') }}"></script>
<script src="{{ asset('bundles/easyadmin/javascript/admin.js') }}"></script>
{% endblock %}
{% for js_asset in config.assets.js %}
{% for js_asset in easyadmin_config('assets.js') %}
<script src="{{ asset(js_asset) }}"></script>
{% endfor %}

Expand Down
2 changes: 1 addition & 1 deletion Resources/views/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
</td>
{% endfor %}
<td class="actions">
{% for action in config['list_actions'] %}
{% for action in easyadmin_config('list_actions') %}
<a href="{{ path('admin', { action: action, entity: entity.name, id: attribute(item, entity.primary_key_field_name) }) }}">
{{- ('action.' ~ action)|trans -}}
</a>
Expand Down
28 changes: 28 additions & 0 deletions Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function getFunctions()
{
return array(
new \Twig_SimpleFunction('entity_field', array($this, 'displayEntityField')),
new \Twig_SimpleFunction('easyadmin_config', array($this, 'getEasyAdminConfig')),
new \Twig_SimpleFunction('easyadmin_entity', array($this, 'getEntity')),
);
}

Expand All @@ -40,6 +42,32 @@ public function getFilters()
);
}

public function getEasyAdminConfig($path = null)
{
$config = $this->configurator->getBackendConfig();

if (!empty($path)) {
$parts = explode('.', $path);

foreach ($parts as $part) {
if (!isset($config[$part])) {
$config = null;
break;
}
$config = $config[$part];
}
}

return $config;
}

public function getEntity($entityName)
{
return null !== $this->getEasyAdminConfig('entities.' . $entityName)
? $this->configurator->getEntityConfiguration($entityName)
: null;
}

public function displayEntityField($entity, array $fieldMetadata)
{
if (!$fieldMetadata['canBeGet']) {
Expand Down

0 comments on commit 5b0f8b1

Please sign in to comment.