Skip to content

Commit

Permalink
Fix slug generator not taking into account sibling nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
strads10 committed Jul 26, 2017
1 parent 786642b commit 2ed7607
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
9 changes: 6 additions & 3 deletions resources/assets/js/controllers/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ class NodeStoreItem {
}

class SlugApiHandler {
constructor(apiUrl) {
constructor(apiUrl, parameters = {}) {
this.apiUrl = apiUrl;
this.parameters = parameters;
}

create(string) {
return jQuery.get( this.apiUrl, { name: string } );
return jQuery.get(this.apiUrl, Object.assign({name: string}, this.parameters));
}
}

Expand Down Expand Up @@ -159,7 +160,9 @@ jQuery(document).ready(() => {
let generateButton = slugField.find('.button.generate');
let slugLink = slugField.find('.link');

let slugApi = new SlugApiHandler(slugInput.data('generatorUrl'));
let slugApi = new SlugApiHandler(slugInput.data('generatorUrl'), {
parent_id: slugInput.data('nodeParentId')
});

let updateSlugLink = () => {
slugLink.find('span:last').text(encodeURIComponent(slugInput.val()));
Expand Down
15 changes: 13 additions & 2 deletions src/Admin/Form/Fields/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public function render()
->setName( $this->getNameSpacedName() )
->setValue( $this->getValue() )
->addClass( 'text' )
->addAttributes( [ 'data-generator-url' => $this->apiUrl ] );
->addAttributes( [
'data-generator-url' => $this->apiUrl,
'data-node-parent-id' => $this->getParentId()
] );

$button = Button::create()
->type( 'button', 'secondary generate' )
Expand Down Expand Up @@ -132,7 +135,7 @@ protected function getUriToNewModel()
* @var Node $parentNode
*/
$repository = new NodesRepository;
$parentNode = $repository->find( request( 'parent_id' ) );
$parentNode = $repository->find( $this->getParentId() );

return $parentNode ? $parentNode->getUri() : (string) null;
}
Expand All @@ -144,4 +147,12 @@ protected function getUriToSlug()
{
return $this->getUriToExistingModel() ?: $this->getUriToNewModel();
}

/**
* @return int
*/
protected function getParentId()
{
return request( 'parent_id' ) ?: $this->getModel()->getParentId();
}
}

0 comments on commit 2ed7607

Please sign in to comment.