diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2018-07-21 23:52:44 +0200 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2018-07-21 23:52:44 +0200 |
commit | 748b7bf7c5c4df2d546ae375d9c13b5657e431df (patch) | |
tree | 35ac11de286bdfe4b6e243e2eb05b52992b6f420 /fai_gestion/plugins | |
parent | 38fc194ababe7e1876a27697a9beb6d21623dcd6 (diff) | |
download | chd_gestion-748b7bf7c5c4df2d546ae375d9c13b5657e431df.zip chd_gestion-748b7bf7c5c4df2d546ae375d9c13b5657e431df.tar.gz chd_gestion-748b7bf7c5c4df2d546ae375d9c13b5657e431df.tar.bz2 |
bake: implement virtual entity titles
Diffstat (limited to 'fai_gestion/plugins')
-rw-r--r-- | fai_gestion/plugins/CustomTheme/config/bootstrap.php | 31 | ||||
-rw-r--r-- | fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig | 22 |
2 files changed, 52 insertions, 1 deletions
diff --git a/fai_gestion/plugins/CustomTheme/config/bootstrap.php b/fai_gestion/plugins/CustomTheme/config/bootstrap.php index 7215ce7..351e582 100644 --- a/fai_gestion/plugins/CustomTheme/config/bootstrap.php +++ b/fai_gestion/plugins/CustomTheme/config/bootstrap.php @@ -20,6 +20,7 @@ **/ use Cake\Event\Event; use Cake\Event\EventManager; +use Cake\Utility\Inflector; // https://book.cakephp.org/3.0/en/bake/development.html @@ -52,6 +53,36 @@ EventManager::instance()->on('Bake.beforeRender.Model.table', function (Event $e if ( $view->BakeExtra->hasFilters($controllerName) ) { $view->viewVars['behaviors'] += ['Search.Search' => [] ]; } + $titleOpts = $view->BakeExtra->getTitleOpts($controllerName); + // Force displayField to be the custom title if any defined in bake_extra.php + if ( is_array($titleOpts) ) { + $view->viewVars['displayField'] = 'title'; + } +}); + +EventManager::instance()->on('Bake.beforeRender.Model.entity', function (Event $event) { + $view = $event->getSubject(); + // Set template variables for virtual title if any defined in bake_extra.php + $controllerName = Inflector::pluralize($view->viewVars['name']); + $titleOpts = $view->BakeExtra->getTitleOpts($controllerName); + + // viewVars are kept from one entity to another (Cake bug ?) + unset($view->viewVars['virtualTitleGlue']); + unset($view->viewVars['virtualTitlePieces']); + unset($view->viewVars['virtualTitleCustomCode']); + + if ( is_array($titleOpts) ) { + foreach ($titleOpts as $k => $v) { + // For pieces array, prefix each item to have '$this->col_name' + if ( $k === 'pieces' && is_array($v) ) { + $v = preg_filter('/^/', '$this->', $v); + } + // Prefix each key (less view namespace problems and get more explcit in .twig + $k = 'virtualTitle'.Inflector::camelize($k); + // Export options to the view + $view->viewVars[$k] = $v; + } + } }); /* * Note : "cake bake all" behavior is customized in diff --git a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig index aaa15e5..527a30b 100644 --- a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig +++ b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig @@ -76,7 +76,27 @@ class {{ name }} extends Entity protected $_hidden = [{{ Bake.stringifyList(hidden)|raw }}]; {% endif %} -{%- if not accessible and not hidden %} +{%- if virtualTitleCustomCode or virtualTitlePieces %} + /** + * Virtual fields to be exposed (serialized) + */ + protected $_virtual = ['title']; + + /** + * Virtual field for pretty print in related table's views + * + * @return String + */ + protected function _getTitle() + { +{%- if virtualTitleCustomCode %} + +{{ virtualTitleCustomCode|raw }} +{% else %} + + return "{{ virtualTitlePrefix }}" . implode("{{ virtualTitleGlue??' ' }}", [{{ Bake.stringifyList(virtualTitlePieces, { indent:3, quotes: false } )|raw }}]); +{% endif %} + } {% endif %} } |