Created
September 6, 2019 19:24
-
-
Save samhernandez/24e4b1be3c32a8b7d09c3265517cb26b to your computer and use it in GitHub Desktop.
Craft CMS - conditionally propagate entry and category titles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: config/app.php | |
/** | |
* Yii Application Config | |
* | |
* Edit this file at your own risk! | |
* | |
* The array returned by this file will get merged with | |
* vendor/craftcms/cms/src/config/app.php and app.[web|console].php, when | |
* Craft's bootstrap script is defining the configuration for the entire | |
* application. | |
* | |
* You can define custom modules and system components, and even override the | |
* built-in system components. | |
* | |
* If you want to modify the application config for *only* web requests or | |
* *only* console requests, create an app.web.php or app.console.php file in | |
* your config/ folder, alongside this one. | |
*/ | |
return [ | |
'modules' => [ | |
'my-module' => \modules\Module::class, | |
], | |
'bootstrap' => ['my-module'], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: modules\Module.php | |
namespace modules; | |
use Craft; | |
use craft\base\Element; | |
use craft\elements\Category; | |
use craft\elements\Entry; | |
use yii\base\Event; | |
/** | |
* Custom module class. | |
* | |
* This class will be available throughout the system via: | |
* `Craft::$app->getModule('my-module')`. | |
* | |
* You can change its module ID ("my-module") to something else from | |
* config/app.php. | |
* | |
* If you want the module to get loaded on every request, uncomment this line | |
* in config/app.php: | |
* | |
* 'bootstrap' => ['my-module'] | |
* | |
* Learn more about Yii module development in Yii's documentation: | |
* http://www.yiiframework.com/doc-2.0/guide-structure-modules.html | |
*/ | |
class Module extends \yii\base\Module | |
{ | |
/** | |
* Initializes the module. | |
*/ | |
public function init() | |
{ | |
// Set a @modules alias pointed to the modules/ directory | |
Craft::setAlias('@modules', __DIR__); | |
// Set the controllerNamespace based on whether this is a console or web request | |
if (Craft::$app->getRequest()->getIsConsoleRequest()) { | |
$this->controllerNamespace = 'modules\\console\\controllers'; | |
} else { | |
$this->controllerNamespace = 'modules\\controllers'; | |
} | |
parent::init(); | |
if (Craft::$app->request->isCpRequest) { | |
Event::on(Element::class, Element::EVENT_BEFORE_SAVE, function (Event $event) { | |
// Static var to memoize titles | |
static $titles = []; | |
// Entry section handles we want titles to propagate for | |
$sectionHandles = ['news', 'sitePages']; | |
// For readability | |
$element = $event->sender; | |
// If the element is a Category or an Entry in a desired section ... | |
if ($element instanceof Category || | |
($element instanceof Entry && in_array($element->section->handle, $sectionHandles)) | |
) { | |
if (!$element->propagating) { | |
// This is the first save, so memoize the title | |
// and let it save | |
$titles[$element->id] = $element->title; | |
} else if (isset($titles[$element->id])) { | |
// This is a subsequent save while propagating | |
// so propagate the first saved title | |
$element->title = $titles[$element->id]; | |
} | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment