Skip to content

Commit

Permalink
Allow adding new items in the CP
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Jan 10, 2020
1 parent 3537635 commit 834d594
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/Wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,17 @@ private function _registerCpRoutes()
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
$event->rules = array_merge($event->rules, [
'wishlist' => 'wishlist/lists/index',

'wishlist/lists/<listTypeHandle:{handle}>' => 'wishlist/lists/index',
'wishlist/lists/<listTypeHandle:{handle}>/new' => 'wishlist/lists/edit-list',
'wishlist/lists/<listTypeHandle:{handle}>/<listId:\d+>' => 'wishlist/lists/edit-list',
'wishlist/lists/<listTypeHandle:{handle}>/<listId:\d+>/items/new' => 'wishlist/items/edit-item',
'wishlist/lists/<listTypeHandle:{handle}>/<listId:\d+>/items/<itemId:\d+>' => 'wishlist/items/edit-item',

'wishlist/list-types' => 'wishlist/list-types/list-type-index',
'wishlist/list-types/<listTypeId:\d+>' => 'wishlist/list-types/edit-list-type',
'wishlist/list-types/new' => 'wishlist/list-types/edit-list-type',

'wishlist/settings' => 'wishlist/default/settings',
]);
});
Expand Down
7 changes: 6 additions & 1 deletion src/controllers/ItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ItemsController extends BaseController
// Public Methods
// =========================================================================

public function actionEditItem(string $listTypeHandle, int $listId, int $itemId, Item $item = null): Response
public function actionEditItem(string $listTypeHandle, int $listId, int $itemId = null, Item $item = null): Response
{
$variables = [
'listTypeHandle' => $listTypeHandle,
Expand All @@ -33,6 +33,11 @@ public function actionEditItem(string $listTypeHandle, int $listId, int $itemId,

$this->_prepareVariableArray($variables);

// Properly bootstrap a new item
if (!$variables['item']->id) {
$variables['item']->listId = $listId;
}

// Can't just use the entry's getCpEditUrl() because that might include the site handle when we don't want it
$variables['baseCpEditUrl'] = 'wishlist/lists/' . $listTypeHandle . '/' . $listId . '/items/{id}';

Expand Down
30 changes: 14 additions & 16 deletions src/templates/items/_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{ label: list.title | t('wishlist'), url: list.cpEditUrl },
] %}

{% set title = item.title %}
{% set title = item.title ?? 'Add item' %}

{% set fullPageForm = true %}
{% set saveShortcutRedirect = continueEditingUrl %}
Expand Down Expand Up @@ -81,21 +81,19 @@
first: true,
}) }}

<div class="field">
<div class="heading">
<label>{{ 'Element' | t('wishlist') }}</label>
</div>

{% if item.element %}
<div class="input ltr">
<a href="{{ item.element.cpEditUrl }}">{{ item.element }}</a>
</div>
{% else %}
<div class="input ltr">
{{ 'Element not found' | t('wishlist') }}
</div>
{% endif %}
</div>
{# Not sure the best way to handle this for new entries... #}
{% set elementType = item.elementClass ?? 'craft\\elements\\Entry' %}

{{ forms.elementSelectField({
label: 'Element' | t('wishlist'),
id: 'elementId',
name: 'elementId',
elementType: elementType,
elements: (item.element is defined and item.element ? [item.element]),
selectionLabel: "Choose" | t('app'),
limit: 1,
errors: item ? item.getErrors('elementId'),
}) }}
</div>

<hr>
Expand Down
4 changes: 4 additions & 0 deletions src/templates/lists/_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ <h2>{{ 'List Items' | t('comments') }}</h2>
{% else %}
<p><em>{{ 'No items in this list.' | t('wishlist') }}</em></p>
{% endif %}

<hr>

<a class="btn formsubmit submit icon add" data-redirect="{{ url('wishlist/lists/' ~ list.type.handle ~ '/' ~ list.id ~ '/items/new') | hash }}">{{ 'Add item' }}</a>
</div>
{% endblock %}

Expand Down

0 comments on commit 834d594

Please sign in to comment.