Skip to content

Commit

Permalink
Use a special Twig env for Template UI elements w/ the CP extension
Browse files Browse the repository at this point in the history
Resolves #16267
  • Loading branch information
brandonkelly committed Dec 9, 2024
1 parent 00b415b commit 6767471
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- The Updates utility now shows an action menu for each plugin, with “Copy plugin handle” and “Copy package name” options. ([#16281](https://github.com/craftcms/cms/discussions/16281))
- The Queue Manager utility now shows jobs’ class names. ([#16228](https://github.com/craftcms/cms/pull/16228))
- Improved the wording of field instance action labels. ([#16261](https://github.com/craftcms/cms/discussions/16261))
- Templates rendered for “Template” field layout UI elements can now call control panel template functions like `elementChip()` and `elementCard()`. ([#16267](https://github.com/craftcms/cms/issues/16267))
- Improved the error output for nested elements when they can’t be resaved via `resave` commands.
- `resave` commands’ `--drafts`, `--provisional-drafts`, and `--revisions` options can now be set to `null`, causing elements to be resaved regardless of whether they’re drafts/provisional drafts/revisions.

Expand Down Expand Up @@ -58,6 +59,7 @@
- Added `craft\mail\Mailer::$siteId`.
- Added `craft\mail\Mailer::$siteOverrides`.
- Added `craft\models\MailSettings::$siteOverrides`.
- Added `craft\web\View::setTwig()`.
- `craft\elements\NestedElementManager::getIndexHtml()` now supports passing `defaultSort` in the `$config` array. ([#16236](https://github.com/craftcms/cms/discussions/16236))
- `craft\elements\conditions\entries\MatrixFieldConditionRule` is now an alias of `FieldConditionRule`.
- `craft\helpers\Cp::elementIndexHtml()` now supports passing `defaultSort` in the `$config` array, when `sources` is `null`. ([#16236](https://github.com/craftcms/cms/discussions/16236))
Expand Down
32 changes: 31 additions & 1 deletion src/fieldlayoutelements/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use craft\base\ElementInterface;
use craft\helpers\Cp;
use craft\helpers\Html;
use craft\web\twig\CpExtension;
use craft\web\twig\Environment;
use craft\web\View;
use Throwable;

Expand All @@ -22,6 +24,25 @@
*/
class Template extends BaseUiElement
{
private static Environment $twig;

/**
* @return Environment
*/
private static function twig(): Environment
{
if (!isset(self::$twig)) {
$view = Craft::$app->getView();
$templateMode = $view->getTemplateMode();
$view->setTemplateMode(View::TEMPLATE_MODE_SITE);
self::$twig = Craft::$app->getView()->createTwig();
self::$twig->addExtension(new CpExtension());
$view->setTemplateMode($templateMode);
}

return self::$twig;
}

/**
* @var string The template path
*/
Expand Down Expand Up @@ -102,13 +123,22 @@ public function formHtml(?ElementInterface $element = null, bool $static = false
return $this->_error(Craft::t('app', 'No template path has been chosen yet.'), 'warning');
}

$view = Craft::$app->getView();
$templateMode = $view->getTemplateMode();
$view->setTemplateMode(View::TEMPLATE_MODE_SITE);
$twig = $view->getTwig();
$view->setTwig(self::twig());

try {
$content = trim(Craft::$app->getView()->renderTemplate($this->template, [
$content = trim($view->renderTemplate($this->template, [
'element' => $element,
'static' => $static,
], $this->templateMode));
} catch (Throwable $e) {
return $this->_error($e->getMessage(), 'error');
} finally {
$view->setTwig($twig);
$view->setTemplateMode($templateMode);
}

if ($content === '') {
Expand Down
15 changes: 15 additions & 0 deletions src/web/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,21 @@ public function getTwig(): Environment
: $this->_siteTwig ?? ($this->_siteTwig = $this->createTwig());
}

/**
* Sets the Twig environment for the current template mode.
*
* @param Environment $twig
* @since 5.6.0
*/
public function setTwig(Environment $twig): void
{
if ($this->_templateMode === self::TEMPLATE_MODE_CP) {
$this->_cpTwig = $twig;
} else {
$this->_siteTwig = $twig;
}
}

/**
* Creates a new Twig environment.
*
Expand Down

0 comments on commit 6767471

Please sign in to comment.