This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

pastorryanhayden11094
pastorryanhayden11094

I've developed several themes that each share common attributes (logo, name, contact info, etc.) and I'd like to create a settings panel where this stuff can be entered and referenced in the themes. That way clients can switch themes without having to re-enter this information over and over. My question is - what is the best way to do this?

Eoler
Eoler

pastorryanhayden11094 said: My question is - what is the best way to do this?

Write a plugin with Settings model, use it in each theme: https://octobercms.com/docs/plugin/settings

pastorryanhayden11094
pastorryanhayden11094

That was actually really helpful. Thank you. I now have the settings panel setup with all of the fields I needed. Now, how do I get to them via twig? (I.e. How do I get it so I can use something like {{settings.church_name}} ?)

Last updated

strk
strk

pastorryanhayden11094 said:

That was actually really helpful. Thank you. I now have the settings panel setup with all of the fields I needed. Now, how do I get to them via twig? (I.e. How do I get it so I can use something like {{settings.church_name}} ?)

Hi, you could also use theme config as settings (supports backend form widgets) After adding new fields you will have new button to customize values here (example.com/backend/cms/themes) https://octobercms.com/docs/themes/development#customization

Last updated

pastorryanhayden11094
pastorryanhayden11094

ed said: Hi, you could also use theme config as settings (supports backend form widgets) After adding new fields you will have new button to customize values here (example.com/backend/cms/themes) https://octobercms.com/docs/themes/development#customization

Thanks. I'm actually already doing this. I think this is one of the best features of OctoberCMS theming.

But it doesn't work for what I'm wanting to do.

I'm setting up a system for church websites with lots of themes and much of the info would be very repetitive. I want the people who use the system to be able to enter the basic information once and it just work across multiple themes.

pastorryanhayden11094
pastorryanhayden11094

This is what I have:

Now I just need to figure out how to use it on the front end.

Eoler
Eoler

pastorryanhayden11094 said: Now, how do I get to them via twig? (I.e. How do I get it so I can use something like {{settings.church_name}} ?)

Something like this in a CMS page/layout code section:

use Acme\Demo\Models\Settings;
$settings = Settings::instance();
$this['church_name'] = $settings->church_name;
...

or use a component to inject values into page...

pastorryanhayden11094
pastorryanhayden11094

Eoler said:

pastorryanhayden11094 said: Now, how do I get to them via twig? (I.e. How do I get it so I can use something like {{settings.church_name}} ?)

Something like this in a CMS page/layout code section:

use Acme\Demo\Models\Settings;
$settings = Settings::instance();
$this['church_name'] = $settings->church_name;
...

or use a component to inject values into page...

Thanks man. This is going to help a lot.

fadi.a.shakra22773
fadi.a.shakra22773

Can you share your code or plugin ?

mail31666
mail31666

This is a working model with a multiple selection of categories (Rainlab/Blog/Models/Category) on theme level.

This creates a dialog with categories in the backend and makes it possible to select multiple categories through checkboxes.

fields.yaml (excerpt)

    selected_categories:
      tab: Defaults
      label: Categories
      size: small
      span: left
      type: relation
      attributes:
          multiple: true

Theme addon plugin

While the theme is missing a kind of theme.php I have created a plugin and named it<yourtheme>addons (replace yourtheme with something useful).

Plugin.php

<?php namespace YourNameSpace\YourThemeAddons;  

use System\Classes\PluginBase;  
use Cms\Models\ThemeData;  
use Cms\Classes\Theme;  

class Plugin extends PluginBase  
{  
    // Table that joins the ThemeData model with categories  
    // Rename it to something to your own liking
    // See below for the SQL definition
    private $table = 'yournamespace_yourthemeaddons_';  

    public function boot()  
    {  
        $this->getSelectedCategoriesOptions();  
    }  

   /**
    *  
    * Added a Twig function to get the selected categories available
    * in my partials
    * 
    */ 
    public function registerMarkupTags()
    {  
       return [  
         'functions' => [  
            'themeSelectedCategories' => [$this, 'themeSelectedCategories']  
         ]  
      ];  
    }  

   /**
    * Function to get the data that goes into the Twig result
    * 
    */
    public function themeSelectedCategories()
    {  
       $currentTheme = Theme::getActiveTheme();  
       $selected = ThemeData::with(['selected_categories'])  
           ->where('theme', '=', $currentTheme->getDirName())  
           ->first();  

       return $selected->selected_categories;  
    }  

   /**  
    * Get Categories Options * * @return array  
    */
    public function getSelectedCategoriesOptions()  
    {  
       ThemeData::extend(function ($model) {  
          $model->belongsToMany['selected_categories'] = [  
              // Rainlab category model (blog)
              'RainLab\Blog\Models\Category', 
              // our pivot table (connecting themedata with category table
              'table' => $this->table, 
              // order to show the categories
              'order' => 'name',
              // theme id in pivot  
              'key' => 'theme',  
              // category id in pivot
              'otherKey' => 'category'
          ];  
      });  
    }  
}

Pivot: yournamespace_yourthemeaddons_ (table)

Rename the table to your own liking

CREATE TABLE `yournamespace_yourthemeaddons_` (
    `theme` INT(10) UNSIGNED NOT NULL,
    `category` INT(10) UNSIGNED NOT NULL,
    PRIMARY KEY (`theme`, `category`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB

or in the plugins updates folder:

builder_table_create_yournamespace_yourthemeaddons_.php
<?php namespace YourNameSpace\YourThemeAddons\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class BuilderTableCreateYourNameSpaceYourthemeaddons extends Migration
{
    public function up()
    {
        Schema::create('yournamespace_yourthemeaddons_', function($table)
        {
            $table->engine = 'InnoDB';
            $table->integer('theme')->unsigned();
            $table->integer('category')->unsigned();
            $table->primary(['theme','category']);
        });
    }

    public function down()
    {
        Schema::dropIfExists('yournamespace_yourthemeaddons_');
    }
}

This can easily be done with the builder plugin.

Twig

This is a very rudimentary twig based on the twig function in the plugin

<ul>
{% for category in themeSelectedCategories() %}
    <li>{{ category.name }}</li>
{% endfor %}
</ul>
</div>

Hope this was helpful.

Last updated

1-10 of 10

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.