This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I want to enable template configuration where user can choose among Blog Categories. Meaning my template config should be able to generate generate list of available categories.
I know this can be done in theme.yaml
by using type dropdown (https://octobercms.com/docs/backend/forms#field-dropdown)
How do I get options populated? Where to put getCategoriesOptions function? How to extend ThemeData?
Thanks!
ThemeData::extend(function($model) {
$model->addDynamicMethod('listStatuses', function($query) {
return [
1 => 'Main',
2 => 'Main 2',
];
});
});
this part of the code should do the trick just add this to the custom plugin method and it should work
Last updated
@izodevelopment, Still it is not quite clear to me. I have the same issue and I need in my theme a custom form field with a dropdown showing the filenames of a directory within that same theme. I know how to do that, but do I really need to create a plugin to define this custom behavior that is entirely related to the theme?
There is not an option to add the code you have provided in your reply within the theme itself?
I have figured it out, here some samples on how I did it.
Let's assume our theme is named: "MyNiceTheme"
fields.yaml (excerpt)
theme_variation:
tab: Defaults
label: Theme variation
size: small
span: left
type: dropdown
Then I created a plugin named "MyNiceThemeAddons" and placed the following in the Plugin.php:
public $elevated = true;
public function boot()
{
$this->getThemeVariationOptions();
}
/**
* Get Theme Variations
*
* @return array
*/
public function getThemeVariationOptions()
{
$theme = Theme::getActiveTheme();
$path = $theme->getPath() . '/assets/vendor/bootstrap.themestr/scss';
if (file_exists($path) === false) {
return [];
}
$files = array_diff(scandir($path), array('.', '..'));
$options = [];
foreach ($files as $file) {
if (substr($file, 0, 6) === 'theme_') {
$name = $name = ucfirst(explode('.', substr($file, 6))[0]);
$options[$file] = $name;
}
}
ThemeData::extend(function ($model) use ($options) {
$model->addDynamicMethod('getThemeVariationOptions', function () use ($model, $options) {
return $options;
});
});
}
What this sample above does is reading the file contents of a folder within my theme folder and return of filenames starting with 'theme_' the name of the file and a nice readable name for it. In the backend of the theme you can select a filename from a dropdown on theme level.
Select from categories
fields.yaml (excerpt)
include_categories:
tab: Defaults
label: Categories to include
size: small
span: left
type: dropdown
exclude_categories:
tab: Defaults
label: Categories to exclude
size: small
span: left
type: dropdown
In your Plugin.php:
use RainLab\Blog\Models\Category as BlogCategoryModel;
Get the categories (from the Rainlab Blog plugin)
$categoryItems = BlogCategoryModel::lists( 'name', 'id' );
This is how it could be used, I haven't tested it, it is an adaption of how it was used in a component I wrote.
public $elevated = true;
public function boot()
{
$this->getIncludeCategoriesOptions();
$this->getExcludeCategoriesOptions();
}
public function getIncludeCategoriesOptions()
{
$categories = BlogCategoryModel::lists( 'name', 'id' );
ThemeData::extend(function ($model) use ($categories) {
$model->addDynamicMethod('getIncludeCategoriesOptions', function () use ($model, $categories) {
return $categories;
});
});
}
public function getExcludeCategoriesOptions()
{
$categories = BlogCategoryModel::lists( 'name', 'id' );
ThemeData::extend(function ($model) use ($categories) {
$model->addDynamicMethod('getExcludeCategoriesOptions', function () use ($model, $categories) {
return $categories;
});
});
}
Last updated
1-4 of 4