Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Command/ControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@ public function bake(string $controllerName, Arguments $args, ConsoleIo $io): vo

$currentModelName = $controllerName;
$plugin = $this->plugin;
if ($plugin) {
$plugin .= '.';
$pluginPath = $plugin;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the process of simplifying I also found a bug: A dot added to plugin is passed along to the templates.
This is now also fixed as a side effect.

if ($pluginPath) {
$pluginPath .= '.';
}

if ($this->getTableLocator()->exists($plugin . $currentModelName)) {
$modelObj = $this->getTableLocator()->get($plugin . $currentModelName);
if ($this->getTableLocator()->exists($pluginPath . $currentModelName)) {
$modelObj = $this->getTableLocator()->get($pluginPath . $currentModelName);
} else {
$modelObj = $this->getTableLocator()->get($plugin . $currentModelName, [
$modelObj = $this->getTableLocator()->get($pluginPath . $currentModelName, [
'connectionName' => $this->connection,
]);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Command/TemplateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ public function getContent(Arguments $args, ConsoleIo $io, string $action, ?arra
}
$renderer->set('indexColumns', $indexColumns);

// Always use domain translations when in plugin context
$useDomain = (bool)$this->plugin;
$renderer->set('useDomain', $useDomain);

return $renderer->generate("Bake.Template/$action");
}

Expand Down
24 changes: 12 additions & 12 deletions templates/bake/Template/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
?>
<div class="{{ pluralVar }} index content">
{% set fields = Bake.filterFields(fields, schema, modelObject, indexColumns, ['binary', 'text']) %}
<?= $this->Html->link(__('New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'button float-right']) ?>
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'button float-right']) ?>
{% set done = [] %}
<h3><?= __('{{ pluralHumanName }}') ?></h3>
<h3><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ pluralHumanName }}') ?></h3>
<div class="table-responsive">
<table>
<thead>
<tr>
{% for field in fields %}
<th><?= $this->Paginator->sort('{{ field }}') ?></th>
{% endfor %}
<th class="actions"><?= __('Actions') ?></th>
<th class="actions"><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Actions') ?></th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -63,14 +63,14 @@
{% endfor %}
{% set pk = '$' ~ singularVar ~ '->' ~ primaryKey[0] %}
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', {{ pk|raw }}]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', {{ pk|raw }}]) ?>
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'View'), ['action' => 'view', {{ pk|raw }}]) ?>
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Edit'), ['action' => 'edit', {{ pk|raw }}]) ?>
<?= $this->Form->postLink(
__('Delete'),
{% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Delete'),
['action' => 'delete', {{ pk|raw }}],
[
'method' => 'delete',
'confirm' => __('Are you sure you want to delete # {0}?', {{ pk|raw }}),
'confirm' => {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Are you sure you want to delete # {0}?', {{ pk|raw }}),
]
) ?>
</td>
Expand All @@ -81,12 +81,12 @@
</div>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->first('<< ' . {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'first')) ?>
<?= $this->Paginator->prev('< ' . {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
<?= $this->Paginator->next({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'next') . ' >') ?>
<?= $this->Paginator->last({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(__('Page {{ '{{' }}page{{ '}}' }} of {{ '{{' }}pages{{ '}}' }}, showing {{ '{{' }}current{{ '}}' }} record(s) out of {{ '{{' }}count{{ '}}' }} total')) ?></p>
<p><?= $this->Paginator->counter({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Page {{ '{{' }}page{{ '}}' }} of {{ '{{' }}pages{{ '}}' }}, showing {{ '{{' }}current{{ '}}' }} record(s) out of {{ '{{' }}count{{ '}}' }} total')) ?></p>
</div>
</div>
4 changes: 2 additions & 2 deletions templates/bake/Template/login.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<div class="{{ pluralVar }} form content">
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Please enter your username and password') ?></legend>
<legend><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Please enter your username and password') ?></legend>
<?= $this->Form->control('username') ?>
<?= $this->Form->control('password') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->button({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Login')); ?>
<?= $this->Form->end() ?>
</div>
47 changes: 24 additions & 23 deletions templates/bake/Template/view.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Html->link(__('Edit {{ singularHumanName }}'), ['action' => 'edit', {{ pK|raw }}], ['class' => 'side-nav-item']) ?>
<?= $this->Form->postLink(__('Delete {{ singularHumanName }}'), ['action' => 'delete', {{ pK|raw }}], ['confirm' => __('Are you sure you want to delete # {0}?', {{ pK|raw }}), 'class' => 'side-nav-item']) ?>
<?= $this->Html->link(__('List {{ pluralHumanName }}'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
<?= $this->Html->link(__('New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'side-nav-item']) ?>
<h4 class="heading"><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Actions') ?></h4>
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Edit {{ singularHumanName }}'), ['action' => 'edit', {{ pK|raw }}], ['class' => 'side-nav-item']) ?>
<?= $this->Form->postLink({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Delete {{ singularHumanName }}'), ['action' => 'delete', {{ pK|raw }}], ['confirm' => {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Are you sure you want to delete # {0}?', {{ pK|raw }}), 'class' => 'side-nav-item']) ?>
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'List {{ pluralHumanName }}'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'side-nav-item']) ?>
{% set done = [] %}
</div>
</aside>
<div class="column column-80">
Expand All @@ -44,12 +45,12 @@
{%~ if associationFields[field] is defined %}
{%~ set details = associationFields[field] %}
<tr>
<th><?= __('{{ details.property|humanize }}') ?></th>
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ details.property|humanize }}') ?></th>
<td><?= ${{ singularVar }}->hasValue('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?></td>
</tr>
{%~ else %}
<tr>
<th><?= __('{{ field|humanize }}') ?></th>
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
<td><?= h(${{ singularVar }}->{{ field }}) ?></td>
</tr>
{%~ endif %}
Expand All @@ -58,17 +59,17 @@
{% if associations.HasOne %}
{%~ for alias, details in associations.HasOne %}
<tr>
<th><?= __('{{ alias|underscore|singularize|humanize }}') ?></th>
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ alias|underscore|singularize|humanize }}') ?></th>
<td><?= ${{ singularVar }}->hasValue('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?></td>
</tr>
{%~ endfor %}
{% endif %}
{% if groupedFields.number %}
{%~ for field in groupedFields.number %}
<tr>
<th><?= __('{{ field|humanize }}') ?></th>
{%~ set columnData = Bake.columnData(field, schema) %}
{%~ if columnData.null %}
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
{% set columnData = Bake.columnData(field, schema) %}
{% if columnData.null %}
<td><?= ${{ singularVar }}->{{ field }} === null ? '' : $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
{%~ else %}
<td><?= $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
Expand All @@ -79,7 +80,7 @@
{% if groupedFields.enum %}
{%~ for field in groupedFields.enum %}
<tr>
<th><?= __('{{ field|humanize }}') ?></th>
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
{%~ set columnData = Bake.columnData(field, schema) %}
{%~ set supportsLabel = Bake.enumSupportsLabel(field, schema) %}
{%~ if columnData.null %}
Expand All @@ -93,24 +94,24 @@
{% if groupedFields.date %}
{%~ for field in groupedFields.date %}
<tr>
<th><?= __('{{ field|humanize }}') ?></th>
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
<td><?= h(${{ singularVar }}->{{ field }}) ?></td>
</tr>
{%~ endfor %}
{% endif %}
{% if groupedFields.boolean %}
{%~ for field in groupedFields.boolean %}
<tr>
<th><?= __('{{ field|humanize }}') ?></th>
<td><?= ${{ singularVar }}->{{ field }} ? __('Yes') : __('No'); ?></td>
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
<td><?= ${{ singularVar }}->{{ field }} ? {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Yes') : {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'No'); ?></td>
</tr>
{%~ endfor %}
{% endif %}
</table>
{% if groupedFields.text %}
{%~ for field in groupedFields.text %}
<div class="text">
<strong><?= __('{{ field|humanize }}') ?></strong>
<strong><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></strong>
<blockquote>
<?= $this->Text->autoParagraph(h(${{ singularVar }}->{{ field }})); ?>
</blockquote>
Expand All @@ -122,15 +123,15 @@
{%~ set otherSingularVar = alias|singularize|variable %}
{%~ set otherPluralHumanName = details.controller|underscore|humanize %}
<div class="related">
<h4><?= __('Related {{ otherPluralHumanName }}') ?></h4>
<h4><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Related {{ otherPluralHumanName }}') ?></h4>
<?php if (!empty(${{ singularVar }}->{{ details.property }})) : ?>
<div class="table-responsive">
<table>
<tr>
{%~ for field in details.fields %}
<th><?= __('{{ field|humanize }}') ?></th>
<th><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ field|humanize }}') ?></th>
{%~ endfor %}
<th class="actions"><?= __('Actions') ?></th>
<th class="actions"><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Actions') ?></th>
</tr>
<?php foreach (${{ singularVar }}->{{ details.property }} as ${{ otherSingularVar }}) : ?>
<tr>
Expand All @@ -139,14 +140,14 @@
{%~ endfor %}
{%~ set otherPk = '$' ~ otherSingularVar ~ '->' ~ details.primaryKey[0] %}
<td class="actions">
<?= $this->Html->link(__('View'), ['controller' => '{{ details.controller }}', 'action' => 'view', {{ otherPk|raw }}]) ?>
<?= $this->Html->link(__('Edit'), ['controller' => '{{ details.controller }}', 'action' => 'edit', {{ otherPk|raw }}]) ?>
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'View'), ['controller' => '{{ details.controller }}', 'action' => 'view', {{ otherPk|raw }}]) ?>
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Edit'), ['controller' => '{{ details.controller }}', 'action' => 'edit', {{ otherPk|raw }}]) ?>
<?= $this->Form->postLink(
__('Delete'),
{% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Delete'),
['controller' => '{{ details.controller }}', 'action' => 'delete', {{ otherPk|raw }}],
[
'method' => 'delete',
'confirm' => __('Are you sure you want to delete # {0}?', {{ otherPk|raw }}),
'confirm' => {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'Are you sure you want to delete # {0}?', {{ otherPk|raw }}),
]
) ?>
</td>
Expand Down
4 changes: 2 additions & 2 deletions templates/bake/element/Controller/add.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
if ($this->request->is('post')) {
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData());
if ($this->{{ currentModelName }}->save(${{ singularName }})) {
$this->Flash->success(__('The {{ singularHumanName|lower }} has been saved.'));
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been saved.'));

return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
}
{% set associations = Bake.aliasExtractor(modelObj, 'BelongsTo') %}
{% set associations = associations|merge(Bake.aliasExtractor(modelObj, 'BelongsToMany')) %}
Expand Down
4 changes: 2 additions & 2 deletions templates/bake/element/Controller/delete.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
$this->Authorization->authorize(${{ singularName }});
{% endif %}
if ($this->{{ currentModelName }}->delete(${{ singularName }})) {
$this->Flash->success(__('The {{ singularHumanName|lower }} has been deleted.'));
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been deleted.'));
} else {
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be deleted. Please, try again.'));
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be deleted. Please, try again.'));
}

return $this->redirect(['action' => 'index']);
Expand Down
4 changes: 2 additions & 2 deletions templates/bake/element/Controller/edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
if ($this->request->is(['patch', 'post', 'put'])) {
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData());
if ($this->{{ currentModelName }}->save(${{ singularName }})) {
$this->Flash->success(__('The {{ singularHumanName|lower }} has been saved.'));
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been saved.'));

return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
}
{% for assoc in belongsTo|merge(belongsToMany) %}
{%~ set otherName = Bake.getAssociatedTableAlias(modelObj, assoc) %}
Expand Down
4 changes: 2 additions & 2 deletions templates/bake/element/Controller/login.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
if ($result->isValid()) {
$this->Flash->success(__('Login successful'));
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'Login successful'));
$redirect = $this->Authentication->getLoginRedirect();
if ($redirect) {
return $this->redirect($redirect);
Expand All @@ -36,6 +36,6 @@

// Display error if user submitted and authentication failed
if ($this->request->is('post')) {
$this->Flash->error(__('Invalid username or password'));
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'Invalid username or password'));
}
}
22 changes: 22 additions & 0 deletions tests/TestCase/Command/TemplateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,28 @@ public function testBakeIndexPlugin()
$this->assertFileContains('$comment->article->id', $this->generatedFile);
}

/**
* test Bake with plugins with __d() translations.
*
* @return void
*/
public function testBakePluginTemplatesWithDomain()
{
$this->_loadTestPlugin('BakeTest');
$path = Plugin::templatePath('BakeTest');

// Setup association to ensure properties don't have dots
$model = $this->getTableLocator()->get('BakeTest.Comments');
$model->belongsTo('Articles');

$this->generatedFile = $path . 'Comments/index.php';
$this->exec('bake template BakeTest.comments index');

$this->assertExitCode(CommandInterface::CODE_SUCCESS);
$this->assertFileExists($this->generatedFile);
$this->assertFileContains('__d(\'BakeTest\', ', $this->generatedFile);
}

/**
* Ensure that models in a tree don't include form fields for lft/rght
*
Expand Down
Loading