├── CHANGELOG.md ├── phpstan.neon ├── ecs.php ├── src ├── translations │ └── en │ │ └── empty-coalesce.php ├── EmptyCoalesce.php ├── twigextensions │ └── EmptyCoalesceTwigExtension.php ├── Node │ └── Expression │ │ └── EmptyCoalesceExpression.php └── icon.svg ├── LICENSE.md ├── test └── Twig │ └── Tests │ └── Node │ └── Expression │ └── EmptyCoalesceTest.php ├── composer.json └── README.md /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Empty Coalesce Changelog 2 | 3 | ## 5.0.0 - 2024.04.13 4 | ### Added 5 | * Initial Craft CMS 5 release 6 | -------------------------------------------------------------------------------- /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon 3 | 4 | parameters: 5 | level: 5 6 | paths: 7 | - src 8 | -------------------------------------------------------------------------------- /ecs.php: -------------------------------------------------------------------------------- 1 | paths([ 8 | __DIR__ . '/src', 9 | __FILE__, 10 | ]); 11 | $ecsConfig->parallel(); 12 | $ecsConfig->sets([SetList::CRAFT_CMS_4]); 13 | }; 14 | -------------------------------------------------------------------------------- /src/translations/en/empty-coalesce.php: -------------------------------------------------------------------------------- 1 | 'Empty Coalesce plugin loaded', 19 | ]; 20 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) nystudio107 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /test/Twig/Tests/Node/Expression/EmptyCoalesceTest.php: -------------------------------------------------------------------------------- 1 | view->registerTwigExtension(new EmptyCoalesceTwigExtension()); 57 | 58 | Craft::info( 59 | Craft::t( 60 | 'empty-coalesce', 61 | '{name} plugin loaded', 62 | ['name' => $this->name] 63 | ), 64 | __METHOD__ 65 | ); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/twigextensions/EmptyCoalesceTwigExtension.php: -------------------------------------------------------------------------------- 1 | 57 | */ 58 | public function getOperators(): array 59 | { 60 | return [ 61 | // Unary operators 62 | [], 63 | // Binary operators 64 | [ 65 | '???' => [ 66 | 'precedence' => 300, 67 | 'class' => EmptyCoalesceExpression::class, 68 | 'associativity' => ExpressionParser::OPERATOR_RIGHT, 69 | ], 70 | ], 71 | ]; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-emptycoalesce/badges/quality-score.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-emptycoalesce/?branch=v5) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-emptycoalesce/badges/coverage.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-emptycoalesce/?branch=v5) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-emptycoalesce/badges/build.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-emptycoalesce/build-status/v5) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-emptycoalesce/badges/code-intelligence.svg?b=v5)](https://scrutinizer-ci.com/code-intelligence) 2 | 3 | # Empty Coalesce plugin for Craft CMS 5.x 4 | 5 | Empty Coalesce adds the ??? operator to Twig that will return the first thing that is defined, not null, and not empty. 6 | 7 | ![Screenshot](./docs/docs/resources/img/plugin-logo.png) 8 | 9 | ## Requirements 10 | 11 | This plugin requires Craft CMS 5.0.0 or later. 12 | 13 | ## Installation 14 | 15 | To install the plugin, follow these instructions. 16 | 17 | 1. Open your terminal and go to your Craft project: 18 | 19 | cd /path/to/project 20 | 21 | 2. Then tell Composer to load the plugin: 22 | 23 | composer require nystudio107/craft-emptycoalesce 24 | 25 | 3. Install the plugin via `./craft install/plugin empty-coalesce` via the CLI, or in the Control Panel, go to Settings → Plugins and click the “Install” button for Empty Coalesce. 26 | 27 | You can also install Empty Coalesce via the **Plugin Store** in the Craft Control Panel. 28 | 29 | ## Documentation 30 | 31 | Click here -> [Empty Coalesce Documentation](https://nystudio107.com/plugins/empty-coalesce/documentation) 32 | 33 | ## Empty Coalesce Roadmap 34 | 35 | Some things to do, and ideas for potential features: 36 | 37 | * Submit a PR to the Twig project to get it into core 38 | 39 | Brought to you by [nystudio107](https://nystudio107.com/) 40 | -------------------------------------------------------------------------------- /src/Node/Expression/EmptyCoalesceExpression.php: -------------------------------------------------------------------------------- 1 | setAttribute('ignore_strict_check', true); 54 | $left->setAttribute('is_defined_test', false); 55 | 56 | $right->setAttribute('ignore_strict_check', true); 57 | $right->setAttribute('is_defined_test', false); 58 | parent::__construct( 59 | ['left' => $left, 'right' => $right], 60 | ['ignore_strict_check' => true, 'is_defined_test' => false], 61 | $lineno 62 | ); 63 | } 64 | 65 | public function compile(Compiler $compiler): void 66 | { 67 | //$this->getNode('expr1')->setAttribute('always_defined', true); 68 | $compiler 69 | ->raw('((' . self::class . '::empty(') 70 | ->subcompile($this->getNode('left')) 71 | ->raw(') ? null : ') 72 | ->subcompile($this->getNode('left')) 73 | ->raw(') ?? (' . self::class . '::empty(') 74 | ->subcompile($this->getNode('right')) 75 | ->raw(') ? null : ') 76 | ->subcompile($this->getNode('right')) 77 | ->raw('))') 78 | ; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 12 | 13 | 19 | 25 | 31 | 32 | 33 | 34 | --------------------------------------------------------------------------------