This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi, I'm stuck in a parameter value problem within my component.
class Songs extends ComponentBase
{
...
public function defineProperties()
{
return [
'pageNumber' => [
'title' => 'codalia.songbook::lang.settings.songs_pagination',
'description' => 'codalia.songbook::lang.settings.songs_pagination_description',
'type' => 'string',
'default' => '{{ :page }}',
],
'categoryFilter' => [
'title' => 'codalia.songbook::lang.settings.songs_filter',
'description' => 'codalia.songbook::lang.settings.songs_filter_description',
'type' => 'string',
'default' => '',
],
'songsPerPage' => [
'title' => 'codalia.songbook::lang.settings.songs_per_page',
'type' => 'string',
'validationPattern' => '^[0-9]+$',
'validationMessage' => 'codalia.songbook::lang.settings.songs_per_page_validation',
'default' => '10',
],
...
}
...
protected function listSongs()
{
...
}])->listFrontEnd([
// still returns the default value...
'page' => $this->property('pageNumber'),
'sort' => $this->property('sortOrder'),
'perPage' => $this->property('songsPerPage'),
...
}
...
}
Now in my category page I have set some custom values in my list component configuration but the
$this->property() method still returns the default values regardless of the custom values newly set.
Did I miss something in my code ?
Last updated
I've just put code snippets as the whole code is a bit long. I actually defined a sortOrder property I just didn't include in my code sample.
public $sortOrder;
...
public function defineProperties()
{
...
'sortOrder' => [
'title' => 'codalia.songbook::lang.settings.songs_order',
'description' => 'codalia.songbook::lang.settings.songs_description',
'type' => 'dropdown',
'default' => 'published_at desc',
],
...
}
protected function listSongs()
{
$category = $this->category ? $this->category->id : null;
/*
* List all the songs, eager load their categories
*/
//$isPublished = !$this->checkEditor();
$isPublished = true;
$songs = Song::whereHas('categories', function ($query) {
// Gets songs which have at least one published category.
$query->where('status', 'published');
})->with(['categories' => function ($query) {
// Gets published categories only.
$query->where('status', 'published');
}])->listFrontEnd([
'page' => $this->property('pageNumber'),
'sort' => $this->property('sortOrder'),
'perPage' => $this->property('songsPerPage'),
'search' => trim(input('search')),
'category' => $category,
'published' => $isPublished,
'exceptSong' => is_array($this->property('exceptSong'))
? $this->property('exceptSong')
: preg_split('/,\s*/', $this->property('exceptSong'), -1, PREG_SPLIT_NO_EMPTY),
'exceptCategories' => is_array($this->property('exceptCategories'))
? $this->property('exceptCategories')
: preg_split('/,\s*/', $this->property('exceptCategories'), -1, PREG_SPLIT_NO_EMPTY),
]);
/*
* Add a "url" helper attribute for linking to each song and category
*/
$songs->each(function($song) {
$song->setUrl($this->songPage, $this->controller);
$song->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
});
});
return $songs;
}
It all looks good to me.
I assume you did "simpler" tests? There are many ways this could go wrong with that "interesting" query... ;)
properties have been/are tested quite a bit, so I doubt there is a problem with them... so my guess is the problem is somewhere else. Try simplifying the code and increase complexity from where it works up, you'll find where the problem is.
I don't think the problem comes from within the function. If I put this code line at the top:
protected function listSongs() {
dd($this->property('songsPerPage'));
...
I get the default value instead of the custom one set in the component configuration. Thus the problem occurs ahead of the function. I tried to trace back the property() method but I can't figure out where and how the custom values are stored. Any idea ?
@lucas.sanner54070 do you see the custom property value in the file content of the page you have the component on?
Not sure to really get what you're talking about but here's the file content of the page and the custom properties have been taken into account properly:
song-category.htm
title = "Song Category"
url = "/song-category/:slug/:page?"
layout = "default"
is_hidden = 0
[songList]
pageNumber = "{{ :page }}"
categoryFilter = "{{ :slug }}"
songsPerPage = "{{ 5 }}"
noSongsMessage = "Hello World"
sortOrder = "title asc"
categoryPage = "song-category"
songPage = "song"
==
<div class="container col-sm-9">
{% component 'songList' %}
</div>
Last updated
Ok, I found your problem. When yo uset the Component's property value from the Inspector, don't click on the ">_" to assign a value, just click in the textbox left of it.
Make sure your CMS page has the following for that property:
[songList]
songsPerPage = "5"
And that should work
I tried almost any combination: "5", {{ 5 }}, 5 , none of them have worked. Strangely enough I noticed the exact same problem with the Blog plugin Do properties work properly in your plugins ?
yes, there is something at play here on your end.
Do you have many plugins installed? I would try disabling all plugins and see if you still have the problem
My plugin is the only one installed in October so I really don't know where this problem comes from.
Is your plugin doing many things? Try only the registerComponent method to be sure it's not some bug you introduce elsewhere in the plugin.
After many hours of struggling I think I finally solved my problem.
As you've mentioned above, the problem comes from the ">_" in the textbox, but for whatever reason
it didn't work on the first time (probably because my code was too messy).
Everything seems ok now, but to get rid of the '>__' I set the showExternalParam to false in
some of my parameters
...
'songsPerPage' => [
'title' => 'codalia.songbook::lang.settings.songs_per_page',
'default' => 5,
'type' => 'string',
'validationPattern' => '^[0-9]+$',
'validationMessage' => 'codalia.songbook::lang.settings.songs_per_page_validation',
'showExternalParam' => false
],
...
1-17 of 17