Override (almost) any core file in Concrete CMS

This is a community-contributed tutorial. This tutorial is over a year old and may not apply to your version of Concrete CMS.
Jun 8, 2015

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?

  1. Open up the core app config group, this is located in /concrete/config/app.php
  2. Find where we list "LoggingServiceProvider" and determine the config key. It looks to be app.providers.core_logging
  3. Open up our application's app config group, this is located in /application/config/app.php
  4. 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
  • 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
  • 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
  • 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
  • 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
  • Single Pages
    • Core Path: /concrete/single_pages/account/avatar.php
    • Override Path: /application/single_pages/account/avatar.php
  • Elements
    • Core Path: /concrete/elements/account/menu.php
    • Override Path: /application/elements/account/menu.php
  • Compiled CSS
    • Core Path: /concrete/css/image-editor.css
    • Override Path: /application/css/image-editor.css
  • Compiled JS
    • Core Path: /concrete/js/jquery.js
    • Override Path: /application/js/jquery.js
Recent Tutorials
Setting addon/theme version compatibility in the marketplace
Jan 9, 2024

For developers worn out with setting the latest addon or theme version manually across too many core versions, here is a JavaScript bookmarklet to do it for you.

How to get the locale of a page
Jan 8, 2024
By mandako.

Now, why don't we just have a getLocale() method on Page objects beats me, but here's how you work around it

Using a Redis Server
Jun 16, 2023
By mlocati.

How to configure Concrete to use one or more Redis servers to persist the cache.

Using the Concrete Migration Tool Addon
Apr 27, 2023

How to use the Concrete CMS Migration Tool

How To Add Page Last Updated To Your Concrete CMS Pages
Mar 7, 2023

Concrete CMS has a page attribute you can add to a global area called "Page Date Modified." Here's how to add it

How To Exclude Subpages from Navigation
Dec 24, 2022

How to exclude subpages from navigation - useful for a news or blog link in your main navigation where you don't want all the subpages to appear in a drop down menu.

Improvements?

Let us know by posting here.