Override (almost) any core file in Concrete CMS
When building complex Concrete CMS sites, you may find yourself needing to change some core functionality. Modern versions of concrete offer a few ways that we can do this without sacrificing updatability or performance.
Disable caching
First, turn off overrides caching via Dashboard > System & Settings > Optimization > Cache & Speed Settings. Until this is turned off Concrete CMS won't look in the application directory unless it knows it has a file in there. This will save you from a lot of headaches when developing and testing these overrides.
Service Provider Overrides
Most of concrete's core functionality is bundled into what we call "services", you can find services in subdirectories
of the /concrete/src/
directory.
In order to start these services up, concrete employs a pattern known as "Service Provider". A service provider
manages booting up a service and adding it onto our Application
instance so if we want to modify what a service is
doing, this is the place to start.
For example, what if you wanted to swap the core logger with a custom one?
- Open up the core
app
config group, this is located in/concrete/config/app.php
- Find where we list "LoggingServiceProvider" and determine the config key. It looks to be
app.providers.core_logging
- Open up our application's
app
config group, this is located in/application/config/app.php
- Change
app.providers.core_logging
to our own custom class
In /application/config/app.php
:
<?php
return array(
'providers' => array(
'core_logging' => '\Application\Src\Custom\CustomLoggingServiceProvider'
)
);
Now we've overridden the core logging service provider with our own, but the class we offered CustomLoggingServiceProvider
doesn't exist since we just made it up.
In /application/src/Custom/CustomLoggingServiceProvider
<?php
namespace Application\Src\Custom;
class CustomLoggingServiceProvider extends \Concrete\Core\Service\Provider
{
public function register()
{
$my_psr3_logger = new \Some\Custom\Logger();
$this->app->bind('log', $my_psr3_logger);
// Set the rest of the bindings that the core sets
$this->app->bind('Concrete\Core\Logging\Logger', 'log');
$this->app->bind('Psr\Log\LoggerInterface', 'log');
}
}
Now instead of the core logging service provider, our own logging service provider is registered and our own custom logger is used everywhere!
Path Based Overrides
Many files can be overridden in concrete simply by providing a file at a specific path. Pretty much the only
exclusion to what can be overridden by path is the contents of concrete/src/
. Today, we can use paths to override
essentially everything that is not a service.
Essentially all path overrides follow an extremely simply pattern: find the path of the file you want to override within the concrete directory, and create a file at that same path within your application directory.
For example:
Core: /concrete/blocks/some_block/view.php
Override: /application/blocks/some_block/view.php
Core: /concrete/js/some_js_file.js
Override: /application/js/some_js_file.js
Overriding Core Block Types
Concrete comes with many useful block types, but sometimes you might want to tweak how a core block functions. Some reasons you might want to override a block include:
- Override functionality and display
- Adding a custom template
Override functionality and display
We can add actions, change how things save, and even replace data by overriding a block controller.
For example if we wanted to override the content block to capitalize all content:
In /application/blocks/content/controller.php
<?php
namespace Application\Block\Content;
class Controller extends \Concrete\Block\Content\Controller
{
public function view()
{
$this->set('content', strtoupper($this->getContent()));
}
}
That's not all we can override, we can do this same thing with any file in the /concrete/blocks/content/
directory. All
you need to do is create the file in your /application/blocks/content/
directory!
Adding a Custom Template
Custom Templates allow us to create custom views for blocks that we can optionally enable.
For example, to modify the HTML block to render as code:
In /application/blocks/html/templates/code_block/view.php
<pre><code><?= h(trim($content)) ?></code></pre>
Custom templates can be used to replace any javascript, css, or view files but they cannot be used to replace the controller.
Path Based Examples
- Themes
- Core Path:
/concrete/themes/theme_handle/page_theme.php
- Package Path:
/packages/package_handle/themes/theme_handle/page_theme.php
- Override Path:
/application/themes/theme_handle/page_theme.php
- Override Class Name:
\Application\Theme\ThemeHandle\PageTheme
- Core Path:
- Blocks
- Core Path:
/concrete/blocks/block_handle/controller.php
- Package Path:
/packages/package_handle/blocks/block_handle/controller.php
- Override Path:
/application/blocks/block_handle/controller.php
- Override Class Name:
\Application\Block\BlockHandle\Controller
- Core Path:
- Attributes
- Core Path:
/concrete/attributes/attribute_handle/controller.php
- Package Path:
/packages/package_handle/attributes/attribute_handle/controller.php
- Override Path:
/application/attributes/attribute_handle/controller.php
- Override Class Name:
\Application\Attribute\AttributeHandle\Controller
- Core Path:
- Authentication Types
- Core Path:
/concrete/authentication/type_handle/controller.php
- Package Path:
/packages/package_handle/authentication/type_handle/controller.php
- Override Path:
/application/authentication/type_handle/controller.php
- Override Class Name:
\Application\Authentication\TypeHandle\Controller
- Core Path:
- Controllers
- Core Path:
/concrete/controllers/single_page/page_name.php
- Package Path:
/packages/package_handle/controllers/single_page/page_name.php
- Override Path:
/application/controllers/single_page/page_name.php
- Override Class Name:
\Application\Controller\SinglePage\PageName
- Core Path:
- Single Pages
- Core Path:
/concrete/single_pages/account/avatar.php
- Override Path:
/application/single_pages/account/avatar.php
- Core Path:
- Elements
- Core Path:
/concrete/elements/account/menu.php
- Override Path:
/application/elements/account/menu.php
- Core Path:
- Compiled CSS
- Core Path:
/concrete/css/image-editor.css
- Override Path:
/application/css/image-editor.css
- Core Path:
- Compiled JS
- Core Path:
/concrete/js/jquery.js
- Override Path:
/application/js/jquery.js
- Core Path: