Settings Page Concrete CMS

User
also create a settings page in concrete cms 9 custom package like number of records per page and cron run or not
Assistant
Here’s how you can create a **Settings Page** in your **Concrete CMS 9 custom package** for options like:

– **Number of records per page**
– **Enable/disable cron run**

### ✅ Step-by-Step Guide

#### 1. **Add Settings Table (Optional but Recommended)**

In `db.xml` of your package:

<schema>
  <table name="ShoppingFeedSettings">
    <field name="recordsPerPage" type="I" size="11" />
    <field name="enableCron" type="B" />
    <key name="primary" primary="true">
      <field name="recordsPerPage"/>
    </key>
  </table>
</schema>

Or if you store in config (simpler way), skip DB and use the config repository.

#### 2. **Controller for Dashboard Settings Page**

Create the controller:
`/packages/shopping_feed_manager/controller/single_page/dashboard/shopping_feed/settings.php`

<?php
namespace ConcretePackageShoppingFeedManagerControllerSinglePageDashboardShoppingFeed;

use ConcreteCorePageControllerDashboardPageController;
use ConcreteCoreConfigRepositoryRepository;

class Settings extends DashboardPageController
{
    protected $config;

    public function on_start()
    {
        parent::on_start();
        $this->config = $this->app->make(Repository::class);
    }

    public function view()
    {
        $this->set('recordsPerPage', $this->config->get('shopping_feed_manager.records_per_page', 10));
        $this->set('enableCron', $this->config->get('shopping_feed_manager.enable_cron', false));
    }

    public function save()
    {
        $recordsPerPage = (int)$this->request->request->get('records_per_page');
        $enableCron = $this->request->request->get('enable_cron') ? true : false;

        $this->config->save('shopping_feed_manager.records_per_page', $recordsPerPage);
        $this->config->save('shopping_feed_manager.enable_cron', $enableCron);

        $this->flash('success', 'Settings saved successfully.');
        return $this->redirect($this->action(''));
    }
}

#### 3. **View File**

Create the view file:
`/packages/shopping_feed_manager/single_pages/dashboard/shopping_feed/settings.php`

<?php defined('C5_EXECUTE') or die("Access Denied."); ?>
<form method="post" action="<?= $controller->action('save') ?>">
    <?= $this->controller->token->output('save') ?>

    <div class="form-group">
        <label for="records_per_page">Number of Records Per Page</label>
        <input type="number" name="records_per_page" class="form-control" value="<?= h($recordsPerPage) ?>" required>
    </div>

    <div class="form-group">
        <label>
            <input type="checkbox" name="enable_cron" value="1" <?= $enableCron ? 'checked' : '' ?>>
            Enable Cron Run
        </label>
    </div>

    <button type="submit" class="btn btn-primary">Save Settings</button>
</form>

#### 4. **Install Page During Package Install**

In your `package controller`:

use ConcreteCorePageSingle;

public function install()
{
    $pkg = parent::install();

    Single::add('/dashboard/shopping_feed/settings', $pkg);

    // Set default values
    $config = $this->app->make('config');
    $config->save('shopping_feed_manager.records_per_page', 10);
    $config->save('shopping_feed_manager.enable_cron', false);
}

### ✅ Usage

Anywhere in your code:

$config = Core::make('config');
$perPage = $config->get('shopping_feed_manager.records_per_page');
$cronEnabled = $config->get('shopping_feed_manager.enable_cron');

Let me know if you'd like to add validation, permissions, or extend this page with more options.

User
Attempted to load class "Core" from namespace "ConcretePackageShoppingFeedManagerControllerSinglePageDashboardShoppingFeed".
Did you forget a "use" statement for "DefuseCryptoCore"?
Assistant
That error occurs because you're trying to use `Core::make()` statically, but in **Concrete CMS 9**, the `Core` facade has been deprecated. Instead, you should use **dependency injection** or access services via `$this->app`.

### ✅ Fix: Replace `Core::make(…)` with `$this->app->make(…)`

Here’s how you can fix your code:

### ✅ Option 1: Use `$this->app` inside controller methods

Replace this:

$config = Core::make('config');

With:

$config = $this->app->make('config');

### ✅ Option 2: Inject the service in the constructor or `on_start`

If you're outside a controller, use:

use ConcreteCoreSupportFacadeApplication;

$app = Application::getFacadeApplication();
$config = $app->make('config');

### ✅ Correct Controller Usage Example

Here’s the corrected part inside your `Settings.php` controller:

public function view()
{
    $config = $this->app->make('config');
    $this->set('recordsPerPage', $config->get('shopping_feed_manager.records_per_page', 10));
    $this->set('enableCron', $config->get('shopping_feed_manager.enable_cron', false));
}

### ✅ Also Fix in `package controller`

In your `install()` method:

use ConcreteCoreSupportFacadeApplication;

public function install()
{
    $pkg = parent::install();
    ConcreteCorePageSingle::add('/dashboard/shopping_feed/settings', $pkg);

    $app = Application::getFacadeApplication();
    $config = $app->make('config');
    $config->save('shopping_feed_manager.records_per_page', 10);
    $config->save('shopping_feed_manager.enable_cron', false);
}

Let me know if you want to store these settings in a DB table instead of the config system.

Leave a Comment

Your email address will not be published. Required fields are marked *